Skip to content

Commit 1cd74a5

Browse files
committed
Merge pull request #424 from Filip Hrisafov
* gh-424: Polish "Add support for configuring default request and response preprocessors" Add support for configuring default request and response preprocessors
2 parents 22cf08a + eed90c0 commit 1cd74a5

File tree

19 files changed

+730
-62
lines changed

19 files changed

+730
-62
lines changed

docs/src/docs/asciidoc/configuration.adoc

+24
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,27 @@ include::{examples-dir}/com/example/mockmvc/CustomDefaultSnippets.java[tags=cust
114114
----
115115
include::{examples-dir}/com/example/restassured/CustomDefaultSnippets.java[tags=custom-default-snippets]
116116
----
117+
118+
[[configuration-default-preprocessors]]
119+
=== Default operation preprocessors
120+
121+
You can configure default request and response preprocessors during setup using the
122+
`RestDocumentationConfigurer` API. For example, to remove the `Foo` headers from all requests
123+
and pretty print all responses:
124+
125+
[source,java,indent=0,role="primary"]
126+
.MockMvc
127+
----
128+
include::{examples-dir}/com/example/mockmvc/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors]
129+
----
130+
<1> Apply a request preprocessor that will remove the header named `Foo`.
131+
<2> Apply a response preprocessor that will pretty print its content.
132+
133+
[source,java,indent=0,role="secondary"]
134+
.REST Assured
135+
----
136+
include::{examples-dir}/com/example/restassured/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors]
137+
----
138+
<1> Apply a request preprocessor that will remove the header named `Foo`.
139+
<2> Apply a response preprocessor that will pretty print its content.
140+

docs/src/docs/asciidoc/customizing-requests-and-responses.adoc

+7-16
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,25 @@ include::{examples-dir}/com/example/restassured/PerTestPreprocessing.java[tags=p
2727
<2> Apply a response preprocessor that will pretty print its content.
2828

2929
Alternatively, you may want to apply the same preprocessors to every test. You can do
30-
so by configuring the preprocessors in your `@Before` method and using the
31-
<<documentating-your-api-parameterized-output-directories, support for parameterized
32-
output directories>>:
30+
so by configuring the preprocessors using the `RestDocumentationConfigurer` API in your
31+
`@Before` method. For example to remove the `Foo` header from all requests and pretty print
32+
all responses:
3333

3434
[source,java,indent=0,role="primary"]
3535
.MockMvc
3636
----
3737
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=setup]
3838
----
39-
<1> Create a `RestDocumentationResultHandler`, configured to preprocess the request
40-
and response.
41-
<2> Create a `MockMvc` instance, configured to always call the documentation result
42-
handler.
39+
<1> Apply a request preprocessor that will remove the header named `Foo`.
40+
<2> Apply a response preprocessor that will pretty print its content.
4341

4442
[source,java,indent=0,role="secondary"]
4543
.REST Assured
4644
----
4745
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=setup]
4846
----
49-
<1> Create a `RestDocumentationFilter`, configured to preprocess the request
50-
and response.
51-
<2> Create a `RequestSpecification` instance, configured to always call the documentation
52-
filter.
47+
<1> Apply a request preprocessor that will remove the header named `Foo`.
48+
<2> Apply a response preprocessor that will pretty print its content.
5349

5450
Then, in each test, any configuration specific to that test can be performed. For example:
5551

@@ -58,17 +54,12 @@ Then, in each test, any configuration specific to that test can be performed. Fo
5854
----
5955
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=use]
6056
----
61-
<1> The request and response will be preprocessed due to the use of `alwaysDo` above.
62-
<2> Document the links specific to the resource that is being tested
6357

6458
[source,java,indent=0,role="secondary"]
6559
.REST Assured
6660
----
6761
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=use]
6862
----
69-
<1> The request and response will be preprocessed due to the configuration of the
70-
`RequestSpecification` in the `setUp` method.
71-
<2> Document the links specific to the resource that is being tested
7263

7364
Various built in preprocessors, including those illustrated above, are available via the
7465
static methods on `Preprocessors`. See <<Preprocessors, below>> for further details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.mockmvc;
18+
19+
import org.junit.Before;
20+
import org.junit.Rule;
21+
22+
import org.springframework.restdocs.JUnitRestDocumentation;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
25+
import org.springframework.web.context.WebApplicationContext;
26+
27+
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
28+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
29+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders;
30+
31+
public class CustomDefaultOperationPreprocessors {
32+
33+
@Rule
34+
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
35+
36+
private WebApplicationContext context;
37+
38+
@SuppressWarnings("unused")
39+
private MockMvc mockMvc;
40+
41+
@Before
42+
public void setup() {
43+
// tag::custom-default-operation-preprocessors[]
44+
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
45+
.apply(documentationConfiguration(this.restDocumentation)
46+
.operationPreprocessors()
47+
.withRequestDefaults(removeHeaders("Foo")) // <1>
48+
.withResponseDefaults(prettyPrint())) // <2>
49+
.build();
50+
// end::custom-default-operation-preprocessors[]
51+
}
52+
53+
}

docs/src/test/java/com/example/mockmvc/EveryTestPreprocessing.java

+6-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.junit.Before;
2020
import org.junit.Rule;
2121
import org.springframework.restdocs.JUnitRestDocumentation;
22-
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
2322
import org.springframework.test.web.servlet.MockMvc;
2423
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
2524
import org.springframework.web.context.WebApplicationContext;
@@ -29,8 +28,6 @@
2928
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
3029
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
3130
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
32-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
33-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
3431
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
3532
import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders;
3633
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -45,26 +42,21 @@ public class EveryTestPreprocessing {
4542
// tag::setup[]
4643
private MockMvc mockMvc;
4744

48-
private RestDocumentationResultHandler documentationHandler;
49-
5045
@Before
5146
public void setup() {
52-
this.documentationHandler = document("{method-name}", // <1>
53-
preprocessRequest(removeHeaders("Foo")),
54-
preprocessResponse(prettyPrint()));
5547
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
56-
.apply(documentationConfiguration(this.restDocumentation))
57-
.alwaysDo(this.documentationHandler) // <2>
58-
.build();
48+
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
49+
.withRequestDefaults(removeHeaders("Foo")) // <1>
50+
.withResponseDefaults(prettyPrint())) // <2>
51+
.build();
5952
}
60-
6153
// end::setup[]
6254

6355
public void use() throws Exception {
6456
// tag::use[]
65-
this.mockMvc.perform(get("/")) // <1>
57+
this.mockMvc.perform(get("/"))
6658
.andExpect(status().isOk())
67-
.andDo(this.documentationHandler.document( // <2>
59+
.andDo(document("index",
6860
links(linkWithRel("self").description("Canonical self link"))
6961
));
7062
// end::use[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.restassured;
18+
19+
import io.restassured.builder.RequestSpecBuilder;
20+
import io.restassured.specification.RequestSpecification;
21+
import org.junit.Before;
22+
import org.junit.Rule;
23+
24+
import org.springframework.restdocs.JUnitRestDocumentation;
25+
26+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
27+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders;
28+
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.documentationConfiguration;
29+
30+
public class CustomDefaultOperationPreprocessors {
31+
32+
@Rule
33+
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
34+
35+
@SuppressWarnings("unused")
36+
private RequestSpecification spec;
37+
38+
@Before
39+
public void setup() {
40+
// tag::custom-default-operation-preprocessors[]
41+
this.spec = new RequestSpecBuilder()
42+
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
43+
.withRequestDefaults(removeHeaders("Foo")) // <1>
44+
.withResponseDefaults(prettyPrint())) // <2>
45+
.build();
46+
// end::custom-default-operation-preprocessors[]
47+
}
48+
49+
}

docs/src/test/java/com/example/restassured/EveryTestPreprocessing.java

+7-15
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
import org.junit.Rule;
2424

2525
import org.springframework.restdocs.JUnitRestDocumentation;
26-
import org.springframework.restdocs.restassured3.RestDocumentationFilter;
2726

2827
import static org.hamcrest.CoreMatchers.is;
2928
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.linkWithRel;
3029
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
31-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
32-
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
3330
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
3431
import static org.springframework.restdocs.operation.preprocess.Preprocessors.removeHeaders;
3532
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
@@ -43,26 +40,21 @@ public class EveryTestPreprocessing {
4340
// tag::setup[]
4441
private RequestSpecification spec;
4542

46-
private RestDocumentationFilter documentationFilter;
47-
4843
@Before
4944
public void setup() {
50-
this.documentationFilter = document("{method-name}",
51-
preprocessRequest(removeHeaders("Foo")),
52-
preprocessResponse(prettyPrint())); // <1>
5345
this.spec = new RequestSpecBuilder()
54-
.addFilter(documentationConfiguration(this.restDocumentation))
55-
.addFilter(this.documentationFilter)// <2>
56-
.build();
46+
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
47+
.withRequestDefaults(removeHeaders("Foo")) // <1>
48+
.withResponseDefaults(prettyPrint())) // <2>
49+
.build();
5750
}
58-
5951
// end::setup[]
6052

6153
public void use() throws Exception {
6254
// tag::use[]
63-
RestAssured.given(this.spec) // <1>
64-
.filter(this.documentationFilter.document( // <2>
65-
links(linkWithRel("self").description("Canonical self link"))))
55+
RestAssured.given(this.spec)
56+
.filter(document("index",
57+
links(linkWithRel("self").description("Canonical self link"))))
6658
.when().get("/")
6759
.then().assertThat().statusCode(is(200));
6860
// end::use[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2014-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.restdocs.config;
18+
19+
import java.util.Map;
20+
21+
import org.springframework.restdocs.RestDocumentationContext;
22+
import org.springframework.restdocs.generate.RestDocumentationGenerator;
23+
import org.springframework.restdocs.operation.preprocess.OperationPreprocessor;
24+
import org.springframework.restdocs.operation.preprocess.OperationRequestPreprocessor;
25+
import org.springframework.restdocs.operation.preprocess.OperationResponsePreprocessor;
26+
import org.springframework.restdocs.operation.preprocess.Preprocessors;
27+
28+
/**
29+
* A configurer that can be used to configure the default operation preprocessors.
30+
*
31+
* @param <PARENT> The type of the configurer's parent
32+
* @param <TYPE> The concrete type of the configurer to be returned from chained methods
33+
* @author Filip Hrisafov
34+
* @author Andy Wilkinson
35+
* @since 2.0.0
36+
*/
37+
public abstract class OperationPreprocessorsConfigurer<PARENT, TYPE>
38+
extends AbstractNestedConfigurer<PARENT> {
39+
40+
private OperationRequestPreprocessor defaultOperationRequestPreprocessor;
41+
42+
private OperationResponsePreprocessor defaultOperationResponsePreprocessor;
43+
44+
/**
45+
* Creates a new {@code OperationPreprocessorConfigurer} with the given
46+
* {@code parent}.
47+
*
48+
* @param parent the parent
49+
*/
50+
protected OperationPreprocessorsConfigurer(PARENT parent) {
51+
super(parent);
52+
}
53+
54+
@Override
55+
public void apply(Map<String, Object> configuration,
56+
RestDocumentationContext context) {
57+
configuration.put(
58+
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
59+
this.defaultOperationRequestPreprocessor);
60+
configuration.put(
61+
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
62+
this.defaultOperationResponsePreprocessor);
63+
}
64+
65+
/**
66+
* Configures the default operation request preprocessors.
67+
*
68+
* @param preprocessors the preprocessors
69+
* @return {@code this}
70+
*/
71+
@SuppressWarnings("unchecked")
72+
public TYPE withRequestDefaults(OperationPreprocessor... preprocessors) {
73+
this.defaultOperationRequestPreprocessor = Preprocessors
74+
.preprocessRequest(preprocessors);
75+
return (TYPE) this;
76+
}
77+
78+
/**
79+
* Configures the default operation response preprocessors.
80+
*
81+
* @param preprocessors the preprocessors
82+
* @return {@code this}
83+
*/
84+
@SuppressWarnings("unchecked")
85+
public TYPE withResponseDefaults(OperationPreprocessor... preprocessors) {
86+
this.defaultOperationResponsePreprocessor = Preprocessors
87+
.preprocessResponse(preprocessors);
88+
return (TYPE) this;
89+
}
90+
91+
}

0 commit comments

Comments
 (0)