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,104 +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.net.URI;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationRequestFactory;
import org.springframework.restdocs.operation.OperationRequestPart;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.operation.OperationResponseFactory;
import org.springframework.restdocs.operation.Parameters;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link HeaderRemovingOperationPreprocessorTests}.
*
* @author Andy Wilkinson
* @author Roland Huss
*/
public class HeaderRemovingOperationPreprocessorTests {
private final OperationRequestFactory requestFactory = new OperationRequestFactory();
private final OperationResponseFactory responseFactory = new OperationResponseFactory();
private final HeaderRemovingOperationPreprocessor preprocessor = new HeaderRemovingOperationPreprocessor(
new ExactMatchHeaderFilter("b"));
@Test
public void modifyRequestHeaders() {
OperationRequest request = this.requestFactory.create(URI.create("http://localhost"), HttpMethod.GET,
new byte[0], getHttpHeaders(), new Parameters(), Collections.<OperationRequestPart>emptyList());
OperationRequest preprocessed = this.preprocessor.preprocess(request);
assertThat(preprocessed.getHeaders().size()).isEqualTo(2);
assertThat(preprocessed.getHeaders()).containsEntry("a", Arrays.asList("alpha"));
assertThat(preprocessed.getHeaders()).containsEntry("Host", Arrays.asList("localhost"));
}
@Test
public void modifyResponseHeaders() {
OperationResponse response = createResponse();
OperationResponse preprocessed = this.preprocessor.preprocess(response);
assertThat(preprocessed.getHeaders().size()).isEqualTo(1);
assertThat(preprocessed.getHeaders()).containsEntry("a", Arrays.asList("alpha"));
}
@Test
public void modifyWithPattern() {
OperationResponse response = createResponse("content-length", "1234");
HeaderRemovingOperationPreprocessor processor = new HeaderRemovingOperationPreprocessor(
new PatternMatchHeaderFilter("co.*le(.)gth]"));
OperationResponse preprocessed = processor.preprocess(response);
assertThat(preprocessed.getHeaders().size()).isEqualTo(2);
assertThat(preprocessed.getHeaders()).containsEntry("a", Arrays.asList("alpha"));
assertThat(preprocessed.getHeaders()).containsEntry("b", Arrays.asList("bravo", "banana"));
}
@Test
public void removeAllHeaders() {
HeaderRemovingOperationPreprocessor processor = new HeaderRemovingOperationPreprocessor(
new PatternMatchHeaderFilter(".*"));
OperationResponse preprocessed = processor.preprocess(createResponse());
assertThat(preprocessed.getHeaders().size()).isEqualTo(0);
}
private OperationResponse createResponse(String... extraHeaders) {
return this.responseFactory.create(HttpStatus.OK.value(), getHttpHeaders(extraHeaders), new byte[0]);
}
private HttpHeaders getHttpHeaders(String... extraHeaders) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("a", "alpha");
httpHeaders.add("b", "bravo");
httpHeaders.add("b", "banana");
for (int i = 0; i < extraHeaders.length; i += 2) {
httpHeaders.add(extraHeaders[i], extraHeaders[i + 1]);
}
return httpHeaders;
}
}

View File

@@ -19,7 +19,8 @@ package org.springframework.restdocs.operation.preprocess;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.List;
import java.util.function.Consumer;
import org.junit.Test;
@@ -38,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link HeadersModifyingOperationPreprocessor}.
*
* @author Jihoon Cha
* @author Andy Wilkinson
*/
public class HeadersModifyingOperationPreprocessorTests {
@@ -45,110 +47,120 @@ public class HeadersModifyingOperationPreprocessorTests {
@Test
public void addNewHeader() {
HttpHeaders headers = new HttpHeaders();
OperationPreprocessor preprocessor = this.preprocessor.add("a", "alpha");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).containsEntry("a",
this.preprocessor.add("a", "alpha");
assertThat(this.preprocessor.preprocess(createRequest()).getHeaders()).containsEntry("a",
Arrays.asList("alpha"));
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).containsEntry("a",
assertThat(this.preprocessor.preprocess(createResponse()).getHeaders()).containsEntry("a",
Arrays.asList("alpha"));
}
@Test
public void addValueToExistingHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("a", "apple");
OperationPreprocessor preprocessor = this.preprocessor.add("a", "alpha");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).containsEntry("a",
Arrays.asList("apple", "alpha"));
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).containsEntry("a",
Arrays.asList("apple", "alpha"));
this.preprocessor.add("a", "alpha");
assertThat(this.preprocessor.preprocess(createRequest((headers) -> headers.add("a", "apple"))).getHeaders())
.containsEntry("a", Arrays.asList("apple", "alpha"));
assertThat(this.preprocessor.preprocess(createResponse((headers) -> headers.add("a", "apple"))).getHeaders())
.containsEntry("a", Arrays.asList("apple", "alpha"));
}
@Test
public void setNewHeader() {
HttpHeaders headers = new HttpHeaders();
OperationPreprocessor preprocessor = this.preprocessor.set("a", "alpha", "avocado");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).containsEntry("a",
this.preprocessor.set("a", "alpha", "avocado");
assertThat(this.preprocessor.preprocess(createRequest()).getHeaders()).containsEntry("a",
Arrays.asList("alpha", "avocado"));
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).containsEntry("a",
assertThat(this.preprocessor.preprocess(createResponse()).getHeaders()).containsEntry("a",
Arrays.asList("alpha", "avocado"));
}
@Test
public void setExistingHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("a", "apple");
OperationPreprocessor preprocessor = this.preprocessor.set("a", "alpha", "avocado");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).containsEntry("a",
Arrays.asList("alpha", "avocado"));
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).containsEntry("a",
Arrays.asList("alpha", "avocado"));
this.preprocessor.set("a", "alpha", "avocado");
assertThat(this.preprocessor.preprocess(createRequest((headers) -> headers.add("a", "apple"))).getHeaders())
.containsEntry("a", Arrays.asList("alpha", "avocado"));
assertThat(this.preprocessor.preprocess(createResponse((headers) -> headers.add("a", "apple"))).getHeaders())
.containsEntry("a", Arrays.asList("alpha", "avocado"));
}
@Test
public void removeNonExistentHeader() {
HttpHeaders headers = new HttpHeaders();
OperationPreprocessor preprocessor = this.preprocessor.remove("a");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).doesNotContainKey("a");
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).doesNotContainKey("a");
this.preprocessor.remove("a");
assertThat(this.preprocessor.preprocess(createRequest()).getHeaders()).doesNotContainKey("a");
assertThat(this.preprocessor.preprocess(createResponse()).getHeaders()).doesNotContainKey("a");
}
@Test
public void removeHeader() {
HttpHeaders headers = new HttpHeaders();
headers.add("a", "apple");
OperationPreprocessor preprocessor = this.preprocessor.remove("a");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).doesNotContainKey("a");
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).doesNotContainKey("a");
assertThat(preprocessor.preprocess(createRequest((headers) -> headers.add("a", "apple"))).getHeaders())
.doesNotContainKey("a");
assertThat(preprocessor.preprocess(createResponse((headers) -> headers.add("a", "apple"))).getHeaders())
.doesNotContainKey("a");
}
@Test
public void removeHeaderValueForNonExistentHeader() {
HttpHeaders headers = new HttpHeaders();
OperationPreprocessor preprocessor = this.preprocessor.remove("a", "apple");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).doesNotContainKey("a");
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).doesNotContainKey("a");
this.preprocessor.remove("a", "apple");
assertThat(this.preprocessor.preprocess(createRequest()).getHeaders()).doesNotContainKey("a");
assertThat(this.preprocessor.preprocess(createResponse()).getHeaders()).doesNotContainKey("a");
}
@Test
public void removeHeaderValueWithMultipleValues() {
HttpHeaders headers = new HttpHeaders();
headers.add("a", "apple");
headers.add("a", "alpha");
OperationPreprocessor preprocessor = this.preprocessor.remove("a", "apple");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).containsEntry("a",
Arrays.asList("alpha"));
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).containsEntry("a",
Arrays.asList("alpha"));
this.preprocessor.remove("a", "apple");
assertThat(this.preprocessor
.preprocess(createRequest((headers) -> headers.addAll("a", List.of("apple", "alpha")))).getHeaders())
.containsEntry("a", Arrays.asList("alpha"));
assertThat(this.preprocessor
.preprocess(createResponse((headers) -> headers.addAll("a", List.of("apple", "alpha")))).getHeaders())
.containsEntry("a", Arrays.asList("alpha"));
}
@Test
public void removeHeaderValueWithSingleValueRemovesEntryEntirely() {
HttpHeaders headers = new HttpHeaders();
headers.add("a", "apple");
OperationPreprocessor preprocessor = this.preprocessor.remove("a", "apple");
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders()).doesNotContainKey("a");
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders()).doesNotContainKey("a");
this.preprocessor.remove("a", "apple");
assertThat(this.preprocessor.preprocess(createRequest((headers) -> headers.add("a", "apple"))).getHeaders())
.doesNotContainKey("a");
assertThat(this.preprocessor.preprocess(createResponse((headers) -> headers.add("a", "apple"))).getHeaders())
.doesNotContainKey("a");
}
@Test
public void removeHeadersByNamePattern() {
HttpHeaders headers = new HttpHeaders();
headers.add("apple", "apple");
headers.add("alpha", "alpha");
headers.add("avocado", "avocado");
headers.add("bravo", "bravo");
OperationPreprocessor preprocessor = this.preprocessor.remove(Pattern.compile("^a.*"));
assertThat(preprocessor.preprocess(createRequest(headers)).getHeaders().size()).isEqualTo(2);
assertThat(preprocessor.preprocess(createResponse(headers)).getHeaders().size()).isEqualTo(1);
Consumer<HttpHeaders> headersCustomizer = (headers) -> {
headers.add("apple", "apple");
headers.add("alpha", "alpha");
headers.add("avocado", "avocado");
headers.add("bravo", "bravo");
};
this.preprocessor.removeMatching("^a.*");
assertThat(this.preprocessor.preprocess(createRequest(headersCustomizer)).getHeaders()).containsOnlyKeys("Host",
"bravo");
assertThat(this.preprocessor.preprocess(createResponse(headersCustomizer)).getHeaders())
.containsOnlyKeys("bravo");
}
private OperationRequest createRequest(HttpHeaders headers) {
private OperationRequest createRequest() {
return createRequest(null);
}
private OperationRequest createRequest(Consumer<HttpHeaders> headersCustomizer) {
HttpHeaders headers = new HttpHeaders();
if (headersCustomizer != null) {
headersCustomizer.accept(headers);
}
return new OperationRequestFactory().create(URI.create("http://localhost:8080"), HttpMethod.GET, new byte[0],
headers, new Parameters(), Collections.emptyList());
}
private OperationResponse createResponse(HttpHeaders headers) {
private OperationResponse createResponse() {
return createResponse(null);
}
private OperationResponse createResponse(Consumer<HttpHeaders> headersCustomizer) {
HttpHeaders headers = new HttpHeaders();
if (headersCustomizer != null) {
headersCustomizer.accept(headers);
}
return new OperationResponseFactory().create(HttpStatus.OK.value(), headers, new byte[0]);
}