Polish "Provide a preprocessor for modifying request and response headers"

See gh-584
This commit is contained in:
Andy Wilkinson
2022-05-12 14:27:49 +01:00
parent 768a43a61e
commit 36418f47d7
6 changed files with 102 additions and 270 deletions

View File

@@ -1,69 +0,0 @@
/*
* Copyright 2014-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.restdocs.operation.preprocess;
import java.util.List;
import java.util.Map.Entry;
import org.springframework.http.HttpHeaders;
import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationRequestFactory;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.OperationResponseFactory;
/**
* An {@link OperationPreprocessor} that removes headers. The headers to remove are
* provided as constructor arguments and can be either plain string or patterns to match
* against the headers found
*
* @author Andy Wilkinson
* @deprecated Use {@link HeadersModifyingOperationPreprocessor} instead
*/
@Deprecated
class HeaderRemovingOperationPreprocessor implements OperationPreprocessor {
private final OperationRequestFactory requestFactory = new OperationRequestFactory();
private final OperationResponseFactory responseFactory = new OperationResponseFactory();
private final HeaderFilter headerFilter;
HeaderRemovingOperationPreprocessor(HeaderFilter headerFilter) {
this.headerFilter = headerFilter;
}
@Override
public OperationResponse preprocess(OperationResponse response) {
return this.responseFactory.createFrom(response, removeHeaders(response.getHeaders()));
}
@Override
public OperationRequest preprocess(OperationRequest request) {
return this.requestFactory.createFrom(request, removeHeaders(request.getHeaders()));
}
private HttpHeaders removeHeaders(HttpHeaders originalHeaders) {
HttpHeaders processedHeaders = new HttpHeaders();
for (Entry<String, List<String>> header : originalHeaders.entrySet()) {
if (!this.headerFilter.excludeHeader(header.getKey())) {
processedHeaders.put(header.getKey(), header.getValue());
}
}
return processedHeaders;
}
}

View File

@@ -30,10 +30,12 @@ import org.springframework.restdocs.operation.OperationResponseFactory;
import org.springframework.util.Assert;
/**
* An {@link OperationPreprocessor} that can be used to modify a request's
* {@link OperationRequest#getHeaders()} by adding, setting, and removing headers.
* An {@link OperationPreprocessor} that modifies a request or response by adding,
* setting, or removing headers.
*
* @author Jihoon Cha
* @author Andy Wilkinson
* @since 3.0.0
*/
public class HeadersModifyingOperationPreprocessor implements OperationPreprocessor {
@@ -45,32 +47,21 @@ public class HeadersModifyingOperationPreprocessor implements OperationPreproces
@Override
public OperationRequest preprocess(OperationRequest request) {
HttpHeaders headers = copyHttpHeaders(request.getHeaders());
for (Modification modification : this.modifications) {
modification.applyTo(headers);
}
return this.requestFactory.createFrom(request, headers);
return this.requestFactory.createFrom(request, preprocess(request.getHeaders()));
}
@Override
public OperationResponse preprocess(OperationResponse response) {
HttpHeaders headers = copyHttpHeaders(response.getHeaders());
for (Modification modification : this.modifications) {
modification.applyTo(headers);
}
return this.responseFactory.createFrom(response, headers);
return this.responseFactory.createFrom(response, preprocess(response.getHeaders()));
}
private HttpHeaders copyHttpHeaders(HttpHeaders headers) {
HttpHeaders copy = new HttpHeaders();
for (String name : headers.keySet()) {
List<String> values = headers.get(name);
if (values == null) {
continue;
}
copy.put(name, new ArrayList<>(values));
private HttpHeaders preprocess(HttpHeaders headers) {
HttpHeaders modifiedHeaders = new HttpHeaders();
modifiedHeaders.putAll(headers);
for (Modification modification : this.modifications) {
modification.applyTo(modifiedHeaders);
}
return copy;
return modifiedHeaders;
}
/**
@@ -123,8 +114,8 @@ public class HeadersModifyingOperationPreprocessor implements OperationPreproces
* @return {@code this}
* @see Matcher#matches()
*/
public HeadersModifyingOperationPreprocessor remove(Pattern namePattern) {
this.modifications.add(new RemoveHeadersByNamePatternModification(namePattern));
public HeadersModifyingOperationPreprocessor removeMatching(String namePattern) {
this.modifications.add(new RemoveHeadersByNamePatternModification(Pattern.compile(namePattern)));
return this;
}

View File

@@ -74,12 +74,17 @@ public final class Preprocessors {
* {@code headersToRemove}.
* @param headerNames the header names
* @return the preprocessor
* @deprecated Use {@link #modifyHeaders()} instead
* @deprecated since 3.0.0 in favor of {@link #modifyHeaders()} and
* {@link HeadersModifyingOperationPreprocessor#remove(String)}
* @see String#equals(Object)
*/
@Deprecated
public static OperationPreprocessor removeHeaders(String... headerNames) {
return new HeaderRemovingOperationPreprocessor(new ExactMatchHeaderFilter(headerNames));
HeadersModifyingOperationPreprocessor preprocessor = new HeadersModifyingOperationPreprocessor();
for (String headerName : headerNames) {
preprocessor.remove(headerName);
}
return preprocessor;
}
/**
@@ -88,12 +93,17 @@ public final class Preprocessors {
* {@code headerNamePatterns} regular expressions.
* @param headerNamePatterns the header name patterns
* @return the preprocessor
* @deprecated Use {@link #modifyHeaders()} instead
* @deprecated since 3.0.0 in favor of {@link #modifyHeaders()} and
* {@link HeadersModifyingOperationPreprocessor#removeMatching(String)}
* @see java.util.regex.Matcher#matches()
*/
@Deprecated
public static OperationPreprocessor removeMatchingHeaders(String... headerNamePatterns) {
return new HeaderRemovingOperationPreprocessor(new PatternMatchHeaderFilter(headerNamePatterns));
HeadersModifyingOperationPreprocessor preprocessor = new HeadersModifyingOperationPreprocessor();
for (String headerNamePattern : headerNamePatterns) {
preprocessor.removeMatching(headerNamePattern);
}
return preprocessor;
}
/**
@@ -141,6 +151,7 @@ public final class Preprocessors {
* Returns a {@code HeadersModifyingOperationPreprocessor} that can then be configured
* to modify the headers of the request.
* @return the preprocessor
* @since 3.0.0
*/
public static HeadersModifyingOperationPreprocessor modifyHeaders() {
return new HeadersModifyingOperationPreprocessor();