Skip to content

Commit

Permalink
MockHttpServletRequestBuilder decodes pathInfo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rwinch authored and rstoyanchev committed Feb 2, 2018
1 parent 609f173 commit 0cd427b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
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
Expand All @@ -80,6 +81,8 @@
public class MockHttpServletRequestBuilder
implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable {

private final UrlPathHelper urlPathHelper = new UrlPathHelper();

private final String method;

private final URI url;
Expand Down Expand Up @@ -696,7 +699,7 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
"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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public void contextPathServletPathInfo() {
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]");
Expand Down

0 comments on commit 0cd427b

Please sign in to comment.