UrlHandlerFilter ignores trailing slash in contextPath

Closes gh-33565
This commit is contained in:
rstoyanchev
2024-09-18 22:37:21 +01:00
parent e587753b1d
commit 2090ece62a
4 changed files with 18 additions and 12 deletions

View File

@@ -87,17 +87,20 @@ public class UrlHandlerFilterTests {
@Test
void noUrlHandling() throws Exception {
testNoUrlHandling("/path/**", "/path/123");
testNoUrlHandling("/path/*", "/path/123");
testNoUrlHandling("/**", "/"); // gh-33444
testNoUrlHandling("/path/**", "", "/path/123");
testNoUrlHandling("/path/*", "", "/path/123");
testNoUrlHandling("/**", "", "/"); // gh-33444
testNoUrlHandling("/**", "/myApp", "/myApp/"); // gh-33565
}
private static void testNoUrlHandling(String pattern, String requestURI) throws ServletException, IOException {
private static void testNoUrlHandling(String pattern, String contextPath, String requestURI)
throws ServletException, IOException {
// No request wrapping
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).wrapRequest().build();
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestURI);
request.setContextPath(contextPath);
MockFilterChain chain = new MockFilterChain();
filter.doFilterInternal(request, new MockHttpServletResponse(), chain);
@@ -109,6 +112,7 @@ public class UrlHandlerFilterTests {
filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build();
request = new MockHttpServletRequest("GET", requestURI);
request.setContextPath(contextPath);
MockHttpServletResponse response = new MockHttpServletResponse();
chain = new MockFilterChain();

View File

@@ -76,17 +76,18 @@ public class UrlHandlerFilterTests {
@Test
void noUrlHandling() {
testNoUrlHandling("/path/**", "/path/123");
testNoUrlHandling("/path/*", "/path/123");
testNoUrlHandling("/**", "/"); // gh-33444
testNoUrlHandling("/path/**", "", "/path/123");
testNoUrlHandling("/path/*", "", "/path/123");
testNoUrlHandling("/**", "", "/"); // gh-33444
testNoUrlHandling("/**", "/myApp", "/myApp/"); // gh-33565
}
private static void testNoUrlHandling(String pattern, String path) {
private static void testNoUrlHandling(String pattern, String contextPath, String path) {
// No request mutation
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).mutateRequest().build();
MockServerHttpRequest request = MockServerHttpRequest.get(path).build();
MockServerHttpRequest request = MockServerHttpRequest.get(path).contextPath(contextPath).build();
ServerWebExchange exchange = MockServerWebExchange.from(request);
ServerHttpRequest actual = invokeFilter(filter, exchange);
@@ -98,7 +99,7 @@ public class UrlHandlerFilterTests {
HttpStatus status = HttpStatus.PERMANENT_REDIRECT;
filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build();
request = MockServerHttpRequest.get(path).build();
request = MockServerHttpRequest.get(path).contextPath(contextPath).build();
exchange = MockServerWebExchange.from(request);
actual = invokeFilter(filter, exchange);