ContentTypeMiddleware extends AbstractWebMiddleware<HttpRequest, NRES> { @Override public HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain) { HttpResponse response = castToHttpResponse(chain.next(request)); if (response != null) { contentTypeResponse(response, request); } return response; }  @Override HttpResponse handle(HttpRequest request, MiddlewareChain<HttpRequest, NRES, ?, ?> chain);  }
@Test void testSetContentType() { MiddlewareChain<HttpRequest, HttpResponse, HttpRequest, HttpResponse> chain = new DefaultMiddlewareChain<>(new AnyPredicate<>(), null, (Endpoint<HttpRequest, HttpResponse>) req -> builder(HttpResponse.of("hello")) .set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html")) .build()); HttpResponse response = middleware.handle(request, chain); final Object header = HttpResponseUtils.getHeader(response, "content-type"); assertThat(header) .asString() .isEqualTo("text/html"); }
@Test void defaultValueIsOctetStream() { MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(new AnyPredicate<>(), null, (Endpoint<HttpRequest, HttpResponse>) req -> builder(HttpResponse.of("hello")) .build()); HttpResponse response = middleware.handle(request, chain); final Object header = HttpResponseUtils.getHeader(response, "content-type"); assertThat(header).isEqualTo("text/plain"); }
@Test void testRedirect() { MiddlewareChain<HttpRequest, HttpResponse, ?, ?> chain = new DefaultMiddlewareChain<>(new AnyPredicate<>(), null, (Endpoint<HttpRequest, HttpResponse>) req -> builder(HttpResponse.of("hello")) .set(HttpResponse::setStatus, 303) .set(HttpResponse::setHeaders, Headers.of("Location", "foo/bar")) .build()); HttpResponse response = middleware.handle(request, chain); assertThat((String)HttpResponseUtils.getHeader(response, "content-type")) .as("This is a specification of ring content_type middleware") .isEqualTo("text/plain"); }