diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/FormContentTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/FormContentTests.java new file mode 100644 index 00000000000..129befb2300 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/FormContentTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2017 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 + * + * http://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.test.web.servlet.samples.spr; + +import org.junit.Test; + +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.filter.HttpPutFormContentFilter; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; + +/** + * Test for issues related to form content. + * @author Rossen Stoyanchev + */ +public class FormContentTests { + + @Test // SPR-15753 + public void formContentIsNotDuplicated() throws Exception { + + MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new Spr15753Controller()) + .addFilter(new HttpPutFormContentFilter()) + .build(); + + mockMvc.perform(put("/").content("d1=a&d2=s").contentType(MediaType.APPLICATION_FORM_URLENCODED)) + .andExpect(content().string("d1:a, d2:s.")); + + } + + + @RestController + public class Spr15753Controller { + + @PutMapping + public String test(Data d) { + return String.format("d1:%s, d2:%s.", d.getD1(), d.getD2()); + } + } + + public class Data { + + private String d1; + + private String d2; + + public Data() { + } + + public String getD1() { + return d1; + } + + public void setD1(String d1) { + this.d1 = d1; + } + + public String getD2() { + return d2; + } + + public void setD2(String d2) { + this.d2 = d2; + } + } +} diff --git a/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java b/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java index 19b9f3313cd..0768f05dc69 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java @@ -160,19 +160,20 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter { } @Override + @Nullable public String[] getParameterValues(String name) { - String[] queryStringValues = super.getParameterValues(name); - List formValues = this.formParameters.get(name); - if (formValues == null) { - return queryStringValues; + String[] queryParam = (super.getQueryString() != null ? super.getParameterValues(name) : null); + List formParam = this.formParameters.get(name); + if (formParam == null) { + return queryParam; } - else if (queryStringValues == null) { - return formValues.toArray(new String[formValues.size()]); + else if (queryParam == null) { + return formParam.toArray(new String[formParam.size()]); } else { - List result = new ArrayList<>(queryStringValues.length + formValues.size()); - result.addAll(Arrays.asList(queryStringValues)); - result.addAll(formValues); + List result = new ArrayList<>(queryParam.length + formParam.size()); + result.addAll(Arrays.asList(queryParam)); + result.addAll(formParam); return result.toArray(new String[result.size()]); } } diff --git a/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java index 179eb4cccac..6a5de837ddd 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/HttpPutFormContentFilterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. @@ -136,6 +136,7 @@ public class HttpPutFormContentFilterTests { @Test public void getParameterValues() throws Exception { + request.setQueryString("name=value1&name=value2"); request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("name=value3&name=value4".getBytes("ISO-8859-1")); @@ -149,6 +150,7 @@ public class HttpPutFormContentFilterTests { @Test public void getParameterValuesFromQueryString() throws Exception { + request.setQueryString("name=value1&name=value2"); request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("anotherName=anotherValue".getBytes("ISO-8859-1")); @@ -188,6 +190,7 @@ public class HttpPutFormContentFilterTests { @Test public void getParameterMap() throws Exception { + request.setQueryString("name=value1&name=value2"); request.addParameter("name", "value1"); request.addParameter("name", "value2"); request.setContent("name=value3&name4=value4".getBytes("ISO-8859-1"));