Skip to content

Commit 36418f4

Browse files
committed
Polish "Provide a preprocessor for modifying request and response headers"
See gh-584
1 parent 768a43a commit 36418f4

File tree

6 files changed

+102
-270
lines changed

6 files changed

+102
-270
lines changed

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,10 @@ You can also specify a different replacement if you wish.
108108
109109
110110
111-
[[customizing-requests-and-responses-preprocessors-remove-headers]]
112-
==== Removing Headers
113-
114-
`removeHeaders` on `Preprocessors` removes any headers from the request or response where the name is equal to any of the given header names.
111+
[[customizing-requests-and-responses-preprocessors-modify-headers]]
112+
==== Modifying Headers
115113
116-
`removeMatchingHeaders` on `Preprocessors` removes any headers from the request or response where the name matches any of the given regular expression patterns.
114+
You can use `modifyHeaders` on `Preprocessors` to add, set, and remove request or response headers.
117115
118116
119117
@@ -125,13 +123,6 @@ Any occurrences that match a regular expression are replaced.
125123
126124
127125
128-
[[customizing-requests-and-responses-preprocessors-modify-headers]]
129-
==== Modifying Headers
130-
131-
You can use `modifyHeaders` on `Preprocessors` to add, set, and remove request or response headers.
132-
133-
134-
135126
[[customizing-requests-and-responses-preprocessors-modify-request-parameters]]
136127
==== Modifying Request Parameters
137128

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessor.java

-69
This file was deleted.

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/HeadersModifyingOperationPreprocessor.java

+14-23
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
import org.springframework.util.Assert;
3131

3232
/**
33-
* An {@link OperationPreprocessor} that can be used to modify a request's
34-
* {@link OperationRequest#getHeaders()} by adding, setting, and removing headers.
33+
* An {@link OperationPreprocessor} that modifies a request or response by adding,
34+
* setting, or removing headers.
3535
*
3636
* @author Jihoon Cha
37+
* @author Andy Wilkinson
38+
* @since 3.0.0
3739
*/
3840
public class HeadersModifyingOperationPreprocessor implements OperationPreprocessor {
3941

@@ -45,32 +47,21 @@ public class HeadersModifyingOperationPreprocessor implements OperationPreproces
4547

4648
@Override
4749
public OperationRequest preprocess(OperationRequest request) {
48-
HttpHeaders headers = copyHttpHeaders(request.getHeaders());
49-
for (Modification modification : this.modifications) {
50-
modification.applyTo(headers);
51-
}
52-
return this.requestFactory.createFrom(request, headers);
50+
return this.requestFactory.createFrom(request, preprocess(request.getHeaders()));
5351
}
5452

5553
@Override
5654
public OperationResponse preprocess(OperationResponse response) {
57-
HttpHeaders headers = copyHttpHeaders(response.getHeaders());
58-
for (Modification modification : this.modifications) {
59-
modification.applyTo(headers);
60-
}
61-
return this.responseFactory.createFrom(response, headers);
55+
return this.responseFactory.createFrom(response, preprocess(response.getHeaders()));
6256
}
6357

64-
private HttpHeaders copyHttpHeaders(HttpHeaders headers) {
65-
HttpHeaders copy = new HttpHeaders();
66-
for (String name : headers.keySet()) {
67-
List<String> values = headers.get(name);
68-
if (values == null) {
69-
continue;
70-
}
71-
copy.put(name, new ArrayList<>(values));
58+
private HttpHeaders preprocess(HttpHeaders headers) {
59+
HttpHeaders modifiedHeaders = new HttpHeaders();
60+
modifiedHeaders.putAll(headers);
61+
for (Modification modification : this.modifications) {
62+
modification.applyTo(modifiedHeaders);
7263
}
73-
return copy;
64+
return modifiedHeaders;
7465
}
7566

7667
/**
@@ -123,8 +114,8 @@ public HeadersModifyingOperationPreprocessor remove(String name, String value) {
123114
* @return {@code this}
124115
* @see Matcher#matches()
125116
*/
126-
public HeadersModifyingOperationPreprocessor remove(Pattern namePattern) {
127-
this.modifications.add(new RemoveHeadersByNamePatternModification(namePattern));
117+
public HeadersModifyingOperationPreprocessor removeMatching(String namePattern) {
118+
this.modifications.add(new RemoveHeadersByNamePatternModification(Pattern.compile(namePattern)));
128119
return this;
129120
}
130121

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ public static OperationPreprocessor prettyPrint() {
7474
* {@code headersToRemove}.
7575
* @param headerNames the header names
7676
* @return the preprocessor
77-
* @deprecated Use {@link #modifyHeaders()} instead
77+
* @deprecated since 3.0.0 in favor of {@link #modifyHeaders()} and
78+
* {@link HeadersModifyingOperationPreprocessor#remove(String)}
7879
* @see String#equals(Object)
7980
*/
8081
@Deprecated
8182
public static OperationPreprocessor removeHeaders(String... headerNames) {
82-
return new HeaderRemovingOperationPreprocessor(new ExactMatchHeaderFilter(headerNames));
83+
HeadersModifyingOperationPreprocessor preprocessor = new HeadersModifyingOperationPreprocessor();
84+
for (String headerName : headerNames) {
85+
preprocessor.remove(headerName);
86+
}
87+
return preprocessor;
8388
}
8489

8590
/**
@@ -88,12 +93,17 @@ public static OperationPreprocessor removeHeaders(String... headerNames) {
8893
* {@code headerNamePatterns} regular expressions.
8994
* @param headerNamePatterns the header name patterns
9095
* @return the preprocessor
91-
* @deprecated Use {@link #modifyHeaders()} instead
96+
* @deprecated since 3.0.0 in favor of {@link #modifyHeaders()} and
97+
* {@link HeadersModifyingOperationPreprocessor#removeMatching(String)}
9298
* @see java.util.regex.Matcher#matches()
9399
*/
94100
@Deprecated
95101
public static OperationPreprocessor removeMatchingHeaders(String... headerNamePatterns) {
96-
return new HeaderRemovingOperationPreprocessor(new PatternMatchHeaderFilter(headerNamePatterns));
102+
HeadersModifyingOperationPreprocessor preprocessor = new HeadersModifyingOperationPreprocessor();
103+
for (String headerNamePattern : headerNamePatterns) {
104+
preprocessor.removeMatching(headerNamePattern);
105+
}
106+
return preprocessor;
97107
}
98108

99109
/**
@@ -141,6 +151,7 @@ public static ParametersModifyingOperationPreprocessor modifyParameters() {
141151
* Returns a {@code HeadersModifyingOperationPreprocessor} that can then be configured
142152
* to modify the headers of the request.
143153
* @return the preprocessor
154+
* @since 3.0.0
144155
*/
145156
public static HeadersModifyingOperationPreprocessor modifyHeaders() {
146157
return new HeadersModifyingOperationPreprocessor();

spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/HeaderRemovingOperationPreprocessorTests.java

-104
This file was deleted.

0 commit comments

Comments
 (0)