Skip to content

Commit 72f5efb

Browse files
committed
Add PutResponder
1 parent dbd2705 commit 72f5efb

File tree

6 files changed

+75
-30
lines changed

6 files changed

+75
-30
lines changed

src/main/java/httpserver/ResponderSupplierFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public ResponderSupplier makeResponderSupplier() {
1818
methodResponderMap.put(Method.POST, new PostResponder(
1919
new PathExaminer(),
2020
new FileOperator()));
21-
methodResponderMap.put(Method.PUT, new PutResponder());
21+
methodResponderMap.put(Method.PUT, new PutResponder(
22+
new PathExaminer(),
23+
new FileOperator()));
2224
return new ResponderSupplier(methodResponderMap, new InvalidMethodResponder());
2325
}
2426

src/main/java/httpserver/responder/PostResponder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public PostResponder(PathExaminer pathExaminer, FileOperator fileOperator) {
2222
@Override
2323
public Response respond(AppConfig appConfig, Request request) throws IOException {
2424
if (allowed(request.getPathString())) {
25-
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(),
26-
request.getPathString());
25+
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
2726
if (pathExaminer.pathExists(fullPath)) {
2827
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
2928
return new OkResponse(fileOperator.readContents(fullPath));

src/main/java/httpserver/responder/PutResponder.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,40 @@
22

33
import httpserver.AppConfig;
44
import httpserver.Request;
5+
import httpserver.file.FileOperator;
6+
import httpserver.file.PathExaminer;
57
import httpserver.response.MethodNotAllowedResponse;
8+
import httpserver.response.NotFoundResponse;
69
import httpserver.response.OkResponse;
710
import httpserver.response.Response;
811

12+
import java.io.IOException;
13+
import java.nio.file.Path;
14+
915
public class PutResponder implements Responder {
16+
private final PathExaminer pathExaminer;
17+
private final FileOperator fileOperator;
18+
19+
public PutResponder(PathExaminer pathExaminer, FileOperator fileOperator) {
20+
this.pathExaminer = pathExaminer;
21+
this.fileOperator = fileOperator;
22+
}
23+
1024
@Override
11-
public Response respond(AppConfig appconfig, Request request) {
12-
if (request.getPathString().equals("/form")) {
13-
return new OkResponse(new byte[0]);
25+
public Response respond(AppConfig appConfig, Request request) throws IOException {
26+
if (allowed(request.getPathString())) {
27+
Path fullPath = pathExaminer.getFullPath(appConfig.getRoot(), request.getPathString());
28+
if (pathExaminer.pathExists(fullPath)) {
29+
fileOperator.replaceContents(fullPath, request.getBody().getBytes());
30+
return new OkResponse(fileOperator.readContents(fullPath));
31+
} else {
32+
return new NotFoundResponse();
33+
}
1434
}
1535
return new MethodNotAllowedResponse();
1636
}
37+
38+
private boolean allowed(String pathString) {
39+
return pathString.equals("/form");
40+
}
1741
}

src/test/java/httpserver/SocketHandlerTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,6 @@ public void returns405ForPOST() throws Exception {
113113
assertEquals(expected, stringOutputForRequestBytes(request));
114114
}
115115

116-
@Test
117-
public void returns200ForPUTToForm() throws Exception {
118-
byte[] request = ("PUT /form HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\nMy=Data").getBytes();
119-
120-
String expected = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
121-
assertEquals(expected, stringOutputForRequestBytes(request));
122-
}
123-
124-
@Test
125-
public void returns405ForPUT() throws Exception {
126-
byte[] request = ("PUT /example HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\n").getBytes();
127-
128-
String expected = "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n";
129-
assertEquals(expected, stringOutputForRequestBytes(request));
130-
}
131-
132116
@Test
133117
public void returns405ForUnsupportedMethod() throws Exception {
134118
byte[] request = ("XYZABC /example HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\n").getBytes();

src/test/java/httpserver/responder/PostResponderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.junit.Test;
1010

1111
import java.nio.file.Path;
12-
import java.util.Arrays;
1312

1413
import static junit.framework.TestCase.*;
1514
import static org.mockito.Mockito.*;

src/test/java/httpserver/responder/PutResponderTest.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,73 @@
11
package httpserver.responder;
22

33
import httpserver.AppConfig;
4+
import httpserver.file.FileOperator;
5+
import httpserver.file.PathExaminer;
46
import httpserver.header.Header;
57
import httpserver.Request;
68
import httpserver.response.Response;
9+
import org.junit.Ignore;
710
import org.junit.Test;
811

9-
import static org.junit.Assert.*;
12+
import java.nio.file.Path;
13+
14+
import static junit.framework.TestCase.assertEquals;
15+
import static org.mockito.ArgumentMatchers.any;
1016
import static org.mockito.Mockito.mock;
17+
import static org.mockito.Mockito.verify;
18+
import static org.mockito.Mockito.when;
1119

1220
public class PutResponderTest {
13-
1421
private final PutResponder putResponder;
1522
private final AppConfig appConfigMock;
23+
private final PathExaminer pathExaminerMock;
24+
private final FileOperator fileOperatorMock;
25+
private final Path rootMock;
26+
private final Path fullPathMock;
27+
private final byte[] fileContentsMock;
1628

1729
public PutResponderTest() {
30+
pathExaminerMock = mock(PathExaminer.class);
31+
fileOperatorMock = mock(FileOperator.class);
32+
putResponder = new PutResponder(pathExaminerMock, fileOperatorMock);
33+
rootMock = mock(Path.class);
1834
appConfigMock = mock(AppConfig.class);
19-
putResponder = new PutResponder();
35+
when(appConfigMock.getRoot()).thenReturn(rootMock);
36+
fullPathMock = mock(Path.class);
37+
fileContentsMock = new byte[0];
2038
}
2139

2240
@Test
23-
public void gets200StatusCode() throws Exception {
24-
Request request = new Request("PUT", "/form", new Header[0], "");
41+
public void overwritesFileWithDataIfAllowedAndExists() throws Exception {
42+
String pathString = "/form";
43+
when(pathExaminerMock.pathExists(any())).thenReturn(true);
44+
when(pathExaminerMock.getFullPath(rootMock, pathString)).thenReturn(fullPathMock);
45+
when(fileOperatorMock.readContents(fullPathMock)).thenReturn(fileContentsMock);
46+
Request request = new Request("POST", pathString, new Header[0], "", "data=example");
2547

2648
Response response = putResponder.respond(appConfigMock, request);
2749

2850
assertEquals(200, response.getStatusCode());
51+
verify(fileOperatorMock).replaceContents(fullPathMock, "data=example".getBytes());
52+
verify(fileOperatorMock).readContents(fullPathMock);
53+
assertEquals(fileContentsMock, response.getPayload());
54+
}
55+
56+
@Test
57+
public void returns404IfAllowedButDoesntExist() throws Exception {
58+
String pathString = "/form";
59+
when(pathExaminerMock.pathExists(any())).thenReturn(false);
60+
when(pathExaminerMock.getFullPath(rootMock, pathString)).thenReturn(fullPathMock);
61+
Request request = new Request("POST", pathString, new Header[0], "", "data=example");
62+
63+
Response response = putResponder.respond(appConfigMock, request);
64+
65+
assertEquals(404, response.getStatusCode());
2966
}
3067

3168
@Test
32-
public void returns405IfNotFormUrl() throws Exception {
33-
Request request = new Request("POST", "/file1.txt", new Header[0], "");
69+
public void returns405IfNotAllowed() throws Exception {
70+
Request request = new Request("POST", "/not_allowed", new Header[0], "", "data=example");
3471

3572
Response response = putResponder.respond(appConfigMock, request);
3673

0 commit comments

Comments
 (0)