Skip to content

Commit 636523a

Browse files
reda-alaouirstoyanchev
authored andcommitted
AbstractMockHttpServletRequestBuilder#buildRequest is not idempotent
See gh-35493 Signed-off-by: Réda Housni Alaoui <reda-alaoui@hey.com>
1 parent a19b51b commit 636523a

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* @author Arjen Poutsma
7575
* @author Sam Brannen
7676
* @author Kamill Sokol
77+
* @author Réda Housni Alaoui
7778
* @since 6.2
7879
* @param <B> a self reference to the builder type
7980
*/
@@ -854,16 +855,17 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
854855
request.setContextPath(this.contextPath);
855856
request.setServletPath(this.servletPath);
856857

857-
if ("".equals(this.pathInfo)) {
858+
String pathInfoToUse = this.pathInfo;
859+
if ("".equals(pathInfoToUse)) {
858860
if (!requestUri.startsWith(this.contextPath + this.servletPath)) {
859861
throw new IllegalArgumentException(
860862
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
861863
}
862864
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
863-
this.pathInfo = (StringUtils.hasText(extraPath) ?
865+
pathInfoToUse = (StringUtils.hasText(extraPath) ?
864866
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
865867
}
866-
request.setPathInfo(this.pathInfo);
868+
request.setPathInfo(pathInfoToUse);
867869
}
868870

869871
private void addRequestParams(MockHttpServletRequest request, MultiValueMap<String, String> map) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Tests for {@link AbstractMockHttpServletRequestBuilder}
3232
*
3333
* @author Stephane Nicoll
34+
* @author Réda Housni Alaoui
3435
*/
3536
class AbstractMockHttpServletRequestBuilderTests {
3637

@@ -97,6 +98,14 @@ void mergeUriWhenUriIsSetDoesNotOverride() {
9798
}
9899

99100

101+
@Test
102+
void pathInfoIsNotMutatedByBuildMethod() {
103+
TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.GET).uri("/b");
104+
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/b");
105+
builder.uri("/a");
106+
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/a");
107+
}
108+
100109
private MockHttpServletRequest buildRequest(AbstractMockHttpServletRequestBuilder<?> builder) {
101110
return builder.buildRequest(this.servletContext);
102111
}

0 commit comments

Comments
 (0)