Skip to content

Commit 72ae6c7

Browse files
committed
Update RouteMap to Router, change routerForRequest method to respond method
1 parent 7c72830 commit 72ae6c7

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/main/java/httpserver/route/Router.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public boolean canRespond(Request request) {
2424
return false;
2525
}
2626

27-
public Route routeForRequest(Request request) {
27+
private Route routeForRequest(Request request) {
2828
for (Route route : routes) {
2929
if (route.allows(request)) {
3030
return route;
@@ -37,7 +37,7 @@ public Response respond(AppConfig appConfig, Request request) {
3737
Route route = routeForRequest(request);
3838
try {
3939
return route.respond(appConfig, request);
40-
} catch (IOException e) {
40+
} catch (IOException | NullPointerException e) {
4141
return new ServerErrorResponse();
4242
}
4343
}

src/test/java/httpserver/route/RouterTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,21 @@ public RouterTest() {
3030
}
3131

3232
@Test
33-
public void hasRouteTrueWhenARouteAllowsPath() throws Exception {
33+
public void canRespondTrueWhenARouteAllowsPath() throws Exception {
3434
when(routeMock1.allows(any())).thenReturn(false);
3535
when(routeMock2.allows(any())).thenReturn(true);
3636

3737
assertTrue(router.canRespond(null));
3838
}
3939

4040
@Test
41-
public void hasRouteFalseWhenNoRouteAllowsPath() throws Exception {
41+
public void canRespondFalseWhenNoRouteAllowsPath() throws Exception {
4242
when(routeMock1.allows(any())).thenReturn(false);
4343
when(routeMock2.allows(any())).thenReturn(false);
4444

4545
assertFalse(router.canRespond(null));
4646
}
4747

48-
@Test
49-
public void returnsRouteForRequest() throws Exception {
50-
when(routeMock1.allows(any())).thenReturn(false);
51-
when(routeMock2.allows(any())).thenReturn(true);
52-
53-
Route actual = router.routeForRequest(null);
54-
55-
assertEquals(actual, routeMock2);
56-
}
57-
5848
@Test
5949
public void respondsToRequest() throws Exception {
6050
when(routeMock1.allows(any())).thenReturn(false);
@@ -69,7 +59,7 @@ public void respondsToRequest() throws Exception {
6959
}
7060

7161
@Test
72-
public void returns500WhenRouteRespondErrors() throws Exception {
62+
public void returns500WhenRouteRespondThrowsIOException() throws Exception {
7363
when(routeMock1.allows(any())).thenReturn(false);
7464
when(routeMock2.allows(any())).thenReturn(true);
7565
when(routeMock2.respond(any(), any())).thenThrow(new IOException());
@@ -78,4 +68,14 @@ public void returns500WhenRouteRespondErrors() throws Exception {
7868

7969
assertEquals(500, response.getStatusCode());
8070
}
71+
72+
@Test
73+
public void returns500WhenRouteRespondThrowsNullPointerException() throws Exception {
74+
when(routeMock1.allows(any())).thenReturn(false);
75+
when(routeMock2.allows(any())).thenReturn(false);
76+
77+
Response response = router.respond(appConfigMock, requestMock);
78+
79+
assertEquals(500, response.getStatusCode());
80+
}
8181
}

0 commit comments

Comments
 (0)