@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
/** * 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()); }
@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 responseContentUriSchemeCanBeModified() { this.preprocessor.scheme("https"); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'https://localhost:12345' should be used"); }
@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()); }
@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"); }
@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"); }
@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"); }
@Test public void multipleResponseContentUrisCanBeModified() { this.preprocessor.removePort(); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "Use 'http://localhost:12345' or 'https://localhost:23456' to access the service")); assertThat(new String(processed.getContent())).isEqualTo( "Use 'http://localhost' or 'https://localhost' to access the service"); }
@Test public void responseContentUriAnchorIsPreserved() { this.preprocessor.removePort(); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345#foo' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://localhost#foo' should be used"); }
@Test public void responseContentUriPathIsPreserved() { 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"); }
@Test public void responseContentUriSchemeCanBeModified() { this.preprocessor.scheme("https"); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'https://localhost:12345' should be used"); }
@Test public void responseContentUriPortCanBeModified() { this.preprocessor.port(23456); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://localhost:23456' should be used"); }
@Test public void responseContentUriPortCanBeRemoved() { this.preprocessor.removePort(); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://localhost' should be used"); }
@Test public void responseContentUriPathIsPreserved() { 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"); }
@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"); }
@Test public void responseContentUriPortCanBeModified() { this.preprocessor.port(23456); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://localhost:23456' should be used"); }
@Test public void multipleResponseContentUrisCanBeModified() { this.preprocessor.removePort(); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "Use 'http://localhost:12345' or 'https://localhost:23456' to access the service")); assertThat(new String(processed.getContent())).isEqualTo( "Use 'http://localhost' or 'https://localhost' to access the service"); }
@Test public void responseContentUriAnchorIsPreserved() { this.preprocessor.removePort(); OperationResponse processed = this.preprocessor .preprocess(createResponseWithContent( "The uri 'http://localhost:12345#foo' should be used")); assertThat(new String(processed.getContent())) .isEqualTo("The uri 'http://localhost#foo' should be used"); }
@Override public OperationResponse preprocess(OperationResponse response) { return this.contentModifyingDelegate .preprocess(new OperationResponseFactory().create(response.getStatus(), modify(response.getHeaders()), response.getContent())); }