From fe4472dbebbbaa86cf3157a20c0dd2f0aaef57cd Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 2 Feb 2018 15:17:58 -0500 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. Backport of #0cd427bdd35e668dda6332ae2885d94c222d9c49. Fixes: SPR-16453 --- .../MockHttpServletRequestBuilder.java | 21 ++++++++++++------- .../MockHttpServletRequestBuilderTests.java | 10 ++++++++- 2 files changed, 22 insertions(+), 9 deletions(-) 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 4b6d6d993c3..a38e4466948 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -58,16 +58,18 @@ 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 - * requests in {@link MockMvc}. + * Default builder for {@link MockHttpServletRequest} required as input to + * perform requests in {@link MockMvc}. * - *

Application tests will typically access this builder through the static factory - * methods in {@link MockMvcRequestBuilders}. + *

Application tests will typically access this builder through the static + * factory methods in {@link MockMvcRequestBuilders}. * - *

Although this class cannot be extended, additional ways to initialize the - * {@code MockHttpServletRequest} can be plugged in via {@link #with(RequestPostProcessor)}. + *

This class is not open for extension. To apply custom initialization to + * the created {@code MockHttpServletRequest}, please use the + * {@link #with(RequestPostProcessor)} extension point. * * @author Rossen Stoyanchev * @author Juergen Hoeller @@ -81,6 +83,8 @@ public class MockHttpServletRequestBuilder private static final Charset UTF8_CHARSET = Charset.forName("UTF-8"); + private static final UrlPathHelper urlPathHelper = new UrlPathHelper(); + private final String method; @@ -692,7 +696,8 @@ 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) ? + 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 87229a79e39..471c12358e3 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -163,6 +163,14 @@ public class MockHttpServletRequestBuilderTests { assertNull(request.getPathInfo()); } + @Test // SPR-16453 + 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]");