Skip to content

Commit 3df0eac

Browse files
committed
AbstractMockHttpServletRequestBuilder#buildRequest is not idempotent
Signed-off-by: Réda Housni Alaoui <[email protected]>
1 parent 88812ed commit 3df0eac

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
@@ -77,6 +77,7 @@
7777
* @author Arjen Poutsma
7878
* @author Sam Brannen
7979
* @author Kamill Sokol
80+
* @author Réda Housni Alaoui
8081
* @since 6.2
8182
* @param <B> a self reference to the builder type
8283
*/
@@ -898,16 +899,17 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
898899
request.setContextPath(this.contextPath);
899900
request.setServletPath(this.servletPath);
900901

901-
if ("".equals(this.pathInfo)) {
902+
String pathInfoToUse = this.pathInfo;
903+
if ("".equals(pathInfoToUse)) {
902904
if (!requestUri.startsWith(this.contextPath + this.servletPath)) {
903905
throw new IllegalArgumentException(
904906
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
905907
}
906908
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
907-
this.pathInfo = (StringUtils.hasText(extraPath) ?
909+
pathInfoToUse = (StringUtils.hasText(extraPath) ?
908910
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
909911
}
910-
request.setPathInfo(this.pathInfo);
912+
request.setPathInfo(pathInfoToUse);
911913
}
912914

913915
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
@@ -32,6 +32,7 @@
3232
* Tests for {@link AbstractMockHttpServletRequestBuilder}
3333
*
3434
* @author Stephane Nicoll
35+
* @author Réda Housni Alaoui
3536
*/
3637
class AbstractMockHttpServletRequestBuilderTests {
3738

@@ -128,6 +129,14 @@ void mergeVersion() {
128129
assertThat(buildRequest(builder).getHeader("API-Version")).isEqualTo("1.1");
129130
}
130131

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

0 commit comments

Comments
 (0)