From 0cd427bdd35e668dda6332ae2885d94c222d9c49 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 1 Feb 2018 13:23:43 -0600 Subject: [PATCH] MockHttpServletRequestBuilder decodes pathInfo Previously MockHttpServletRequestBuilder calculated the pathInfo from the provided URL without decoding the value. This meant that the pathInfo incorrectly included URL encoded values. Now MockHttpServletRequestBuilder properly decodes the pathInfo. Fixes: SPR-16453 --- .../servlet/request/MockHttpServletRequestBuilder.java | 5 ++++- .../request/MockHttpServletRequestBuilderTests.java | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index cfb900bf0f..5b3255e1c0 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -59,6 +59,7 @@ import org.springframework.web.servlet.FlashMapManager; import org.springframework.web.servlet.support.SessionFlashMapManager; import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriUtils; +import org.springframework.web.util.UrlPathHelper; /** * Default builder for {@link MockHttpServletRequest} required as input to perform @@ -80,6 +81,8 @@ import org.springframework.web.util.UriUtils; public class MockHttpServletRequestBuilder implements ConfigurableSmartRequestBuilder, Mergeable { + private final UrlPathHelper urlPathHelper = new UrlPathHelper(); + private final String method; private final URI url; @@ -696,7 +699,7 @@ public class MockHttpServletRequestBuilder "Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]"); } String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length()); - this.pathInfo = (StringUtils.hasText(extraPath) ? extraPath : null); + this.pathInfo = (StringUtils.hasText(extraPath) ? this.urlPathHelper.decodeRequestString(request, extraPath) : null); } request.setPathInfo(this.pathInfo); } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java index 1b4ae58015..65b2ee7cde 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java @@ -164,6 +164,14 @@ public class MockHttpServletRequestBuilderTests { assertNull(request.getPathInfo()); } + @Test + public void pathInfoIsDecoded() { + this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels 42"); + MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); + + assertEquals("/travel/hotels 42", request.getPathInfo()); + } + @Test public void contextPathServletPathInvalid() { testContextPathServletPathInvalid("/Foo", "", "Request URI [/foo/bar] does not start with context path [/Foo]");