diff --git a/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java index f8dec6ba1f3a..d3d1ee73bbba 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java @@ -40,14 +40,13 @@ public class UrlHandlerFilterTests { @Test - void trailingSlashWithRequestWrapping() throws Exception { - testTrailingSlashWithRequestWrapping("/path/**", "/path/123", null); - testTrailingSlashWithRequestWrapping("/path/*", "/path", "/123"); - testTrailingSlashWithRequestWrapping("/path/*", "", "/path/123"); + void requestWrapping() throws Exception { + testRequestWrapping("/path/**", "/path/123", null); + testRequestWrapping("/path/*", "/path", "/123"); + testRequestWrapping("/path/*", "", "/path/123"); } - void testTrailingSlashWithRequestWrapping( - String pattern, String servletPath, @Nullable String pathInfo) throws Exception { + void testRequestWrapping(String pattern, String servletPath, @Nullable String pathInfo) throws Exception { UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).wrapRequest().build(); @@ -70,31 +69,32 @@ void testTrailingSlashWithRequestWrapping( } @Test - void shouldNotSkipTrailingSlashForRootPath() throws Exception { - UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/**").wrapRequest().build(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); - request.setPathInfo("/"); + void redirect() throws Exception { + HttpStatus status = HttpStatus.PERMANENT_REDIRECT; + UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/path/*").redirect(status).build(); + + String path = "/path/123"; + MockHttpServletResponse response = new MockHttpServletResponse(); MockFilterChain chain = new MockFilterChain(); - filter.doFilterInternal(request, new MockHttpServletResponse(), chain); + filter.doFilterInternal(new MockHttpServletRequest("GET", path + "/"), response, chain); - HttpServletRequest actual = (HttpServletRequest) chain.getRequest(); - assertThat(actual).isNotNull().isSameAs(request); - assertThat(actual.getRequestURI()).isEqualTo("/"); - assertThat(actual.getRequestURL().toString()).isEqualTo("http://localhost/"); - assertThat(actual.getServletPath()).isEqualTo(""); - assertThat(actual.getPathInfo()).isEqualTo("/"); + assertThat(chain.getRequest()).isNull(); + assertThat(response.getStatus()).isEqualTo(status.value()); + assertThat(response.getHeader(HttpHeaders.LOCATION)).isEqualTo(path); + assertThat(response.isCommitted()).isTrue(); } @Test - void noTrailingSlashWithRequestWrapping() throws Exception { - testNoTrailingSlashWithRequestWrapping("/path/**", "/path/123"); - testNoTrailingSlashWithRequestWrapping("/path/*", "/path/123"); + void noUrlHandling() throws Exception { + testNoUrlHandling("/path/**", "/path/123"); + testNoUrlHandling("/path/*", "/path/123"); + testNoUrlHandling("/**", "/"); // gh-33444 } - private static void testNoTrailingSlashWithRequestWrapping( - String pattern, String requestURI) throws ServletException, IOException { + private static void testNoUrlHandling(String pattern, String requestURI) throws ServletException, IOException { + // No request wrapping UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).wrapRequest().build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", requestURI); @@ -103,34 +103,15 @@ private static void testNoTrailingSlashWithRequestWrapping( HttpServletRequest actual = (HttpServletRequest) chain.getRequest(); assertThat(actual).as("Request should not be wrapped").isSameAs(request); - } - - @Test - void trailingSlashHandlerWithRedirect() throws Exception { - HttpStatus status = HttpStatus.PERMANENT_REDIRECT; - UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/path/*").redirect(status).build(); - - String path = "/path/123"; - MockHttpServletResponse response = new MockHttpServletResponse(); - - MockFilterChain chain = new MockFilterChain(); - filter.doFilterInternal(new MockHttpServletRequest("GET", path + "/"), response, chain); - - assertThat(chain.getRequest()).isNull(); - assertThat(response.getStatus()).isEqualTo(status.value()); - assertThat(response.getHeader(HttpHeaders.LOCATION)).isEqualTo(path); - assertThat(response.isCommitted()).isTrue(); - } - @Test - void noTrailingSlashWithRedirect() throws Exception { + // No redirect HttpStatus status = HttpStatus.PERMANENT_REDIRECT; - UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/path/*").redirect(status).build(); + filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build(); - MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/123"); + request = new MockHttpServletRequest("GET", requestURI); MockHttpServletResponse response = new MockHttpServletResponse(); - MockFilterChain chain = new MockFilterChain(); + chain = new MockFilterChain(); filter.doFilterInternal(request, response, chain); assertThat(chain.getRequest()).isSameAs(request);