Fix MockMvc + HttpPutFormContentFilter issue

Issue: SPR-15753
This commit is contained in:
Rossen Stoyanchev 2017-07-11 11:43:48 +02:00
parent f6e9c47432
commit eb84547894
3 changed files with 97 additions and 10 deletions

View File

@ -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;
}
}
}

View File

@ -160,19 +160,20 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter {
}
@Override
@Nullable
public String[] getParameterValues(String name) {
String[] queryStringValues = super.getParameterValues(name);
List<String> formValues = this.formParameters.get(name);
if (formValues == null) {
return queryStringValues;
String[] queryParam = (super.getQueryString() != null ? super.getParameterValues(name) : null);
List<String> 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<String> result = new ArrayList<>(queryStringValues.length + formValues.size());
result.addAll(Arrays.asList(queryStringValues));
result.addAll(formValues);
List<String> result = new ArrayList<>(queryParam.length + formParam.size());
result.addAll(Arrays.asList(queryParam));
result.addAll(formParam);
return result.toArray(new String[result.size()]);
}
}

View File

@ -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"));