From 4b38dc897249da19e0ccd56dc7c31d79a72a5804 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 5 Nov 2013 10:59:05 -0500 Subject: [PATCH] Fix exception mock request builder Issue: SPR-11043 --- .../request/MockHttpServletRequestBuilder.java | 5 ++--- .../request/MockHttpServletRequestBuilderTests.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index dbd9ac1d8884..2f9c91d4a697 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -581,9 +581,8 @@ public final MockHttpServletRequest buildRequest(ServletContext servletContext) for (Entry> entry : this.uriComponents.getQueryParams().entrySet()) { for (String value : entry.getValue()) { - request.addParameter( - UriUtils.decode(entry.getKey(), "UTF-8"), - UriUtils.decode(value, "UTF-8")); + value = (value != null) ? UriUtils.decode(value, "UTF-8") : null; + request.addParameter(UriUtils.decode(entry.getKey(), "UTF-8"), value); } } } diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 9320acd338b8..e33e92260b7b 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -228,6 +228,19 @@ public void requestParameterFromQueryWithEncoding() throws Exception { assertEquals("bar=baz", request.getParameter("foo")); } + // SPR-11043 + + @Test + public void requestParameterFromQueryNull() throws Exception { + this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/?foo"); + + MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); + Map parameterMap = request.getParameterMap(); + + assertArrayEquals(new String[]{null}, parameterMap.get("foo")); + assertEquals("foo", request.getQueryString()); + } + @Test public void acceptHeader() throws Exception { this.builder.accept(MediaType.TEXT_HTML, MediaType.APPLICATION_XML);