### Question:
StaticFileHandler implements HttpHandler { @Override public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) { try { return resolveFile(request.path()) .map(ResolvedFile::new) .map(resolvedFile -> HttpResponse.response() .addHeader(CONTENT_TYPE, resolvedFile.mediaType) .body(resolvedFile.content, UTF_8) .build() .stream()) .map(Eventual::of) .orElseGet(() -> new HttpAggregator(NOT_FOUND_HANDLER).handle(request, context)); } catch (IOException e) { return Eventual.of(HttpResponse.response(INTERNAL_SERVER_ERROR).build().stream()); } } StaticFileHandler(File dir); @Override Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context);  }### Answer:
@Test public void should404ForMissingFiles() { assertThat(handle(get("/index.html").build()), hasStatus(NOT_FOUND)); assertThat(handle(get("/notfound.html").build()), hasStatus(NOT_FOUND)); assertThat(handle(get("/foo/bar").build()), hasStatus(NOT_FOUND)); }
@Test public void shouldServeExistingFiles() throws Exception { writeFile("index.html", "Hello world"); writeFile("foo.js", "Blah"); mkdir("/a/b"); writeFile("a/b/good", "hi"); assertThat("asking for /index.html", handle(get("/index.html").build()), hasStatus(OK)); assertThat(handle(get("/foo.js").build()), hasStatus(OK)); assertThat(handle(get("/notfound.html").build()), hasStatus(NOT_FOUND)); assertThat(handle(get("/a/b/good").build()), hasStatus(OK)); assertThat(handle(get("/a/b/bad").build()), hasStatus(NOT_FOUND)); }
@Test public void shouldIgnoreQueryParams() throws Exception { writeFile("index.html", "Hello world"); writeFile("foo.js", "Blah"); mkdir("/a/b"); writeFile("a/b/good", "hi"); assertThat(handle(get("/index.html?foo=x").build()), hasStatus(OK)); assertThat(handle(get("/foo.js?sdfsd").build()), hasStatus(OK)); assertThat(handle(get("/a/b/good?xx").build()), hasStatus(OK)); }
@Test public void shouldDisallowDirectoryTraversals() throws Exception { mkdir("/a/public"); writeFile("a/public/index.html", "hi"); mkdir("/a/private"); writeFile("a/private/index.html", "hi"); handler = new StaticFileHandler(new File(dir, "/a/public")); assertThat(handle(get("/index.html").build()), hasStatus(OK)); assertThat(handle(get("/../private/index.html").build()), hasStatus(NOT_FOUND)); }
@Test public void shouldDisallowDirectoryTraversalsWithEncodedRequests() throws Exception { mkdir("/a/public"); writeFile("a/public/index.html", "hi"); mkdir("/a/private"); writeFile("a/private/index.html", "hi"); handler = new StaticFileHandler(new File(dir, "/a/public")); assertThat(handle(get("/index.html").build()), hasStatus(OK)); assertThat(handle(get("/%2e%2e%2fprivate/index.html").build()), hasStatus(NOT_FOUND)); }