StaticContentResource { @GET @Path("{path:.*}") public Response get(@PathParam("path") String path) { java.nio.file.Path resourcePath = contentDirectory.resolve(path); return ifExists(resourcePath) .map(this::getContents) .orElseGet(() -> Response.status(Response.Status.NOT_FOUND)) .build(); } protected  StaticContentResource(StaticContent content); protected  StaticContentResource(java.nio.file.Path contentDirectory, String indexPage); @GET Response getInfo(@Context UriInfo uriInfo); @GET @Path("{path:.*}") Response get(@PathParam("path") String path);  }
@Test @DisplayName("Redirects root context to the index page (without trailing slash)") void testRedirectRootWithoutSlash() { assertThat(get(CONTEXT_URL)) .redirectsTo(INDEX_URL); }
@Test @DisplayName("Redirects root context to the index page (with trailing slash)") void testRedirectRootWithSlash() { assertThat(get(CONTEXT_URL + "/")) .redirectsTo(INDEX_URL); }
@Test @DisplayName("Serves files from a local folder") void testServesFiles() { assertThat(get(IMAGE_URL)) .isSuccess() .hasContentType("image/jpeg") .andResponse().hasSameContentAs(getImage()); }
@Test @DisplayName("Does not serve files outside the local folder") void testServesNoFilesOutsideFolder() { assertThat(get(CONTEXT_URL + "/../" + INDEX_PAGE)) .isNotFound(); }
@Test @DisplayName("Reports non-existing files as not found") void testNotFoundNonExistingFiles() { assertThat(get(INDEX_URL)) .isNotFound(); }
@Test @DisplayName("Reports directories as not found") void testNotFoundDirectories() { assertThat(get(IMAGES_URL)) .isNotFound(); }
@Test @DisplayName("Reports non-readable files as not found") void testNotFoundNonReadableFiles() { Assumptions.assumeTrue(supportsUnreadableFiles, "OS does not support unreadable files"); assertThat(get(NON_READABLE_URL)) .isNotFound(); }
@Test @DisplayName("Reports blank server error in case of exceptions") void testServerError() { when(getFileContents.apply(any())).thenThrow(new UnsupportedOperationException()); assertThat(get(IMAGE_URL)) .hasStatus(Response.Status.INTERNAL_SERVER_ERROR) .andText().isEmpty(); }