FileRequestHandler implements RequestHandler { @Override public HttpResponse processRequest(HttpRequest request) { HttpResponse response = new HttpResponse(); response.setRequest(request); String path = request.getPath(); File file = resolveFile(path); if (file == null) { response.setStatusCode(HttpStatusCode.NOT_FOUND); return response; } byte[] content = readContentFromFile(file); response.setBody(content); response.setStatusCode(HttpStatusCode.OK); return response; } FileRequestHandler(File documentRoot); @Override boolean accepts(HttpRequest request); @Override HttpResponse processRequest(HttpRequest request); @Override String toString();  }
@Test public void processRequest_GET_NoSuchFile() throws Exception { FileRequestHandler h = getRequestHandlerToTest(); HttpRequest request = new HttpRequest(HttpMethod.GET, "/I/am/sure/there/is/no/such/file.txt"); HttpResponse response = h.processRequest(request); assertEquals(HttpStatusCode.NOT_FOUND, response.getStatusCode()); assertEquals(request, response.getRequest()); }
@Test public void processRequest_GET_FileExists() throws Exception { File file = new File(scratchDirectory, "test.txt"); assertTrue(Files.write(file, "test-content\n")); FileRequestHandler rh = new FileRequestHandler(scratchDirectory); HttpRequest request = new HttpRequest(HttpMethod.GET, "/test.txt"); HttpResponse response = rh.processRequest(request); assertEquals(HttpStatusCode.OK, response.getStatusCode()); List<HttpHeader> header = response.getHeader(HttpEntityHeader.CONTENT_LENGTH); assertEquals(1, header.size()); HttpHeader h = header.get(0); assertEquals("13", h.getFieldBody()); assertEquals(request, response.getRequest()); }