Skip to content

Commit dbe25cf

Browse files
committed
Add RequestPath tests for modifying the contextPath
1 parent 6855a85 commit dbe25cf

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

spring-web/src/main/java/org/springframework/http/server/DefaultRequestPath.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ private static PathContainer initContextPath(PathContainer path, @Nullable Strin
5555
return PathContainer.parseUrlPath("");
5656
}
5757

58-
Assert.isTrue(contextPath.startsWith("/") && !contextPath.endsWith("/") &&
59-
path.value().startsWith(contextPath), "Invalid contextPath: " + contextPath);
58+
validateContextPath(path.value(), contextPath);
6059

6160
int length = contextPath.length();
6261
int counter = 0;
@@ -70,8 +69,24 @@ private static PathContainer initContextPath(PathContainer path, @Nullable Strin
7069
}
7170

7271
// Should not happen..
73-
throw new IllegalStateException("Failed to initialize contextPath='" + contextPath + "'" +
74-
" given path='" + path.value() + "'");
72+
throw new IllegalStateException("Failed to initialize contextPath '" + contextPath + "'" +
73+
" for requestPath '" + path.value() + "'");
74+
}
75+
76+
private static void validateContextPath(String fullPath, String contextPath) {
77+
int length = contextPath.length();
78+
if (contextPath.charAt(0) != '/' || contextPath.charAt(length - 1) == '/') {
79+
throw new IllegalArgumentException("Invalid contextPath: '" + contextPath + "': " +
80+
"must start with '/' and not end with '/'");
81+
}
82+
if (!fullPath.startsWith(contextPath)) {
83+
throw new IllegalArgumentException("Invalid contextPath '" + contextPath + "': " +
84+
"must match the start of requestPath: '" + fullPath + "'");
85+
}
86+
if (fullPath.length() > length && fullPath.charAt(length) != '/') {
87+
throw new IllegalArgumentException("Invalid contextPath '" + contextPath + "': " +
88+
"must match to full path segments for requestPath: '" + fullPath + "'");
89+
}
7590
}
7691

7792
private static PathContainer extractPathWithinApplication(PathContainer fullPath, PathContainer contextPath) {

spring-web/src/main/java/org/springframework/http/server/RequestPath.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public interface RequestPath extends PathContainer {
4646

4747
/**
4848
* Return a new {@code RequestPath} instance with a modified context path.
49-
* The new context path must match the beginning of this request path.
49+
* The new context path must match 0 or more path segments at the start.
5050
* @param contextPath the new context path
5151
* @return a new {@code RequestPath} instance
5252
*/

spring-web/src/test/java/org/springframework/http/server/DefaultRequestPathTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,19 @@ private void testRequestPath(String fullPath, String contextPath, String pathWit
5959
assertEquals(pathWithinApplication, requestPath.pathWithinApplication().value());
6060
}
6161

62+
@Test
63+
public void updateRequestPath() throws Exception {
64+
65+
URI uri = URI.create("http://localhost:8080/aA/bB/cC");
66+
RequestPath requestPath = RequestPath.parse(uri, null);
67+
68+
assertEquals("", requestPath.contextPath().value());
69+
assertEquals("/aA/bB/cC", requestPath.pathWithinApplication().value());
70+
71+
requestPath = requestPath.modifyContextPath("/aA");
72+
73+
assertEquals("/aA", requestPath.contextPath().value());
74+
assertEquals("/bB/cC", requestPath.pathWithinApplication().value());
75+
}
76+
6277
}

0 commit comments

Comments
 (0)