Browse Source

AbstractMockHttpServletRequestBuilder#buildRequest is not idempotent

See gh-35493

Signed-off-by: Réda Housni Alaoui <reda-alaoui@hey.com>
pull/35587/head
Réda Housni Alaoui 3 months ago committed by rstoyanchev
parent
commit
636523a2f5
  1. 8
      spring-test/src/main/java/org/springframework/test/web/servlet/request/AbstractMockHttpServletRequestBuilder.java
  2. 9
      spring-test/src/test/java/org/springframework/test/web/servlet/request/AbstractMockHttpServletRequestBuilderTests.java

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

@ -74,6 +74,7 @@ import org.springframework.web.util.UrlPathHelper; @@ -74,6 +74,7 @@ import org.springframework.web.util.UrlPathHelper;
* @author Arjen Poutsma
* @author Sam Brannen
* @author Kamill Sokol
* @author Réda Housni Alaoui
* @since 6.2
* @param <B> a self reference to the builder type
*/
@ -854,16 +855,17 @@ public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMo @@ -854,16 +855,17 @@ public abstract class AbstractMockHttpServletRequestBuilder<B extends AbstractMo
request.setContextPath(this.contextPath);
request.setServletPath(this.servletPath);
if ("".equals(this.pathInfo)) {
String pathInfoToUse = this.pathInfo;
if ("".equals(pathInfoToUse)) {
if (!requestUri.startsWith(this.contextPath + this.servletPath)) {
throw new IllegalArgumentException(
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
}
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
this.pathInfo = (StringUtils.hasText(extraPath) ?
pathInfoToUse = (StringUtils.hasText(extraPath) ?
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
}
request.setPathInfo(this.pathInfo);
request.setPathInfo(pathInfoToUse);
}
private void addRequestParams(MockHttpServletRequest request, MultiValueMap<String, String> map) {

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

@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -31,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link AbstractMockHttpServletRequestBuilder}
*
* @author Stephane Nicoll
* @author Réda Housni Alaoui
*/
class AbstractMockHttpServletRequestBuilderTests {
@ -97,6 +98,14 @@ class AbstractMockHttpServletRequestBuilderTests { @@ -97,6 +98,14 @@ class AbstractMockHttpServletRequestBuilderTests {
}
@Test
void pathInfoIsNotMutatedByBuildMethod() {
TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.GET).uri("/b");
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/b");
builder.uri("/a");
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/a");
}
private MockHttpServletRequest buildRequest(AbstractMockHttpServletRequestBuilder<?> builder) {
return builder.buildRequest(this.servletContext);
}

Loading…
Cancel
Save