Skip to content

Commit 775c69d

Browse files
committed
AbstractMockHttpServletRequestBuilder#buildRequest is not idempotent
1 parent 88812ed commit 775c69d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,16 +898,17 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
898898
request.setContextPath(this.contextPath);
899899
request.setServletPath(this.servletPath);
900900

901-
if ("".equals(this.pathInfo)) {
901+
String pathInfoToUse = this.pathInfo;
902+
if ("".equals(pathInfoToUse)) {
902903
if (!requestUri.startsWith(this.contextPath + this.servletPath)) {
903904
throw new IllegalArgumentException(
904905
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
905906
}
906907
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
907-
this.pathInfo = (StringUtils.hasText(extraPath) ?
908+
pathInfoToUse = (StringUtils.hasText(extraPath) ?
908909
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
909910
}
910-
request.setPathInfo(this.pathInfo);
911+
request.setPathInfo(pathInfoToUse);
911912
}
912913

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ void mergeVersion() {
128128
assertThat(buildRequest(builder).getHeader("API-Version")).isEqualTo("1.1");
129129
}
130130

131+
@Test
132+
void pathInfoIsNotMutatedByBuildMethod() {
133+
TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.GET).uri("/b");
134+
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/b");
135+
builder.uri("/a");
136+
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/a");
137+
}
138+
131139
private MockHttpServletRequest buildRequest(AbstractMockHttpServletRequestBuilder<?> builder) {
132140
return builder.buildRequest(this.servletContext);
133141
}

0 commit comments

Comments
 (0)