Skip to content

Commit

Permalink
Refactoring in UrlHandlerFilterTests
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Sep 11, 2024
1 parent 7827188 commit 5671744
Showing 1 changed file with 26 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 5671744

Please sign in to comment.