Skip to content

Commit 0cd427b

Browse files
rwinchrstoyanchev
authored andcommitted
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
1 parent 609f173 commit 0cd427b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.springframework.web.servlet.support.SessionFlashMapManager;
6060
import org.springframework.web.util.UriComponentsBuilder;
6161
import org.springframework.web.util.UriUtils;
62+
import org.springframework.web.util.UrlPathHelper;
6263

6364
/**
6465
* Default builder for {@link MockHttpServletRequest} required as input to perform
@@ -80,6 +81,8 @@
8081
public class MockHttpServletRequestBuilder
8182
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {
8283

84+
private final UrlPathHelper urlPathHelper = new UrlPathHelper();
85+
8386
private final String method;
8487

8588
private final URI url;
@@ -696,7 +699,7 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
696699
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
697700
}
698701
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
699-
this.pathInfo = (StringUtils.hasText(extraPath) ? extraPath : null);
702+
this.pathInfo = (StringUtils.hasText(extraPath) ? this.urlPathHelper.decodeRequestString(request, extraPath) : null);
700703
}
701704
request.setPathInfo(this.pathInfo);
702705
}

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@ public void contextPathServletPathInfo() {
164164
assertNull(request.getPathInfo());
165165
}
166166

167+
@Test
168+
public void pathInfoIsDecoded() {
169+
this.builder = new MockHttpServletRequestBuilder(HttpMethod.GET, "/travel/hotels 42");
170+
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
171+
172+
assertEquals("/travel/hotels 42", request.getPathInfo());
173+
}
174+
167175
@Test
168176
public void contextPathServletPathInvalid() {
169177
testContextPathServletPathInvalid("/Foo", "", "Request URI [/foo/bar] does not start with context path [/Foo]");

0 commit comments

Comments
 (0)