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 public OperationResponse preprocess(OperationResponse response) { return this.contentModifyingDelegate .preprocess(new OperationResponseFactory().create(response.getStatus(), modify(response.getHeaders()), response.getContent())); }
@Override protected Map<String, Object> createModel(Operation operation) { try { Object object = this.objectMapper.readValue( operation.getResponse().getContentAsString(), Object.class); Object field = this.fieldProcessor.extract(JsonFieldPath.compile(this.path), object); if (field instanceof List && this.index != null) { field = ((List<?>) field).get(this.index); } return Collections.singletonMap("value", this.objectMapper.writeValueAsString(field)); } catch (Exception ex) { throw new IllegalStateException(ex); } }
/** * Creates a new {@code OperationResponse} based on the given {@code original} but * with the given {@code newHeaders}. * @param original the original response * @param newHeaders the new headers * @return the new response with the new headers */ public OperationResponse createFrom(OperationResponse original, HttpHeaders newHeaders) { return new StandardOperationResponse(original.getStatus(), newHeaders, original.getContent()); }
/** * 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 protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
@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); }
@Override public OperationResponse preprocess(OperationResponse response) { byte[] modifiedContent = this.contentModifier.modifyContent(response.getContent(), response.getHeaders().getContentType()); return this.responseFactory.createFrom(response, modifiedContent); }
@Override protected Map<String, Object> createModel(Operation operation) { OperationResponse response = operation.getResponse(); HttpStatus status = response.getStatus(); Map<String, Object> model = new HashMap<>(); model.put("responseBody", responseBody(response)); model.put("statusCode", status.value()); model.put("statusReason", status.getReasonPhrase()); model.put("headers", headers(response)); return model; }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
@Test public void basicResponse() { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(HttpServletResponse.SC_OK); OperationResponse operationResponse = this.factory.convert(response); assertThat(operationResponse.getStatus()).isEqualTo(HttpStatus.OK); }
@Override protected Set<String> extractActualHeaders(Operation operation) { return operation.getResponse().getHeaders().keySet(); }
protected abstract Map<String, List<Link>> extractLinks(Map<String, Object> json);
private String responseBody(OperationResponse response) { String content = response.getContentAsString(); return content.isEmpty() ? content : String.format("%n%s", content); }
@Override public OperationResponse preprocess(OperationResponse response) { return this.responseFactory.createFrom(response, removeHeaders(response.getHeaders())); }
@Test public void modifyResponseContent() { OperationResponse response = this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), "content".getBytes()); OperationResponse preprocessed = this.preprocessor.preprocess(response); assertThat(preprocessed.getContent()).isEqualTo("modified".getBytes()); }
@Override protected MediaType getContentType(Operation operation) { return operation.getResponse().getHeaders().getContentType(); }
@Test public void responseContentUriHostCanBeModified() { this.preprocessor.host("api.example.com"); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://api.example.com:12345' should be used"); }
@Override protected MediaType getContentType(Operation operation) { return operation.getResponse().getHeaders().getContentType(); }
@Test public void responseContentUriQueryIsPreserved() { this.preprocessor.removePort(); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345?foo=bar' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://localhost?foo=bar' should be used"); }