Skip to content

Commit eed90c0

Browse files
committed
Polish "Add support for configuring default request and response preprocessors"
Closes gh-424
1 parent 4f8b173 commit eed90c0

File tree

19 files changed

+219
-198
lines changed

19 files changed

+219
-198
lines changed

docs/src/docs/asciidoc/configuration.adoc

+8-4
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,25 @@ include::{examples-dir}/com/example/restassured/CustomDefaultSnippets.java[tags=
116116
----
117117

118118
[[configuration-default-preprocessors]]
119-
=== Operation preprocessors
119+
=== Default operation preprocessors
120120

121-
You can configure default Request / Response preprocessors during setup using the
121+
You can configure default request and response preprocessors during setup using the
122122
`RestDocumentationConfigurer` API. For example, to remove the `Foo` headers from all requests
123123
and pretty print all responses:
124124

125125
[source,java,indent=0,role="primary"]
126126
.MockMvc
127127
----
128-
include::{examples-dir}/com/example/mockmvc/CustomDefaultOperationPreprocessors.java[tags=custom-default-preprocessors]
128+
include::{examples-dir}/com/example/mockmvc/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors]
129129
----
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.
130132

131133
[source,java,indent=0,role="secondary"]
132134
.REST Assured
133135
----
134-
include::{examples-dir}/com/example/restassured/CustomDefaultOperationPreprocessors.java[tags=custom-default-preprocessors]
136+
include::{examples-dir}/com/example/restassured/CustomDefaultOperationPreprocessors.java[tags=custom-default-operation-preprocessors]
135137
----
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.
136140

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,20 +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 using the `RestDocumentationConfigurer` API.
31-
For example to remove the `Foo` header from all requests and pretty print all responses:
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:
3233

3334
[source,java,indent=0,role="primary"]
3435
.MockMvc
3536
----
3637
include::{examples-dir}/com/example/mockmvc/EveryTestPreprocessing.java[tags=setup]
3738
----
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.
3841

3942
[source,java,indent=0,role="secondary"]
4043
.REST Assured
4144
----
4245
include::{examples-dir}/com/example/restassured/EveryTestPreprocessing.java[tags=setup]
4346
----
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.
4449

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

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ public class CustomDefaultOperationPreprocessors {
3535

3636
private WebApplicationContext context;
3737

38+
@SuppressWarnings("unused")
3839
private MockMvc mockMvc;
3940

4041
@Before
4142
public void setup() {
42-
// tag::custom-default-preprocessors[]
43+
// tag::custom-default-operation-preprocessors[]
4344
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
44-
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
45-
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
46-
.withDefaultResponsePreprocessors(prettyPrint()))
45+
.apply(documentationConfiguration(this.restDocumentation)
46+
.operationPreprocessors()
47+
.withRequestDefaults(removeHeaders("Foo")) // <1>
48+
.withResponseDefaults(prettyPrint())) // <2>
4749
.build();
48-
// end::custom-default-preprocessors[]
50+
// end::custom-default-operation-preprocessors[]
4951
}
52+
5053
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,18 @@ public class EveryTestPreprocessing {
4545
@Before
4646
public void setup() {
4747
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
48-
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
49-
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
50-
.withDefaultResponsePreprocessors(prettyPrint()))
51-
.build();
48+
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors()
49+
.withRequestDefaults(removeHeaders("Foo")) // <1>
50+
.withResponseDefaults(prettyPrint())) // <2>
51+
.build();
5252
}
53-
5453
// end::setup[]
5554

5655
public void use() throws Exception {
5756
// tag::use[]
58-
this.mockMvc.perform(get("/")) // <1>
57+
this.mockMvc.perform(get("/"))
5958
.andExpect(status().isOk())
60-
.andDo(document("{method-name}",
59+
.andDo(document("index",
6160
links(linkWithRel("self").description("Canonical self link"))
6261
));
6362
// end::use[]

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import io.restassured.builder.RequestSpecBuilder;
2020
import io.restassured.specification.RequestSpecification;
21-
2221
import org.junit.Before;
2322
import org.junit.Rule;
2423

@@ -38,12 +37,13 @@ public class CustomDefaultOperationPreprocessors {
3837

3938
@Before
4039
public void setup() {
41-
// tag::custom-default-preprocessors[]
40+
// tag::custom-default-operation-preprocessors[]
4241
this.spec = new RequestSpecBuilder()
43-
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
44-
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
45-
.withDefaultResponsePreprocessors(prettyPrint()))
46-
.build();
47-
// end::custom-default-preprocessors[]
42+
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
43+
.withRequestDefaults(removeHeaders("Foo")) // <1>
44+
.withResponseDefaults(prettyPrint())) // <2>
45+
.build();
46+
// end::custom-default-operation-preprocessors[]
4847
}
48+
4949
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,18 @@ public class EveryTestPreprocessing {
4343
@Before
4444
public void setup() {
4545
this.spec = new RequestSpecBuilder()
46-
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
47-
.withDefaultRequestPreprocessors(removeHeaders("Foo"))
48-
.withDefaultResponsePreprocessors(prettyPrint()))
49-
.build();
46+
.addFilter(documentationConfiguration(this.restDocumentation).operationPreprocessors()
47+
.withRequestDefaults(removeHeaders("Foo")) // <1>
48+
.withResponseDefaults(prettyPrint())) // <2>
49+
.build();
5050
}
51-
5251
// end::setup[]
5352

5453
public void use() throws Exception {
5554
// tag::use[]
5655
RestAssured.given(this.spec)
57-
.filter(document("{method-name]",
58-
links(linkWithRel("self").description("Canonical self link"))))
56+
.filter(document("index",
57+
links(linkWithRel("self").description("Canonical self link"))))
5958
.when().get("/")
6059
.then().assertThat().statusCode(is(200));
6160
// end::use[]

spring-restdocs-core/src/main/java/org/springframework/restdocs/config/OperationPreprocessorsConfigurer.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@
2626
import org.springframework.restdocs.operation.preprocess.Preprocessors;
2727

2828
/**
29-
* A configurer that can be used to configure the default operation preprocessors that need to be used.
29+
* A configurer that can be used to configure the default operation preprocessors.
3030
*
3131
* @param <PARENT> The type of the configurer's parent
3232
* @param <TYPE> The concrete type of the configurer to be returned from chained methods
3333
* @author Filip Hrisafov
34+
* @author Andy Wilkinson
3435
* @since 2.0.0
3536
*/
3637
public abstract class OperationPreprocessorsConfigurer<PARENT, TYPE>
3738
extends AbstractNestedConfigurer<PARENT> {
3839

3940
private OperationRequestPreprocessor defaultOperationRequestPreprocessor;
41+
4042
private OperationResponsePreprocessor defaultOperationResponsePreprocessor;
4143

4244
/**
43-
* Creates a new {@code OperationPreprocessorConfigurer} with the given {@code parent}.
45+
* Creates a new {@code OperationPreprocessorConfigurer} with the given
46+
* {@code parent}.
4447
*
4548
* @param parent the parent
4649
*/
@@ -49,34 +52,40 @@ protected OperationPreprocessorsConfigurer(PARENT parent) {
4952
}
5053

5154
@Override
52-
public void apply(Map<String, Object> configuration, RestDocumentationContext context) {
53-
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
55+
public void apply(Map<String, Object> configuration,
56+
RestDocumentationContext context) {
57+
configuration.put(
58+
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
5459
this.defaultOperationRequestPreprocessor);
55-
configuration.put(RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
60+
configuration.put(
61+
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
5662
this.defaultOperationResponsePreprocessor);
5763
}
5864

5965
/**
60-
* Configures the default documentation operation request preprocessors.
66+
* Configures the default operation request preprocessors.
6167
*
6268
* @param preprocessors the preprocessors
6369
* @return {@code this}
6470
*/
6571
@SuppressWarnings("unchecked")
66-
public TYPE withDefaultRequestPreprocessors(OperationPreprocessor... preprocessors) {
67-
this.defaultOperationRequestPreprocessor = Preprocessors.preprocessRequest(preprocessors);
72+
public TYPE withRequestDefaults(OperationPreprocessor... preprocessors) {
73+
this.defaultOperationRequestPreprocessor = Preprocessors
74+
.preprocessRequest(preprocessors);
6875
return (TYPE) this;
6976
}
7077

7178
/**
72-
* Configures the default documentation operation response preprocessors.
79+
* Configures the default operation response preprocessors.
7380
*
7481
* @param preprocessors the preprocessors
7582
* @return {@code this}
7683
*/
7784
@SuppressWarnings("unchecked")
78-
public TYPE withDefaultResponsePreprocessors(OperationPreprocessor... preprocessors) {
79-
this.defaultOperationResponsePreprocessor = Preprocessors.preprocessResponse(preprocessors);
85+
public TYPE withResponseDefaults(OperationPreprocessor... preprocessors) {
86+
this.defaultOperationResponsePreprocessor = Preprocessors
87+
.preprocessResponse(preprocessors);
8088
return (TYPE) this;
8189
}
90+
8291
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/config/RestDocumentationConfigurer.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@
3535
/**
3636
* Abstract base class for the configuration of Spring REST Docs.
3737
*
38-
* @param <S> The concrete type of the {@link SnippetConfigurer}.
38+
* @param <S> The concrete type of the {@link SnippetConfigurer}
3939
* @param <P> The concrete type of the {@link OperationPreprocessorsConfigurer}
4040
* @param <T> The concrete type of this configurer, to be returned from methods that
4141
* support chaining
@@ -58,8 +58,8 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
5858
public abstract S snippets();
5959

6060
/**
61-
* Returns an {@link OperationPreprocessorsConfigurer} that can be used to configure the operation request and
62-
* response preprocessors that will be used during the documentation.
61+
* Returns an {@link OperationPreprocessorsConfigurer} that can be used to configure
62+
* the operation request and response preprocessors that will be used.
6363
*
6464
* @return the operation preprocessors configurer
6565
*/
@@ -100,8 +100,8 @@ public final T writerResolver(WriterResolver writerResolver) {
100100
protected final void apply(Map<String, Object> configuration,
101101
RestDocumentationContext context) {
102102
List<AbstractConfigurer> configurers = Arrays.asList(snippets(),
103-
operationPreprocessors(),
104-
this.templateEngineConfigurer, this.writerResolverConfigurer);
103+
operationPreprocessors(), this.templateEngineConfigurer,
104+
this.writerResolverConfigurer);
105105
for (AbstractConfigurer configurer : configurers) {
106106
configurer.apply(configuration, context);
107107
}

spring-restdocs-core/src/main/java/org/springframework/restdocs/generate/RestDocumentationGenerator.java

+40-34
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,16 @@ public final class RestDocumentationGenerator<REQ, RESP> {
5757
public static final String ATTRIBUTE_NAME_DEFAULT_SNIPPETS = "org.springframework.restdocs.defaultSnippets";
5858

5959
/**
60-
* Name of the operation attribute used to hold the default operation request preprocessor.
60+
* Name of the operation attribute used to hold the default operation request
61+
* preprocessor.
6162
*/
62-
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR =
63-
"org.springframework.restdocs.defaultOperationRequestPreprocessor";
63+
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR = "org.springframework.restdocs.defaultOperationRequestPreprocessor";
6464

6565
/**
66-
* Name of the operation attribute used to hold the default operation response preprocessor.
66+
* Name of the operation attribute used to hold the default operation response
67+
* preprocessor.
6768
*/
68-
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR =
69-
"org.springframework.restdocs.defaultOperationResponsePreprocessor";
70-
69+
public static final String ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR = "org.springframework.restdocs.defaultOperationResponsePreprocessor";
7170

7271
private final String identifier;
7372

@@ -198,8 +197,10 @@ public RestDocumentationGenerator(String identifier,
198197
*/
199198
public void handle(REQ request, RESP response, Map<String, Object> configuration) {
200199
Map<String, Object> attributes = new HashMap<>(configuration);
201-
OperationRequest operationRequest = preprocessRequest(request, attributes);
202-
OperationResponse operationResponse = preprocessResponse(response, attributes);
200+
OperationRequest operationRequest = preprocessRequest(
201+
this.requestConverter.convert(request), attributes);
202+
OperationResponse operationResponse = preprocessResponse(
203+
this.responseConverter.convert(response), attributes);
203204
Operation operation = new StandardOperation(this.identifier, operationRequest,
204205
operationResponse, attributes);
205206
try {
@@ -239,40 +240,45 @@ private List<Snippet> getSnippets(Map<String, Object> configuration) {
239240
return combinedSnippets;
240241
}
241242

242-
private OperationRequest preprocessRequest(REQ request, Map<String, Object> configuration) {
243-
List<OperationRequestPreprocessor> requestPreprocessors = getRequestPreprocessors(configuration);
244-
OperationRequest operationRequest = this.requestConverter.convert(request);
243+
private OperationRequest preprocessRequest(OperationRequest request,
244+
Map<String, Object> configuration) {
245+
List<OperationRequestPreprocessor> requestPreprocessors = getRequestPreprocessors(
246+
configuration);
245247
for (OperationRequestPreprocessor preprocessor : requestPreprocessors) {
246-
operationRequest = preprocessor.preprocess(operationRequest);
248+
request = preprocessor.preprocess(request);
247249
}
248-
return operationRequest;
250+
return request;
249251
}
250252

251-
private List<OperationRequestPreprocessor> getRequestPreprocessors(Map<String, Object> configuration) {
252-
List<OperationRequestPreprocessor> preprocessors = new ArrayList<>(2);
253-
preprocessors.add(this.requestPreprocessor);
254-
OperationRequestPreprocessor defaultRequestPreprocessor = (OperationRequestPreprocessor) configuration.get(
255-
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR);
256-
if (defaultRequestPreprocessor != null) {
257-
preprocessors.add(defaultRequestPreprocessor);
258-
}
259-
return preprocessors;
253+
private List<OperationRequestPreprocessor> getRequestPreprocessors(
254+
Map<String, Object> configuration) {
255+
return getPreprocessors(this.requestPreprocessor,
256+
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_REQUEST_PREPROCESSOR,
257+
configuration);
260258
}
261259

262-
private OperationResponse preprocessResponse(RESP response, Map<String, Object> configuration) {
263-
List<OperationResponsePreprocessor> responsePreprocessors = getResponsePreprocessors(configuration);
264-
OperationResponse operationResponse = this.responseConverter.convert(response);
265-
for (OperationResponsePreprocessor preprocessor : responsePreprocessors) {
266-
operationResponse = preprocessor.preprocess(operationResponse);
260+
private OperationResponse preprocessResponse(OperationResponse response,
261+
Map<String, Object> configuration) {
262+
for (OperationResponsePreprocessor preprocessor : getResponsePreprocessors(
263+
configuration)) {
264+
response = preprocessor.preprocess(response);
267265
}
268-
return operationResponse;
266+
return response;
269267
}
270268

271-
private List<OperationResponsePreprocessor> getResponsePreprocessors(Map<String, Object> configuration) {
272-
List<OperationResponsePreprocessor> preprocessors = new ArrayList<>(2);
273-
preprocessors.add(this.responsePreprocessor);
274-
OperationResponsePreprocessor defaultResponsePreprocessor = (OperationResponsePreprocessor) configuration.get(
275-
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR);
269+
private List<OperationResponsePreprocessor> getResponsePreprocessors(
270+
Map<String, Object> configuration) {
271+
return getPreprocessors(this.responsePreprocessor,
272+
RestDocumentationGenerator.ATTRIBUTE_NAME_DEFAULT_OPERATION_RESPONSE_PREPROCESSOR,
273+
configuration);
274+
}
275+
276+
@SuppressWarnings("unchecked")
277+
private <T> List<T> getPreprocessors(T preprocessor, String preprocessorAttribute,
278+
Map<String, Object> configuration) {
279+
List<T> preprocessors = new ArrayList<>(2);
280+
preprocessors.add(preprocessor);
281+
T defaultResponsePreprocessor = (T) configuration.get(preprocessorAttribute);
276282
if (defaultResponsePreprocessor != null) {
277283
preprocessors.add(defaultResponsePreprocessor);
278284
}

0 commit comments

Comments
 (0)