1
0
mirror of synced 2026-08-31 22:46:02 +00:00

Standardize Mocked Request Paths

Historically, Spring Security tests have set the servlet path
to indicate the path of a MockHttpServletRequest. This was needed
for AntPath and MvcRequestMatcher to correctly match the
specified request path.

This can leave MockHttpServletRequest in an inconsistent state
since requestURI is null while servletPath has a value.

For example, PathPatternRequestMatcher does not use the servlet path.
For tests to continue working both before and after the migration
from AntPath/MvcRequestMatcher to PathPatternRequestMatcher, the
mock requests should have a consistent representation of path
in getRequestURI and getServletPath.

This commit updates classes to use TestMockHttpServletRequests,
which ensures that the given path is applied to the servletPath and
requestURI, while also overriding with contextPath, servletPath,
and pathInfo when necessary.
This commit is contained in:
Josh Cummings
2025-06-30 12:24:16 -06:00
parent ef50ff29ad
commit 531c5cafdc
65 changed files with 553 additions and 721 deletions
@@ -9,7 +9,7 @@ dependencies {
implementation 'org.springframework:spring-context'
implementation 'org.springframework:spring-tx'
testImplementation project(':spring-security-web')
testImplementation project(path: ':spring-security-web', configuration: 'tests')
testImplementation 'jakarta.servlet:jakarta.servlet-api'
testImplementation 'org.springframework:spring-web'
testImplementation "org.assertj:assertj-core"
@@ -43,9 +43,7 @@ public class HttpNamespaceWithMultipleInterceptorsTests {
@Test
public void requestThatIsMatchedByDefaultInterceptorIsAllowed() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setServletPath("/somefile.html");
MockHttpServletRequest request = TestMockHttpServletRequests.get("/somefile.html").build();
request.setSession(createAuthenticatedSession("ROLE_0", "ROLE_1", "ROLE_2"));
MockHttpServletResponse response = new MockHttpServletResponse();
this.fcp.doFilter(request, response, new MockFilterChain());
@@ -54,10 +52,7 @@ public class HttpNamespaceWithMultipleInterceptorsTests {
@Test
public void securedUrlAccessIsRejectedWithoutRequiredRole() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setServletPath("/secure/somefile.html");
MockHttpServletRequest request = TestMockHttpServletRequests.get("/secure/somefile.html").build();
request.setSession(createAuthenticatedSession("ROLE_0"));
MockHttpServletResponse response = new MockHttpServletResponse();
this.fcp.doFilter(request, response, new MockFilterChain());