Add support for configuring default request and response preprocessors
See gh-424
This commit is contained in:
committed by
Andy Wilkinson
parent
22cf08a9ad
commit
4f8b173836
@@ -28,6 +28,9 @@ import org.mockito.ArgumentCaptor;
|
||||
|
||||
import org.springframework.restdocs.JUnitRestDocumentation;
|
||||
import org.springframework.restdocs.generate.RestDocumentationGenerator;
|
||||
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;
|
||||
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor;
|
||||
import org.springframework.restdocs.operation.preprocess.Preprocessors;
|
||||
import org.springframework.restdocs.snippet.WriterResolver;
|
||||
import org.springframework.restdocs.templates.TemplateEngine;
|
||||
|
||||
@@ -43,6 +46,7 @@ import static org.mockito.Mockito.verify;
|
||||
* Tests for {@link RestAssuredRestDocumentationConfigurer}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class RestAssuredRestDocumentationConfigurerTests {
|
||||
|
||||
@@ -68,7 +72,11 @@ public class RestAssuredRestDocumentationConfigurerTests {
|
||||
|
||||
@Test
|
||||
public void configurationIsAddedToTheContext() {
|
||||
this.configurer.filter(this.requestSpec, this.responseSpec, this.filterContext);
|
||||
this.configurer
|
||||
.operationPreprocessors()
|
||||
.withDefaultRequestPreprocessors(Preprocessors.prettyPrint())
|
||||
.withDefaultResponsePreprocessors(Preprocessors.removeHeaders("Foo"))
|
||||
.filter(this.requestSpec, this.responseSpec, this.filterContext);
|
||||
@SuppressWarnings("rawtypes")
|
||||
ArgumentCaptor<Map> configurationCaptor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(this.filterContext).setValue(
|
||||
@@ -84,6 +92,14 @@ public class RestAssuredRestDocumentationConfigurerTests {
|
||||
hasEntry(
|
||||
equalTo(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_SNIPPETS),
|
||||
instanceOf(List.class)));
|
||||
assertThat(configuration,
|
||||
hasEntry(
|
||||
equalTo(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR),
|
||||
instanceOf(OperationRequestPreprocessor.class)));
|
||||
assertThat(configuration,
|
||||
hasEntry(
|
||||
equalTo(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR),
|
||||
instanceOf(OperationResponsePreprocessor.class)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ import static org.springframework.restdocs.test.SnippetMatchers.snippet;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Tomasz Kopczynski
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class RestAssuredRestDocumentationIntegrationTests {
|
||||
|
||||
@@ -327,6 +328,31 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
.header("Host", "localhost").content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPreprocessedRequest() throws Exception {
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation)
|
||||
.operationPreprocessors().withDefaultRequestPreprocessors(prettyPrint(),
|
||||
replacePattern(pattern, "\"<<beta>>\""),
|
||||
modifyUris().removePort(),
|
||||
removeHeaders("a", HttpHeaders.CONTENT_LENGTH)))
|
||||
.header("a", "alpha").header("b", "bravo").contentType("application/json")
|
||||
.accept("application/json").body("{\"a\":\"alpha\"}")
|
||||
.filter(document("default-preprocessed-request"))
|
||||
.get("/").then().statusCode(200);
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\"%n}");
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/default-preprocessed-request/http-request.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(httpRequest(asciidoctor(), RequestMethod.GET, "/")
|
||||
.header("b", "bravo")
|
||||
.header("Accept", MediaType.APPLICATION_JSON_VALUE)
|
||||
.header("Content-Type", "application/json; charset=UTF-8")
|
||||
.header("Host", "localhost").content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preprocessedResponse() throws Exception {
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
@@ -353,6 +379,31 @@ public class RestAssuredRestDocumentationIntegrationTests {
|
||||
.content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPreprocessedResponse() throws Exception {
|
||||
Pattern pattern = Pattern.compile("(\"alpha\")");
|
||||
given().port(tomcat.getPort())
|
||||
.filter(documentationConfiguration(this.restDocumentation)
|
||||
.operationPreprocessors().withDefaultResponsePreprocessors(prettyPrint(), maskLinks(),
|
||||
removeHeaders("a", "Transfer-Encoding", "Date", "Server"),
|
||||
replacePattern(pattern, "\"<<beta>>\""), modifyUris()
|
||||
.scheme("https").host("api.example.com").removePort()))
|
||||
.filter(document("default-preprocessed-response"))
|
||||
.get("/").then().statusCode(200);
|
||||
String prettyPrinted = String.format("{%n \"a\" : \"<<beta>>\",%n \"links\" : "
|
||||
+ "[ {%n \"rel\" : \"rel\",%n \"href\" : \"...\"%n } ]%n}");
|
||||
assertThat(
|
||||
new File(
|
||||
"build/generated-snippets/default-preprocessed-response/http-response.adoc"),
|
||||
is(snippet(asciidoctor())
|
||||
.withContents(httpResponse(asciidoctor(), HttpStatus.OK)
|
||||
.header("Foo", "https://api.example.com/foo/bar")
|
||||
.header("Content-Type", "application/json;charset=UTF-8")
|
||||
.header(HttpHeaders.CONTENT_LENGTH,
|
||||
prettyPrinted.getBytes().length)
|
||||
.content(prettyPrinted))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customSnippetTemplate() throws Exception {
|
||||
ClassLoader classLoader = new URLClassLoader(new URL[] {
|
||||
|
||||
Reference in New Issue
Block a user