private List<Map<String, String>> headers(OperationResponse response) { List<Map<String, String>> headers = new ArrayList<>(); for (Entry<String, List<String>> header : response.getHeaders().entrySet()) { List<String> values = header.getValue(); for (String value : values) { headers.add(header(header.getKey(), value)); } } return headers; }
@Override protected Set<String> extractActualHeaders(Operation operation) { return operation.getResponse().getHeaders().keySet(); }
@Override public OperationResponse preprocess(OperationResponse response) { return this.responseFactory.createFrom(response, removeHeaders(response.getHeaders())); }
@Override protected MediaType getContentType(Operation operation) { return operation.getResponse().getHeaders().getContentType(); }
@Override protected MediaType getContentType(Operation operation) { return operation.getResponse().getHeaders().getContentType(); }
@Override public Map<String, List<Link>> extractLinks(OperationResponse response) throws IOException { MediaType contentType = response.getHeaders().getContentType(); LinkExtractor extractorForContentType = getExtractorForContentType(contentType); if (extractorForContentType != null) { return extractorForContentType.extractLinks(response); } throw new IllegalStateException( "No LinkExtractor has been provided and one is not available for the " + "content type " + contentType); }
/** * Creates a new {@code OperationResponse} based on the given {@code original} but * with the given {@code newContent}. If the original response had a * {@code Content-Length} header it will be modified to match the length of the new * content. * @param original the original response * @param newContent the new content * @return the new response with the new content */ public OperationResponse createFrom(OperationResponse original, byte[] newContent) { return new StandardOperationResponse(original.getStatus(), getUpdatedHeaders(original.getHeaders(), newContent), newContent); }
@Override public OperationResponse preprocess(OperationResponse response) { byte[] modifiedContent = this.contentModifier.modifyContent(response.getContent(), response.getHeaders().getContentType()); return this.responseFactory.createFrom(response, modifiedContent); }
@Test public void modifyResponseHeaders() { OperationResponse response = createResponse(); OperationResponse preprocessed = this.preprocessor.preprocess(response); assertThat(preprocessed.getHeaders().size()).isEqualTo(1); assertThat(preprocessed.getHeaders()).containsEntry("a", Arrays.asList("alpha")); }
@Test public void modifyWithPattern() { OperationResponse response = createResponse("content-length", "1234"); HeaderRemovingOperationPreprocessor processor = new HeaderRemovingOperationPreprocessor( new PatternMatchHeaderFilter("co.*le(.)gth]")); OperationResponse preprocessed = processor.preprocess(response); assertThat(preprocessed.getHeaders().size()).isEqualTo(2); assertThat(preprocessed.getHeaders()).containsEntry("a", Arrays.asList("alpha")); assertThat(preprocessed.getHeaders()).containsEntry("b", Arrays.asList("bravo", "banana")); }
@Test public void responseWithCookie() { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(HttpServletResponse.SC_OK); Cookie cookie = new Cookie("name", "value"); cookie.setDomain("localhost"); cookie.setHttpOnly(true); response.addCookie(cookie); OperationResponse operationResponse = this.factory.convert(response); assertThat(operationResponse.getHeaders()).hasSize(1); assertThat(operationResponse.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE, Collections.singletonList("name=value; Domain=localhost; HttpOnly")); }
@Override public OperationResponse preprocess(OperationResponse response) { return this.contentModifyingDelegate .preprocess(new OperationResponseFactory().create(response.getStatus(), modify(response.getHeaders()), response.getContent())); }
@Test public void urisInResponseHeadersCanBeModified() { OperationResponse processed = this.preprocessor.host("api.example.com") .preprocess(createResponseWithHeader("Foo", "http://locahost:12345")); assertThat(processed.getHeaders().getFirst("Foo")) .isEqualTo("http://api.example.com:12345"); }
@Test public void urisInResponseHeadersCanBeModified() { OperationResponse processed = this.preprocessor.host("api.example.com") .preprocess(createResponseWithHeader("Foo", "http://locahost:12345")); assertThat(processed.getHeaders().getFirst("Foo")) .isEqualTo("http://api.example.com:12345"); }
@Test public void removeAllHeaders() { HeaderRemovingOperationPreprocessor processor = new HeaderRemovingOperationPreprocessor( new PatternMatchHeaderFilter(".*")); OperationResponse preprocessed = processor.preprocess(createResponse()); assertThat(preprocessed.getHeaders().size()).isEqualTo(0); }
@Test public void basicResponse() { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.ok().syncBody("Hello, World!"))) .configureClient().baseUrl("http://localhost").build().get().uri("/foo") .exchange().expectBody().returnResult(); OperationResponse response = this.converter.convert(result); assertThat(response.getStatus()).isEqualTo(HttpStatus.OK); assertThat(response.getContentAsString()).isEqualTo("Hello, World!"); assertThat(response.getHeaders().getContentType()) .isEqualTo(MediaType.parseMediaType("text/plain;charset=UTF-8")); assertThat(response.getHeaders().getContentLength()).isEqualTo(13); }
@Test public void customDefaultOperationResponsePreprocessor() { Map<String, Object> configuration = new HashMap<>(); this.configurer.operationPreprocessors() .withResponseDefaults(Preprocessors.prettyPrint(), Preprocessors.removeHeaders("Foo")) .apply(configuration, createContext()); OperationResponsePreprocessor preprocessor = (OperationResponsePreprocessor) configuration .get(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR); HttpHeaders headers = new HttpHeaders(); headers.add("Foo", "value"); OperationResponse response = new OperationResponseFactory().create(HttpStatus.OK, headers, null); assertThat(preprocessor.preprocess(response).getHeaders()) .doesNotContainKey("Foo"); }
@Test public void responseWithCookie() { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> ServerResponse.ok() .cookie(ResponseCookie.from("name", "value") .domain("localhost").httpOnly(true).build()) .build())) .configureClient().baseUrl("http://localhost").build().get().uri("/foo") .exchange().expectBody().returnResult(); OperationResponse response = this.converter.convert(result); assertThat(response.getHeaders()).hasSize(1); assertThat(response.getHeaders()).containsEntry(HttpHeaders.SET_COOKIE, Collections.singletonList("name=value; Domain=localhost; HttpOnly")); }