|
| 1 | +package httpserver.responder; |
| 2 | + |
| 3 | +import httpserver.AppConfig; |
| 4 | +import httpserver.Request; |
| 5 | +import httpserver.file.FileOperator; |
| 6 | +import httpserver.file.PathExaminer; |
| 7 | +import httpserver.header.Header; |
| 8 | +import httpserver.response.Response; |
| 9 | +import org.junit.Ignore; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.nio.file.Path; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | +import static org.mockito.Mockito.*; |
| 16 | + |
| 17 | +public class DeleteResponderTest { |
| 18 | + private final Path rootMock; |
| 19 | + private final AppConfig appConfigMock; |
| 20 | + private final Path fullPathMock; |
| 21 | + private PathExaminer pathExaminerMock; |
| 22 | + private FileOperator fileOperatorMock; |
| 23 | + private DeleteResponder deleteResponder; |
| 24 | + |
| 25 | + public DeleteResponderTest() { |
| 26 | + pathExaminerMock = mock(PathExaminer.class); |
| 27 | + fileOperatorMock = mock(FileOperator.class); |
| 28 | + deleteResponder = new DeleteResponder(pathExaminerMock, fileOperatorMock); |
| 29 | + rootMock = mock(Path.class); |
| 30 | + appConfigMock = mock(AppConfig.class); |
| 31 | + when(appConfigMock.getRoot()).thenReturn(rootMock); |
| 32 | + fullPathMock = mock(Path.class); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void returns200DeletesFileWhenExists() throws Exception { |
| 37 | + String pathString = "/form"; |
| 38 | + when(pathExaminerMock.pathExists(any())).thenReturn(true); |
| 39 | + when(pathExaminerMock.getFullPath(rootMock, pathString)).thenReturn(fullPathMock); |
| 40 | + Request request = new Request("DELETE", pathString, new Header[0], "", "data=example"); |
| 41 | + |
| 42 | + Response response = deleteResponder.respond(appConfigMock, request); |
| 43 | + |
| 44 | + verify(fileOperatorMock).deleteFileAtPath(fullPathMock); |
| 45 | + assertEquals(200, response.getStatusCode()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void returns404WhenAllowedNotExists() throws Exception { |
| 50 | + String pathString = "/form"; |
| 51 | + when(pathExaminerMock.pathExists(any())).thenReturn(false); |
| 52 | + when(pathExaminerMock.getFullPath(rootMock, pathString)).thenReturn(fullPathMock); |
| 53 | + Request request = new Request("DELETE", pathString, new Header[0], "", "data=example"); |
| 54 | + |
| 55 | + Response response = deleteResponder.respond(appConfigMock, request); |
| 56 | + |
| 57 | + assertEquals(404, response.getStatusCode()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void returns405WhenNotAllowed() throws Exception { |
| 62 | + Request request = new Request("DELETE", "/not_allowed", new Header[0], ""); |
| 63 | + |
| 64 | + Response response = deleteResponder.respond(appConfigMock, request); |
| 65 | + |
| 66 | + assertEquals(405, response.getStatusCode()); |
| 67 | + } |
| 68 | +} |
0 commit comments