Skip to content

Commit fe4472d

Browse files
committed
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
1 parent b708027 commit fe4472d

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,16 +58,18 @@
5858
import org.springframework.web.servlet.support.SessionFlashMapManager;
5959
import org.springframework.web.util.UriComponentsBuilder;
6060
import org.springframework.web.util.UriUtils;
61+
import org.springframework.web.util.UrlPathHelper;
6162

6263
/**
63-
* Default builder for {@link MockHttpServletRequest} required as input to perform
64-
* requests in {@link MockMvc}.
64+
* Default builder for {@link MockHttpServletRequest} required as input to
65+
* perform requests in {@link MockMvc}.
6566
*
66-
* <p>Application tests will typically access this builder through the static factory
67-
* methods in {@link MockMvcRequestBuilders}.
67+
* <p>Application tests will typically access this builder through the static
68+
* factory methods in {@link MockMvcRequestBuilders}.
6869
*
69-
* <p>Although this class cannot be extended, additional ways to initialize the
70-
* {@code MockHttpServletRequest} can be plugged in via {@link #with(RequestPostProcessor)}.
70+
* <p>This class is not open for extension. To apply custom initialization to
71+
* the created {@code MockHttpServletRequest}, please use the
72+
* {@link #with(RequestPostProcessor)} extension point.
7173
*
7274
* @author Rossen Stoyanchev
7375
* @author Juergen Hoeller
@@ -81,6 +83,8 @@ public class MockHttpServletRequestBuilder
8183

8284
private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
8385

86+
private static final UrlPathHelper urlPathHelper = new UrlPathHelper();
87+
8488

8589
private final String method;
8690

@@ -692,7 +696,8 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
692696
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
693697
}
694698
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
695-
this.pathInfo = (StringUtils.hasText(extraPath) ? extraPath : null);
699+
this.pathInfo = (StringUtils.hasText(extraPath) ?
700+
urlPathHelper.decodeRequestString(request, extraPath) : null);
696701
}
697702
request.setPathInfo(this.pathInfo);
698703
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -163,6 +163,14 @@ public void contextPathServletPathInfo() {
163163
assertNull(request.getPathInfo());
164164
}
165165

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

0 commit comments

Comments
 (0)