Skip to content

Commit a435d62

Browse files
committed
Change GetResponderTest to test interactions instead of integration
1 parent e58473b commit a435d62

File tree

5 files changed

+84
-83
lines changed

5 files changed

+84
-83
lines changed
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package httpserver;
22

3-
import httpserver.file.FileOperator;
3+
import httpserver.file.Html;
44
import httpserver.file.PathExaminer;
55
import httpserver.responder.*;
66

@@ -10,9 +10,23 @@
1010
public class ResponderSupplierFactory {
1111
public ResponderSupplier makeResponderSupplier() {
1212
Map<Method, Responder> methodResponderMap = new HashMap<>();
13-
methodResponderMap.put(Method.GET, new GetResponder());
13+
methodResponderMap.put(Method.GET, new GetResponder(
14+
getRouteMap(),
15+
new PathExaminer(),
16+
new Html()));
1417
methodResponderMap.put(Method.POST, new PostResponder());
1518
methodResponderMap.put(Method.PUT, new PutResponder());
1619
return new ResponderSupplier(methodResponderMap, new InvalidMethodResponder());
1720
}
21+
22+
private RouteMap getRouteMap() {
23+
Map<String, Responder> routeMap = new HashMap<>();
24+
routeMap.put("/coffee", new CoffeeResponder());
25+
routeMap.put("/tea", new TeaResponder());
26+
routeMap.put("/logs", new LogsResponder());
27+
routeMap.put("/cookie", new CookieResponder());
28+
routeMap.put("/eat_cookie", new EatCookieResponder());
29+
routeMap.put("/parameters", new ParametersResponder());
30+
return new RouteMap(routeMap);
31+
}
1832
}

src/main/java/httpserver/file/Html.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import java.nio.file.Path;
44

55
public class Html {
6-
public static String hrefString(Path root, Path fullFilePath) {
6+
public String hrefString(Path root, Path fullFilePath) {
77
Path diff = root.relativize(fullFilePath);
88
if (new PathExaminer().isDir(fullFilePath)) {
99
return diff.toString() + "/";
1010
}
1111
return diff.toString();
1212
}
1313

14-
public static String linkString(Path root, Path fullFilePath) {
14+
public String linkString(Path root, Path fullFilePath) {
1515
String href = hrefString(root, fullFilePath);
1616
return "<div><a href=\"/" + href +
1717
"\">/" + href + "</a></div>";
Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpserver.responder;
22

33
import httpserver.AppConfig;
4+
import httpserver.file.Html;
45
import httpserver.header.ContentTypeHeader;
56
import httpserver.response.NotFoundResponse;
67
import httpserver.Request;
@@ -9,27 +10,25 @@
910
import httpserver.file.PathExaminer;
1011

1112
import java.nio.file.Path;
12-
import java.util.HashMap;
13-
import java.util.Map;
14-
15-
import static httpserver.file.Html.linkString;
1613

1714
public class GetResponder implements Responder {
1815

1916
private final PathExaminer pathExaminer;
20-
private final Map<String, Responder> specialCaseRouteMap;
17+
private final RouteMap specialCaseRouteMap;
18+
private final Html html;
2119

22-
public GetResponder() {
23-
pathExaminer = new PathExaminer();
24-
specialCaseRouteMap = getRouteMap();
20+
public GetResponder(RouteMap getRouteMap, PathExaminer pathExaminer, Html html) {
21+
this.pathExaminer = pathExaminer;
22+
this.specialCaseRouteMap = getRouteMap;
23+
this.html = html;
2524
}
2625

2726
@Override
2827
public Response respond(AppConfig appConfig, Request request) {
2928
String requestPathString = request.getPathString();
3029

31-
if (specialCaseRouteMap.containsKey(requestPathString)) {
32-
return specialCaseRouteMap.get(requestPathString).respond(appConfig, request);
30+
if (specialCaseRouteMap.contains(requestPathString)) {
31+
return specialCaseRouteMap.getResponder(requestPathString).respond(appConfig, request);
3332
}
3433

3534
Path root = appConfig.getRoot();
@@ -55,7 +54,7 @@ private Response responseForDir(Path root, Path path) {
5554
private String htmlLinksForContents(Path root, Path[] paths) {
5655
String result = "";
5756
for (Path subPath: paths) {
58-
result = result + linkString(root, subPath);
57+
result = result + html.linkString(root, subPath);
5958
}
6059
return result;
6160
}
@@ -66,15 +65,4 @@ private Response responseForFile(Path path) {
6665
okResponse.setHeader(new ContentTypeHeader(path));
6766
return okResponse;
6867
}
69-
70-
private Map<String, Responder> getRouteMap() {
71-
Map<String, Responder> routeMap = new HashMap<>();
72-
routeMap.put("/coffee", new CoffeeResponder());
73-
routeMap.put("/tea", new TeaResponder());
74-
routeMap.put("/logs", new LogsResponder());
75-
routeMap.put("/cookie", new CookieResponder());
76-
routeMap.put("/eat_cookie", new EatCookieResponder());
77-
routeMap.put("/parameters", new ParametersResponder());
78-
return routeMap;
79-
}
8068
}

src/test/java/httpserver/file/HtmlTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,28 @@ public class HtmlTest {
1616
private final Path root;
1717
private final Path fullDirPath;
1818
private final Path fullFilePath;
19+
private final Html html;
1920

2021
public HtmlTest() throws IOException {
2122
root = tempDir();
2223
fullDirPath = tempDirOptions(root);
2324
fullFilePath = tempFileOptions(root, "aaa", "temp");
25+
html = new Html();
2426
}
2527

2628
@Test
2729
public void makesHrefStringFromRootPathAndFullFilePath() throws Exception {
2830
Path root = Paths.get("/Users", "example", "public");
2931
Path fullFilePath = Paths.get("/Users", "example", "public", "example.txt");
3032

31-
assertEquals("example.txt", Html.hrefString(root, fullFilePath));
33+
assertEquals("example.txt", html.hrefString(root, fullFilePath));
3234
}
3335

3436
@Test
3537
public void makesHrefStringFromRootPathAndDirPath() throws Exception {
3638
String expected = fullDirPath.toString().substring(root.toString().length() + 1) + "/";
3739

38-
assertEquals(expected, Html.hrefString(root, fullDirPath));
40+
assertEquals(expected, html.hrefString(root, fullDirPath));
3941
}
4042

4143
@Test
@@ -49,7 +51,7 @@ public void makesHtmlLinkFromRootPathAndDirPath() throws Exception {
4951
String expectedDirLink = "<div><a href=\"/" + expectedDirPath +
5052
"\">/" + expectedDirPath + "</a></div>";
5153

52-
assertEquals(expectedFileLink, Html.linkString(root, fullFilePath));
53-
assertEquals(expectedDirLink, Html.linkString(root, fullDirPath));
54+
assertEquals(expectedFileLink, html.linkString(root, fullFilePath));
55+
assertEquals(expectedDirLink, html.linkString(root, fullDirPath));
5456
}
5557
}
Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package httpserver.responder;
22

33
import httpserver.AppConfig;
4+
import httpserver.file.Html;
5+
import httpserver.file.PathExaminer;
46
import httpserver.header.Header;
57
import httpserver.Request;
68
import httpserver.response.Response;
@@ -9,26 +11,27 @@
911
import java.io.IOException;
1012
import 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;
1514
import static org.junit.Assert.*;
1615
import static org.mockito.Mockito.*;
1716

1817
public 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

Comments
 (0)