|
1 | 1 | package httpserver.responder; |
2 | 2 |
|
3 | 3 | import httpserver.AppConfig; |
| 4 | +import httpserver.file.FileOperator; |
| 5 | +import httpserver.file.PathExaminer; |
4 | 6 | import httpserver.header.Header; |
5 | 7 | import httpserver.Request; |
6 | 8 | import httpserver.response.Response; |
| 9 | +import org.junit.Ignore; |
7 | 10 | import org.junit.Test; |
8 | 11 |
|
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; |
10 | 16 | import static org.mockito.Mockito.mock; |
| 17 | +import static org.mockito.Mockito.verify; |
| 18 | +import static org.mockito.Mockito.when; |
11 | 19 |
|
12 | 20 | public class PutResponderTest { |
13 | | - |
14 | 21 | private final PutResponder putResponder; |
15 | 22 | 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; |
16 | 28 |
|
17 | 29 | public PutResponderTest() { |
| 30 | + pathExaminerMock = mock(PathExaminer.class); |
| 31 | + fileOperatorMock = mock(FileOperator.class); |
| 32 | + putResponder = new PutResponder(pathExaminerMock, fileOperatorMock); |
| 33 | + rootMock = mock(Path.class); |
18 | 34 | appConfigMock = mock(AppConfig.class); |
19 | | - putResponder = new PutResponder(); |
| 35 | + when(appConfigMock.getRoot()).thenReturn(rootMock); |
| 36 | + fullPathMock = mock(Path.class); |
| 37 | + fileContentsMock = new byte[0]; |
20 | 38 | } |
21 | 39 |
|
22 | 40 | @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"); |
25 | 47 |
|
26 | 48 | Response response = putResponder.respond(appConfigMock, request); |
27 | 49 |
|
28 | 50 | 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()); |
29 | 66 | } |
30 | 67 |
|
31 | 68 | @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"); |
34 | 71 |
|
35 | 72 | Response response = putResponder.respond(appConfigMock, request); |
36 | 73 |
|
|
0 commit comments