Browse Source

HtmlUnitRequestBuilder decodes parameter names

Previously HtmlUnitRequestBuilder did not decode parameter names. This
means if a parameter like row[0] was submittted it would be encoded as
row%5B0%5D When the HttpServletRequest was created the parameter name would
not be decoded so the parameter name row[0] would not be found.

This commit ensures that HTTP parameter names are decoded.

Issue SPR-14177
pull/1037/head
Rob Winch 9 years ago
parent
commit
87ed8e939c
  1. 19
      spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java
  2. 21
      spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

19
spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

@ -360,14 +360,10 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { @@ -360,14 +360,10 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
private void params(MockHttpServletRequest request, UriComponents uriComponents) {
for (Entry<String, List<String>> entry : uriComponents.getQueryParams().entrySet()) {
String name = entry.getKey();
String urlDecodedName = urlDecode(name);
for (String value : entry.getValue()) {
try {
value = (value != null ? URLDecoder.decode(value, "UTF-8") : "");
request.addParameter(name, value);
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
value = (value != null ? urlDecode(value) : "");
request.addParameter(urlDecodedName, value);
}
}
for (NameValuePair param : this.webRequest.getRequestParameters()) {
@ -375,6 +371,15 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { @@ -375,6 +371,15 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
}
}
private String urlDecode(String value) {
try {
return URLDecoder.decode(value, "UTF-8");
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
private Locale parseLocale(String locale) {
Matcher matcher = LOCALE_PATTERN.matcher(locale);
if (!matcher.matches()) {

21
spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

@ -411,6 +411,27 @@ public class HtmlUnitRequestBuilderTests { @@ -411,6 +411,27 @@ public class HtmlUnitRequestBuilderTests {
assertThat(actualRequest.getParameter("name"), equalTo("value"));
}
// SPR-14177
@Test
public void buildRequestParameterMapDecodesParameterName() throws Exception {
webRequest.setUrl(new URL("http://example.com/example/?row%5B0%5D=value"));
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getParameterMap().size(), equalTo(1));
assertThat(actualRequest.getParameter("row[0]"), equalTo("value"));
}
@Test
public void buildRequestParameterMapDecodesParameterValue() throws Exception {
webRequest.setUrl(new URL("http://example.com/example/?name=row%5B0%5D"));
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getParameterMap().size(), equalTo(1));
assertThat(actualRequest.getParameter("name"), equalTo("row[0]"));
}
@Test
public void buildRequestParameterMapFromSingleQueryParamWithoutValueAndWithoutEqualsSign() throws Exception {
webRequest.setUrl(new URL("http://example.com/example/?name"));

Loading…
Cancel
Save