Add RequestPath tests for modifying the contextPath

This commit is contained in:
Rossen Stoyanchev 2017-07-28 12:25:30 +02:00
parent 6855a85c41
commit dbe25cf717
3 changed files with 35 additions and 5 deletions

View File

@ -55,8 +55,7 @@ class DefaultRequestPath implements RequestPath {
return PathContainer.parseUrlPath("");
}
Assert.isTrue(contextPath.startsWith("/") && !contextPath.endsWith("/") &&
path.value().startsWith(contextPath), "Invalid contextPath: " + contextPath);
validateContextPath(path.value(), contextPath);
int length = contextPath.length();
int counter = 0;
@ -70,8 +69,24 @@ class DefaultRequestPath implements RequestPath {
}
// Should not happen..
throw new IllegalStateException("Failed to initialize contextPath='" + contextPath + "'" +
" given path='" + path.value() + "'");
throw new IllegalStateException("Failed to initialize contextPath '" + contextPath + "'" +
" for requestPath '" + path.value() + "'");
}
private static void validateContextPath(String fullPath, String contextPath) {
int length = contextPath.length();
if (contextPath.charAt(0) != '/' || contextPath.charAt(length - 1) == '/') {
throw new IllegalArgumentException("Invalid contextPath: '" + contextPath + "': " +
"must start with '/' and not end with '/'");
}
if (!fullPath.startsWith(contextPath)) {
throw new IllegalArgumentException("Invalid contextPath '" + contextPath + "': " +
"must match the start of requestPath: '" + fullPath + "'");
}
if (fullPath.length() > length && fullPath.charAt(length) != '/') {
throw new IllegalArgumentException("Invalid contextPath '" + contextPath + "': " +
"must match to full path segments for requestPath: '" + fullPath + "'");
}
}
private static PathContainer extractPathWithinApplication(PathContainer fullPath, PathContainer contextPath) {

View File

@ -46,7 +46,7 @@ public interface RequestPath extends PathContainer {
/**
* Return a new {@code RequestPath} instance with a modified context path.
* The new context path must match the beginning of this request path.
* The new context path must match 0 or more path segments at the start.
* @param contextPath the new context path
* @return a new {@code RequestPath} instance
*/

View File

@ -59,4 +59,19 @@ public class DefaultRequestPathTests {
assertEquals(pathWithinApplication, requestPath.pathWithinApplication().value());
}
@Test
public void updateRequestPath() throws Exception {
URI uri = URI.create("http://localhost:8080/aA/bB/cC");
RequestPath requestPath = RequestPath.parse(uri, null);
assertEquals("", requestPath.contextPath().value());
assertEquals("/aA/bB/cC", requestPath.pathWithinApplication().value());
requestPath = requestPath.modifyContextPath("/aA");
assertEquals("/aA", requestPath.contextPath().value());
assertEquals("/bB/cC", requestPath.pathWithinApplication().value());
}
}