### Question:
StaticContentHandler { void handle(Http.RequestMethod method, ServerRequest request, ServerResponse response) { if ((method != Http.Method.GET) && (method != Http.Method.HEAD)) { request.next(); return; } String requestPath = request.path().toString(); if (requestPath.startsWith("/")) { requestPath = requestPath.substring(1); } try { if (!doHandle(method, requestPath, request, response)) { request.next(); } } catch (HttpException httpException) { if (httpException.status().code() == Http.Status.NOT_FOUND_404.code()) { request.next(); } else { throw httpException; } } catch (Exception e) { LOGGER.log(Level.FINE, "Failed to access static resource", e); throw new HttpException("Cannot access static resource!", Http.Status.INTERNAL_SERVER_ERROR_500, e); } } StaticContentHandler(String welcomeFilename, ContentTypeSelector contentTypeSelector);   }### Answer:
@Test public void handleRoot() throws Exception { ServerRequest request = mockRequestWithPath("/"); ServerResponse response = mock(ServerResponse.class); TestContentHandler handler = new TestContentHandler("/root", true); handler.handle(Http.Method.GET, request, response); verify(request, never()).next(); assertThat(handler.path, is(Paths.get("/root").toAbsolutePath().normalize())); }
@Test public void handleIllegalMethod() throws Exception { ServerRequest request = mockRequestWithPath("/"); ServerResponse response = mock(ServerResponse.class); TestContentHandler handler = new TestContentHandler("/root", true); handler.handle(Http.Method.POST, request, response); verify(request).next(); assertThat(handler.counter.get(), is(0)); }
@Test public void handleValid() throws Exception { ServerRequest request = mockRequestWithPath("/foo/some.txt"); ServerResponse response = mock(ServerResponse.class); TestContentHandler handler = new TestContentHandler("/root", true); handler.handle(Http.Method.GET, request, response); verify(request, never()).next(); assertThat(handler.path, is(Paths.get("/root/foo/some.txt").toAbsolutePath().normalize())); }
@Test public void handleOutside() throws Exception { ServerRequest request = mockRequestWithPath("/../foo/some.txt"); ServerResponse response = mock(ServerResponse.class); TestContentHandler handler = new TestContentHandler("/root", true); handler.handle(Http.Method.GET, request, response); verify(request).next(); assertThat(handler.counter.get(), is(0)); }
@Test public void handleNextOnFalse() throws Exception { ServerRequest request = mockRequestWithPath("/"); ServerResponse response = mock(ServerResponse.class); TestContentHandler handler = new TestContentHandler("/root", false); handler.handle(Http.Method.GET, request, response); verify(request).next(); assertThat(handler.counter.get(), is(1)); }
@Test public void classpathHandleSpaces() throws Exception { ServerRequest request = mockRequestWithPath("foo/I have spaces.txt"); ServerResponse response = mock(ServerResponse.class); TestClassPathContentHandler handler = new TestClassPathContentHandler("/root", true); handler.handle(Http.Method.GET, request, response); verify(request, never()).next(); assertThat(handler.counter.get(), is(1)); }