11package httpserver .responder ;
22
33import httpserver .AppConfig ;
4+ import httpserver .file .Html ;
5+ import httpserver .file .PathExaminer ;
46import httpserver .header .Header ;
57import httpserver .Request ;
68import httpserver .response .Response ;
911import java .io .IOException ;
1012import java .nio .file .Path ;
1113
12- import static httpserver .file .FileHelpers .tempDir ;
13- import static httpserver .file .FileHelpers .tempFileOptions ;
14- import static java .nio .file .Files .write ;
1514import static org .junit .Assert .*;
1615import static org .mockito .Mockito .*;
1716
1817public class GetResponderTest {
1918
2019 private final GetResponder getResponder ;
21- private final Path root ;
22- private final Path fileWithContents ;
2320 private final AppConfig appConfigMock ;
21+ private final RouteMap routeMapMock ;
22+ private final PathExaminer pathExaminerMock ;
23+ private final Path rootMock ;
24+ private final Html htmlMock ;
2425
2526 public GetResponderTest () throws IOException {
26- getResponder = new GetResponder ();
27- root = tempDir ();
27+ routeMapMock = mock (RouteMap .class );
28+ pathExaminerMock = mock (PathExaminer .class );
29+
30+ htmlMock = mock (Html .class );
31+ getResponder = new GetResponder (routeMapMock , pathExaminerMock , htmlMock );
2832 appConfigMock = mock (AppConfig .class );
29- when (appConfigMock .getRoot ()).thenReturn (root );
30- fileWithContents = tempFileOptions (root , "aaa" , ".gif" );
31- write (fileWithContents , "Test file contents" .getBytes ());
33+ rootMock = mock (Path .class );
34+ when (appConfigMock .getRoot ()).thenReturn (rootMock );
3235 }
3336
3437 @ Test
@@ -43,64 +46,58 @@ public void returns404ForBadPath() throws Exception {
4346 }
4447
4548 @ Test
46- public void getRequestForFile () throws Exception {
47- String fileName = fileWithContents .toString ().substring (root .toString ().length ());
48- Request request = new Request ("GET" , fileName , new Header [0 ], "" );
49+ public void ifPathInRouteMapGetsResponderAndCallsRespond () throws Exception {
50+ Responder responderMock = mock (Responder .class );
51+ when (routeMapMock .contains ("/example_route" )).thenReturn (true );
52+ when (routeMapMock .getResponder ("/example_route" )).thenReturn (responderMock );
53+ Request request = new Request ("GET" , "/example_route" , new Header [0 ], "" );
4954
50- Response response = getResponder .respond (appConfigMock , request );
55+ getResponder .respond (appConfigMock , request );
5156
52- Header [] expectedHeaders = new Header []{new Header ("Content-Type" , "image/gif" )};
53- assertEquals (200 , response .getStatusCode ());
54- assertEquals ("Test file contents" , new String (response .getPayload ()));
55- assertEquals (expectedHeaders , response .getHeaders ());
57+ verify (routeMapMock ).contains ("/example_route" );
58+ verify (routeMapMock ).getResponder ("/example_route" );
59+ verify (responderMock ).respond (appConfigMock , request );
5660 }
5761
5862 @ Test
59- public void getRequestForDir () throws Exception {
60- Request request = new Request ("GET" , "/" , new Header [0 ], "" );
63+ public void getsFileContentsForPath () throws Exception {
64+ Path fullPathMock = mock (Path .class );
65+ when (pathExaminerMock .getFullPath (rootMock , "/filename" )).thenReturn (fullPathMock );
66+ when (pathExaminerMock .pathExists (fullPathMock )).thenReturn (true );
67+ when (pathExaminerMock .isFile (fullPathMock )).thenReturn (true );
68+ byte [] payloadMock = new byte [0 ];
69+ when (pathExaminerMock .fileContents (fullPathMock )).thenReturn (payloadMock );
70+ Request request = new Request ("GET" , "/filename" , new Header [0 ], "" );
6171
6272 Response response = getResponder .respond (appConfigMock , request );
6373
74+ verify (appConfigMock ).getRoot ();
75+ verify (pathExaminerMock ).getFullPath (rootMock , "/filename" );
76+ verify (pathExaminerMock ).pathExists (fullPathMock );
77+ verify (pathExaminerMock ).isFile (fullPathMock );
6478 assertEquals (200 , response .getStatusCode ());
65- assertTrue (new String (response .getPayload ()).contains ("<a href=" ));
79+ assertEquals (payloadMock , response .getPayload ());
80+ assertEquals ("Content-Type" , response .getHeaders ()[0 ].getKey ());
6681 }
6782
6883 @ Test
69- public void getRequestToCoffee () throws Exception {
70- Request request = new Request ("GET" , "/coffee" , new Header [0 ], "" );
71-
72- Response response = getResponder .respond (appConfigMock , request );
73-
74- assertEquals (418 , response .getStatusCode ());
75- assertTrue (new String (response .getPayload ()).contains ("I'm a teapot" ));
76- }
77-
78- @ Test
79- public void getRequestToTea () throws Exception {
80- Request request = new Request ("GET" , "/tea" , new Header [0 ], "" );
81-
82- Response response = getResponder .respond (appConfigMock , request );
83-
84- assertEquals (200 , response .getStatusCode ());
85- }
86-
87- @ Test
88- public void getRequestToCookieWithQueryString () throws Exception {
89- Request request = new Request ("GET" , "/cookie" , new Header [0 ], "type=chocolate" );
90-
91- Response response = getResponder .respond (appConfigMock , request );
92-
93- assertEquals (200 , response .getStatusCode ());
94- assertEquals ("Eat" , new String (response .getPayload ()));
95- }
96-
97- @ Test
98- public void getRequestToEatCookie () throws Exception {
99- Request request = new Request ("GET" , "/eat_cookie" , new Header [0 ], "type=chocolate" );
84+ public void getsDirContentsForPath () throws Exception {
85+ Path fullPathMock = mock (Path .class );
86+ when (pathExaminerMock .getFullPath (rootMock , "/" )).thenReturn (fullPathMock );
87+ when (pathExaminerMock .pathExists (fullPathMock )).thenReturn (true );
88+ when (pathExaminerMock .isFile (fullPathMock )).thenReturn (false );
89+ Path path1 = mock (Path .class );
90+ Path path2 = mock (Path .class );
91+ Path [] pathArrayMock = new Path []{path1 , path2 };
92+ when (pathExaminerMock .directoryContents (fullPathMock )).thenReturn (pathArrayMock );
93+ Request request = new Request ("GET" , "/" , new Header [0 ], "" );
10094
10195 Response response = getResponder .respond (appConfigMock , request );
10296
97+ verify (pathExaminerMock ).isFile (fullPathMock );
98+ verify (pathExaminerMock ).directoryContents (fullPathMock );
99+ verify (htmlMock ).linkString (rootMock , path1 );
100+ verify (htmlMock ).linkString (rootMock , path2 );
103101 assertEquals (200 , response .getStatusCode ());
104- assertEquals ("mmmm chocolate" , new String (response .getPayload ()));
105102 }
106103}
0 commit comments