ResourceMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { @Override public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) { HttpResponse response = resourceRequest(request, rootPath); if (response == null) { response = castToHttpResponse(chain.next(request)); } return response; }  @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain); void setRootPath(String rootPath); void setUriPrefix(String uriPrefix);  }
@Test void resourceIsFound() throws URISyntaxException { ResourceMiddleware<HttpResponse> middleware = new ResourceMiddleware<>(); HttpRequest request = new DefaultHttpRequest(); request.setRequestMethod("GET"); request.setUri("/assets/test.txt"); request.setParams(Parameters.of("A", " B ", "C", 1)); MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(Predicates.any(), "endpoint", (req, c) -> builder(HttpResponse.of("dummy")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/plain")) .build()); HttpResponse response = middleware.handle(request, chain); assertThat(response.getBody()) .isEqualTo(new File(ClassLoader.getSystemResource("public/test.txt").toURI())); }
@Test void resourceAssetPath() throws URISyntaxException { ResourceMiddleware<HttpResponse> middleware = builder(new ResourceMiddleware<HttpResponse>()) .set(ResourceMiddleware::setUriPrefix, (String) null) .build(); HttpRequest request = new DefaultHttpRequest(); request.setRequestMethod("GET"); request.setUri("/test.txt"); request.setParams(Parameters.of("A", " B ", "C", 1)); MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(Predicates.any(), "endpoint", (req, c) -> builder(HttpResponse.of("dummy")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/plain")) .build()); HttpResponse response = middleware.handle(request, chain); assertThat(response.getBody()) .isEqualTo(new File(ClassLoader.getSystemResource("public/test.txt").toURI())); }