mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Merge branch '6.2.x'
This commit is contained in:
@@ -18,8 +18,11 @@ package org.springframework.web.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jakarta.servlet.DispatcherType;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.jspecify.annotations.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -29,6 +32,7 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.testfixture.servlet.MockFilterChain;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
||||
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
||||
import org.springframework.web.util.ServletRequestPathUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -124,4 +128,88 @@ public class UrlHandlerFilterTests {
|
||||
assertThat(response.isCommitted()).isFalse();
|
||||
}
|
||||
|
||||
@Test // gh-35538
|
||||
void shouldNotFilterErrorAndAsyncDispatches() {
|
||||
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/path/**").wrapRequest().build();
|
||||
|
||||
assertThat(filter.shouldNotFilterAsyncDispatch())
|
||||
.as("Should not filter ASYNC dispatch as wrapped request is reused")
|
||||
.isTrue();
|
||||
|
||||
assertThat(filter.shouldNotFilterErrorDispatch())
|
||||
.as("Should not filter ERROR dispatch as it's an internal, fixed path")
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test // gh-35538
|
||||
void shouldNotCacheParsedPath() throws Exception {
|
||||
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/path/*").wrapRequest().build();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/123/");
|
||||
request.setServletPath("/path/123/");
|
||||
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
filter.doFilterInternal(request, new MockHttpServletResponse(), chain);
|
||||
|
||||
assertThat(ServletRequestPathUtils.hasParsedRequestPath(request))
|
||||
.as("Path with trailing slash should not be cached")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test // gh-35538
|
||||
void shouldClearPreviouslyCachedPath() throws Exception {
|
||||
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/path/*").wrapRequest().build();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path/123/");
|
||||
request.setServletPath("/path/123/");
|
||||
|
||||
ServletRequestPathUtils.parseAndCache(request);
|
||||
assertThat(ServletRequestPathUtils.getParsedRequestPath(request).value()).isEqualTo("/path/123/");
|
||||
|
||||
PathServlet servlet = new PathServlet();
|
||||
MockFilterChain chain = new MockFilterChain(servlet);
|
||||
filter.doFilterInternal(request, new MockHttpServletResponse(), chain);
|
||||
|
||||
assertThat(servlet.getParsedPath()).isNull();
|
||||
}
|
||||
|
||||
@Test // gh-35509
|
||||
void shouldRespectForwardedPath() throws Exception {
|
||||
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler("/requestURI/*").wrapRequest().build();
|
||||
|
||||
String requestURI = "/requestURI/123/";
|
||||
MockHttpServletRequest originalRequest = new MockHttpServletRequest("GET", requestURI);
|
||||
originalRequest.setServletPath(requestURI);
|
||||
|
||||
MockFilterChain chain = new MockFilterChain();
|
||||
filter.doFilterInternal(originalRequest, new MockHttpServletResponse(), chain);
|
||||
|
||||
HttpServletRequest wrapped = (HttpServletRequest) chain.getRequest();
|
||||
assertThat(wrapped).isNotNull().isNotSameAs(originalRequest);
|
||||
assertThat(wrapped.getRequestURI()).isEqualTo("/requestURI/123");
|
||||
|
||||
// Change dispatcher type of underlying requests
|
||||
originalRequest.setDispatcherType(DispatcherType.FORWARD);
|
||||
assertThat(wrapped.getRequestURI())
|
||||
.as("Should delegate to underlying request for the requestURI on FORWARD")
|
||||
.isEqualTo(requestURI);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class PathServlet extends HttpServlet {
|
||||
|
||||
private String parsedPath;
|
||||
|
||||
public String getParsedPath() {
|
||||
return parsedPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||
this.parsedPath = (ServletRequestPathUtils.hasParsedRequestPath(request) ?
|
||||
ServletRequestPathUtils.getParsedRequestPath(request).value() : null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user