From 0063b152d99e05b0796295daa4d86b3170ecb717 Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Fri, 29 Mar 2024 15:48:24 +0800 Subject: [PATCH 1/7] test: add DisableRemoveContentLengthFilterTests --- ...DisableRemoveContentLengthFilterTests.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java new file mode 100644 index 0000000000..b66ff7b363 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java @@ -0,0 +1,38 @@ +package org.springframework.cloud.gateway.server.mvc; + + +import java.util.Map; + +import org.junit.jupiter.api.Test; + +import static org.springframework.cloud.gateway.server.mvc.test.TestUtils.getMap; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.util.LinkedMultiValueMap; + +import static org.assertj.core.api.Assertions.assertThat; + + +@SpringBootTest(properties = {"spring.cloud.gateway.mvc.http-client.type=jdk", + "spring.cloud.gateway.mvc.remove-content-length-request-headers-filter.enabled=false"}, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class DisableRemoveContentLengthFilterTests extends ServerMvcIntegrationTests { + + @Test + void formUrlencodedWorks() { + LinkedMultiValueMap formData = new LinkedMultiValueMap<>(); + formData.add("baz", "bam"); + + // @formatter:off + restClient.post().uri("/post?foo=fooquery").header("test", "formurlencoded") + .contentType(FORM_URL_ENCODED_CONTENT_TYPE) + .bodyValue(formData) + .exchange() + .expectStatus().isOk() + .expectBody(Map.class).consumeWith(result -> { + Map map = result.getResponseBody(); + Map form = getMap(map, "form"); + assertThat(form).containsEntry("foo", "bar"); + assertThat(form).containsEntry("baz", "bam"); + }); + } +} From 176a3b815b6844769b1552db20e634a9aa2d4025 Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Fri, 29 Mar 2024 15:48:24 +0800 Subject: [PATCH 2/7] test: add DisableRemoveContentLengthFilterTests --- .../server/mvc/DisableRemoveContentLengthFilterTests.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java index b66ff7b363..b4b8d445c6 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java @@ -31,7 +31,6 @@ void formUrlencodedWorks() { .expectBody(Map.class).consumeWith(result -> { Map map = result.getResponseBody(); Map form = getMap(map, "form"); - assertThat(form).containsEntry("foo", "bar"); assertThat(form).containsEntry("baz", "bam"); }); } From a44e160ec9c5737d367ed0e7efb62e6e943d5d32 Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Fri, 29 Mar 2024 17:03:26 +0800 Subject: [PATCH 3/7] fix: fix checkstyle error --- .../DisableRemoveContentLengthFilterTests.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java index b4b8d445c6..ac27455c75 100644 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2013-2023 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.cloud.gateway.server.mvc; @@ -5,11 +21,11 @@ import org.junit.jupiter.api.Test; -import static org.springframework.cloud.gateway.server.mvc.test.TestUtils.getMap; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.util.LinkedMultiValueMap; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.gateway.server.mvc.test.TestUtils.getMap; @SpringBootTest(properties = {"spring.cloud.gateway.mvc.http-client.type=jdk", From 2c4e5717ec5c25c06c94fcc163efb96753c75f5a Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Fri, 29 Mar 2024 17:09:43 +0800 Subject: [PATCH 4/7] fix: add a boolean variable for predicating whether append "&" --- .../cloud/gateway/server/mvc/filter/FormFilter.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java index d25b2ab063..9b999cc9f9 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java @@ -125,6 +125,7 @@ static HttpServletRequest getRequestWithBodyFromRequestParameters(HttpServletReq Map.Entry entry = entryIterator.next(); String name = entry.getKey(); List values = Arrays.asList(entry.getValue()); + boolean wrote = false; for (Iterator valueIterator = values.iterator(); valueIterator.hasNext();) { String value = valueIterator.next(); List queryValues = queryParams.get(name); @@ -139,9 +140,10 @@ static HttpServletRequest getRequestWithBodyFromRequestParameters(HttpServletReq writer.write('&'); } } + wrote = true; } } - if (entryIterator.hasNext()) { + if (wrote && entryIterator.hasNext()) { writer.append('&'); } } From ac7f2887da53dd4d43ebc1986824908dffc1007e Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Sun, 31 Mar 2024 15:09:50 +0800 Subject: [PATCH 5/7] fix: add a boolean variable for predicating whether append "&" --- ...DisableRemoveContentLengthFilterTests.java | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java deleted file mode 100644 index ac27455c75..0000000000 --- a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/DisableRemoveContentLengthFilterTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2013-2023 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.cloud.gateway.server.mvc; - - -import java.util.Map; - -import org.junit.jupiter.api.Test; - -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.util.LinkedMultiValueMap; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.cloud.gateway.server.mvc.test.TestUtils.getMap; - - -@SpringBootTest(properties = {"spring.cloud.gateway.mvc.http-client.type=jdk", - "spring.cloud.gateway.mvc.remove-content-length-request-headers-filter.enabled=false"}, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -public class DisableRemoveContentLengthFilterTests extends ServerMvcIntegrationTests { - - @Test - void formUrlencodedWorks() { - LinkedMultiValueMap formData = new LinkedMultiValueMap<>(); - formData.add("baz", "bam"); - - // @formatter:off - restClient.post().uri("/post?foo=fooquery").header("test", "formurlencoded") - .contentType(FORM_URL_ENCODED_CONTENT_TYPE) - .bodyValue(formData) - .exchange() - .expectStatus().isOk() - .expectBody(Map.class).consumeWith(result -> { - Map map = result.getResponseBody(); - Map form = getMap(map, "form"); - assertThat(form).containsEntry("baz", "bam"); - }); - } -} From 18411d985d003256e7067ff3c0f4e00b8811ce94 Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Sat, 6 Apr 2024 16:32:18 +0800 Subject: [PATCH 6/7] test: replenish FormFilterTests test case --- .../server/mvc/filter/FormFilterTests.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilterTests.java diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilterTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilterTests.java new file mode 100644 index 0000000000..8d9f67c8a3 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilterTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2013-2023 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.cloud.gateway.server.mvc.filter; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.Channels; +import java.nio.channels.ReadableByteChannel; + +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import org.junit.jupiter.api.Test; +import org.testcontainers.shaded.org.apache.commons.io.IOUtils; + +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockFilterChain; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Lu lu + */ +public class FormFilterTests { + + @Test + public void sameContentLengthWithOriginalPostFormUrlEncodeTest() throws ServletException, IOException { + + byte[] content = "baz=bam".getBytes(); + + MockHttpServletRequest request = MockMvcRequestBuilders.post("http://localhost/post?foo=fooquery") + .contentType(MediaType.APPLICATION_FORM_URLENCODED).content(content).buildRequest(null); + MockHttpServletResponse response = new MockHttpServletResponse(); + MockFilterChain mockFilterChain = new MockFilterChain(); + FormFilter formFilter = new FormFilter(); + formFilter.doFilter(request, response, mockFilterChain); + ServletRequest filterRequest = mockFilterChain.getRequest(); + ByteBuffer buffer = ByteBuffer.allocate(1024); + + ReadableByteChannel readableByteChannel = Channels.newChannel(filterRequest.getInputStream()); + IOUtils.read(readableByteChannel, buffer); + buffer.flip(); + assertThat(content.length).isEqualTo(buffer.remaining()); + + } + +} From 85dc9a482d80651b824db4a258dbfb7361702341 Mon Sep 17 00:00:00 2001 From: sunhao1256 Date: Sat, 6 Apr 2024 16:33:45 +0800 Subject: [PATCH 7/7] test: replenish FormFilterTests test case --- .../cloud/gateway/server/mvc/filter/FormFilter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java index 9b999cc9f9..4db87c23f1 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/FormFilter.java @@ -125,7 +125,7 @@ static HttpServletRequest getRequestWithBodyFromRequestParameters(HttpServletReq Map.Entry entry = entryIterator.next(); String name = entry.getKey(); List values = Arrays.asList(entry.getValue()); - boolean wrote = false; + boolean valueWritten = false; for (Iterator valueIterator = values.iterator(); valueIterator.hasNext();) { String value = valueIterator.next(); List queryValues = queryParams.get(name); @@ -140,10 +140,10 @@ static HttpServletRequest getRequestWithBodyFromRequestParameters(HttpServletReq writer.write('&'); } } - wrote = true; + valueWritten = true; } } - if (wrote && entryIterator.hasNext()) { + if (valueWritten && entryIterator.hasNext()) { writer.append('&'); } }