Skip to content

Commit df860fd

Browse files
committed
Polishing contribution
Closes gh-35493
1 parent 636523a commit df860fd

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
* @author Arjen Poutsma
7575
* @author Sam Brannen
7676
* @author Kamill Sokol
77-
* @author Réda Housni Alaoui
7877
* @since 6.2
7978
* @param <B> a self reference to the builder type
8079
*/
@@ -855,17 +854,14 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
855854
request.setContextPath(this.contextPath);
856855
request.setServletPath(this.servletPath);
857856

858-
String pathInfoToUse = this.pathInfo;
859-
if ("".equals(pathInfoToUse)) {
860-
if (!requestUri.startsWith(this.contextPath + this.servletPath)) {
861-
throw new IllegalArgumentException(
862-
"Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
863-
}
864-
String extraPath = requestUri.substring(this.contextPath.length() + this.servletPath.length());
865-
pathInfoToUse = (StringUtils.hasText(extraPath) ?
866-
UrlPathHelper.defaultInstance.decodeRequestString(request, extraPath) : null);
857+
String path = this.pathInfo;
858+
if ("".equals(path)) {
859+
Assert.isTrue(requestUri.startsWith(this.contextPath + this.servletPath),
860+
() -> "Invalid servlet path [" + this.servletPath + "] for request URI [" + requestUri + "]");
861+
String other = requestUri.substring(this.contextPath.length() + this.servletPath.length());
862+
path = (StringUtils.hasText(other) ? UrlPathHelper.defaultInstance.decodeRequestString(request, other) : null);
867863
}
868-
request.setPathInfo(pathInfoToUse);
864+
request.setPathInfo(path);
869865
}
870866

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void mergeUriWhenUriIsSetDoesNotOverride() {
9898
}
9999

100100

101-
@Test
101+
@Test // gh-35493
102102
void pathInfoIsNotMutatedByBuildMethod() {
103103
TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.GET).uri("/b");
104104
assertThat(buildRequest(builder).getPathInfo()).isEqualTo("/b");

0 commit comments

Comments
 (0)