From b4be34bf8efe1494709a30a64b5ceb6daa1ffbcc Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 12 Oct 2022 10:17:01 +0100 Subject: [PATCH] Remove support for modifying request parameters Request parameters are a server-side constructor, specific to the servlet specification. Rather than modifying request parameters the query string of the URI or the form URL encoded content of the body should be modified instead. Closes gh-846 --- .../customizing-requests-and-responses.adoc | 7 - ...ametersModifyingOperationPreprocessor.java | 176 ------------------ .../operation/preprocess/Preprocessors.java | 10 - ...rsModifyingOperationPreprocessorTests.java | 147 --------------- 4 files changed, 340 deletions(-) delete mode 100644 spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessor.java delete mode 100644 spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessorTests.java diff --git a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc index 59177d3d..b5151269 100644 --- a/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc +++ b/docs/src/docs/asciidoc/customizing-requests-and-responses.adoc @@ -123,13 +123,6 @@ Any occurrences that match a regular expression are replaced. -[[customizing-requests-and-responses-preprocessors-modify-request-parameters]] -==== Modifying Request Parameters - -You can use `modifyParameters` on `Preprocessors` to add, set, and remove request parameters. - - - [[customizing-requests-and-responses-preprocessors-modify-uris]] ==== Modifying URIs diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessor.java deleted file mode 100644 index 94321343..00000000 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessor.java +++ /dev/null @@ -1,176 +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.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.springframework.restdocs.operation.OperationRequest; -import org.springframework.restdocs.operation.OperationRequestFactory; -import org.springframework.restdocs.operation.Parameters; -import org.springframework.util.Assert; - -/** - * An {@link OperationPreprocessor} that can be used to modify a request's - * {@link OperationRequest#getParameters()} by adding, setting, and removing parameters. - * - * @author Andy Wilkinson - * @since 1.1.0 - */ -public final class ParametersModifyingOperationPreprocessor extends OperationPreprocessorAdapter { - - private final OperationRequestFactory requestFactory = new OperationRequestFactory(); - - private final List modifications = new ArrayList<>(); - - @Override - public OperationRequest preprocess(OperationRequest request) { - Parameters parameters = new Parameters(); - parameters.putAll(request.getParameters()); - for (Modification modification : this.modifications) { - modification.apply(parameters); - } - return this.requestFactory.createFrom(request, parameters); - } - - /** - * Adds a parameter with the given {@code name} and {@code value}. - * @param name the name - * @param value the value - * @return {@code this} - */ - public ParametersModifyingOperationPreprocessor add(String name, String value) { - this.modifications.add(new AddParameterModification(name, value)); - return this; - } - - /** - * Sets the parameter with the given {@code name} to have the given {@code values}. - * @param name the name - * @param values the values - * @return {@code this} - */ - public ParametersModifyingOperationPreprocessor set(String name, String... values) { - Assert.notEmpty(values, "At least one value must be provided"); - this.modifications.add(new SetParameterModification(name, Arrays.asList(values))); - return this; - } - - /** - * Removes the parameter with the given {@code name}. - * @param name the name of the parameter - * @return {@code this} - */ - public ParametersModifyingOperationPreprocessor remove(String name) { - this.modifications.add(new RemoveParameterModification(name)); - return this; - } - - /** - * Removes the given {@code value} from the parameter with the given {@code name}. - * @param name the name - * @param value the value - * @return {@code this} - */ - public ParametersModifyingOperationPreprocessor remove(String name, String value) { - this.modifications.add(new RemoveValueParameterModification(name, value)); - return this; - } - - private interface Modification { - - void apply(Parameters parameters); - - } - - private static final class AddParameterModification implements Modification { - - private final String name; - - private final String value; - - private AddParameterModification(String name, String value) { - this.name = name; - this.value = value; - } - - @Override - public void apply(Parameters parameters) { - parameters.add(this.name, this.value); - } - - } - - private static final class SetParameterModification implements Modification { - - private final String name; - - private final List values; - - private SetParameterModification(String name, List values) { - this.name = name; - this.values = values; - } - - @Override - public void apply(Parameters parameters) { - parameters.put(this.name, this.values); - } - - } - - private static final class RemoveParameterModification implements Modification { - - private final String name; - - private RemoveParameterModification(String name) { - this.name = name; - } - - @Override - public void apply(Parameters parameters) { - parameters.remove(this.name); - } - - } - - private static final class RemoveValueParameterModification implements Modification { - - private final String name; - - private final String value; - - private RemoveValueParameterModification(String name, String value) { - this.name = name; - this.value = value; - } - - @Override - public void apply(Parameters parameters) { - List values = parameters.get(this.name); - if (values != null) { - values.remove(this.value); - if (values.isEmpty()) { - parameters.remove(this.name); - } - } - } - - } - -} diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java index e6bf13d9..ba741ff1 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/Preprocessors.java @@ -137,16 +137,6 @@ public final class Preprocessors { return new ContentModifyingOperationPreprocessor(new PatternReplacingContentModifier(pattern, replacement)); } - /** - * Returns a {@code ParametersModifyingOperationPreprocessor} that can then be - * configured to modify the parameters of the request. - * @return the preprocessor - * @since 1.1.0 - */ - public static ParametersModifyingOperationPreprocessor modifyParameters() { - return new ParametersModifyingOperationPreprocessor(); - } - /** * Returns a {@code HeadersModifyingOperationPreprocessor} that can then be configured * to modify the headers of the request or response. diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessorTests.java deleted file mode 100644 index 3331102c..00000000 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/ParametersModifyingOperationPreprocessorTests.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2014-2020 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.restdocs.operation.OperationRequest; -import org.springframework.restdocs.operation.OperationRequestFactory; -import org.springframework.restdocs.operation.OperationRequestPart; -import org.springframework.restdocs.operation.Parameters; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link ParametersModifyingOperationPreprocessor}. - * - * @author Andy Wilkinson - */ -public class ParametersModifyingOperationPreprocessorTests { - - private final ParametersModifyingOperationPreprocessor preprocessor = new ParametersModifyingOperationPreprocessor(); - - @Test - public void addNewParameter() { - Parameters parameters = new Parameters(); - OperationRequest request = this.preprocessor.add("a", "alpha").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha")); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080?a=alpha")); - } - - @Test - public void addValueToExistingParameter() { - Parameters parameters = new Parameters(); - parameters.add("a", "apple"); - OperationRequest request = this.preprocessor.add("a", "alpha").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).containsEntry("a", Arrays.asList("apple", "alpha")); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080?a=apple&a=alpha")); - } - - @Test - public void setNewParameter() { - Parameters parameters = new Parameters(); - OperationRequest request = this.preprocessor.set("a", "alpha", "avocado") - .preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha", "avocado")); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080?a=alpha&a=avocado")); - } - - @Test - public void setExistingParameter() { - Parameters parameters = new Parameters(); - parameters.add("a", "apple"); - OperationRequest request = this.preprocessor.set("a", "alpha", "avocado") - .preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha", "avocado")); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080?a=alpha&a=avocado")); - } - - @Test - public void removeNonExistentParameter() { - Parameters parameters = new Parameters(); - OperationRequest request = this.preprocessor.remove("a").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters().size()).isEqualTo(0); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080")); - } - - @Test - public void removeParameter() { - Parameters parameters = new Parameters(); - parameters.add("a", "apple"); - OperationRequest request = this.preprocessor.remove("a").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).isEmpty(); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080")); - } - - @Test - public void removeParameterValueForNonExistentParameter() { - Parameters parameters = new Parameters(); - OperationRequest request = this.preprocessor.remove("a", "apple").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).isEmpty(); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080")); - } - - @Test - public void removeParameterValueWithMultipleValues() { - Parameters parameters = new Parameters(); - parameters.add("a", "apple"); - parameters.add("a", "alpha"); - parameters.add("b", "bravo"); - OperationRequest request = this.preprocessor.remove("a", "apple").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).containsEntry("a", Arrays.asList("alpha")); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080?a=alpha&b=bravo")); - } - - @Test - public void removeParameterValueWithSingleValueRemovesEntryEntirely() { - Parameters parameters = new Parameters(); - parameters.add("a", "apple"); - parameters.add("b", "bravo"); - OperationRequest request = this.preprocessor.remove("a", "apple").preprocess(createGetRequest(parameters)); - assertThat(request.getParameters()).doesNotContainKey("a"); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080?b=bravo")); - } - - @Test - public void whenParametersOfANonGetRequestAreModifiedThenTheQueryStringIsUnaffected() { - Parameters parameters = new Parameters(); - parameters.add("a", "apple"); - parameters.add("b", "bravo"); - OperationRequest request = this.preprocessor.remove("a", "apple").preprocess(createPostRequest(parameters)); - assertThat(request.getParameters()).doesNotContainKey("a"); - assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080")); - } - - private OperationRequest createGetRequest(Parameters parameters) { - return new OperationRequestFactory().create( - URI.create("http://localhost:8080" + (parameters.isEmpty() ? "" : "?" + parameters.toQueryString())), - HttpMethod.GET, new byte[0], new HttpHeaders(), parameters, - Collections.emptyList()); - } - - private OperationRequest createPostRequest(Parameters parameters) { - return new OperationRequestFactory().create(URI.create("http://localhost:8080"), HttpMethod.POST, new byte[0], - new HttpHeaders(), parameters, Collections.emptyList()); - } - -}