Skip to content

Commit 98686a5

Browse files
committed
Standardize Mock Request Paths
Closes gh-17449
1 parent d869686 commit 98686a5

File tree

64 files changed

+399
-721
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+399
-721
lines changed

cas/spring-security-cas.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies {
1414

1515
provided 'jakarta.servlet:jakarta.servlet-api'
1616

17+
testImplementation project(path : ':spring-security-web', configuration : 'tests')
1718
testImplementation "org.assertj:assertj-core"
1819
testImplementation "org.junit.jupiter:junit-jupiter-api"
1920
testImplementation "org.junit.jupiter:junit-jupiter-params"

cas/src/test/java/org/springframework/security/cas/web/CasAuthenticationFilterTests.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import static org.mockito.Mockito.verify;
5656
import static org.mockito.Mockito.verifyNoInteractions;
5757
import static org.mockito.Mockito.verifyNoMoreInteractions;
58+
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.get;
59+
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.post;
5860

5961
/**
6062
* Tests {@link CasAuthenticationFilter}.
@@ -79,9 +81,7 @@ public void testGettersSetters() {
7981

8082
@Test
8183
public void testNormalOperation() throws Exception {
82-
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/login/cas");
83-
request.setServletPath("/login/cas");
84-
request.addParameter("ticket", "ST-0-ER94xMJmn6pha35CQRoZ");
84+
MockHttpServletRequest request = post("/login/cas").param("ticket", "ST-0-ER94xMJmn6pha35CQRoZ").build();
8585
CasAuthenticationFilter filter = new CasAuthenticationFilter();
8686
filter.setAuthenticationManager((a) -> a);
8787
assertThat(filter.requiresAuthentication(request, new MockHttpServletResponse())).isTrue();
@@ -104,24 +104,22 @@ public void testRequiresAuthenticationFilterProcessUrl() {
104104
String url = "/login/cas";
105105
CasAuthenticationFilter filter = new CasAuthenticationFilter();
106106
filter.setFilterProcessesUrl(url);
107-
MockHttpServletRequest request = new MockHttpServletRequest("POST", url);
107+
MockHttpServletRequest request = post(url).build();
108108
MockHttpServletResponse response = new MockHttpServletResponse();
109-
request.setServletPath(url);
110109
assertThat(filter.requiresAuthentication(request, response)).isTrue();
111110
}
112111

113112
@Test
114113
public void testRequiresAuthenticationProxyRequest() {
115114
CasAuthenticationFilter filter = new CasAuthenticationFilter();
116-
MockHttpServletRequest request = new MockHttpServletRequest();
115+
MockHttpServletRequest request = get("/pgtCallback").build();
117116
MockHttpServletResponse response = new MockHttpServletResponse();
118-
request.setServletPath("/pgtCallback");
119117
assertThat(filter.requiresAuthentication(request, response)).isFalse();
120118
filter.setProxyReceptorUrl(request.getServletPath());
121119
assertThat(filter.requiresAuthentication(request, response)).isFalse();
122120
filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
123121
assertThat(filter.requiresAuthentication(request, response)).isTrue();
124-
request.setServletPath("/other");
122+
request = get("/other").build();
125123
assertThat(filter.requiresAuthentication(request, response)).isFalse();
126124
}
127125

@@ -133,12 +131,10 @@ public void testRequiresAuthenticationAuthAll() {
133131
CasAuthenticationFilter filter = new CasAuthenticationFilter();
134132
filter.setFilterProcessesUrl(url);
135133
filter.setServiceProperties(properties);
136-
MockHttpServletRequest request = new MockHttpServletRequest("POST", url);
134+
MockHttpServletRequest request = post(url).build();
137135
MockHttpServletResponse response = new MockHttpServletResponse();
138-
request.setServletPath(url);
139136
assertThat(filter.requiresAuthentication(request, response)).isTrue();
140-
request = new MockHttpServletRequest("POST", "/other");
141-
request.setServletPath("/other");
137+
request = post("/other").build();
142138
assertThat(filter.requiresAuthentication(request, response)).isFalse();
143139
request.setParameter(properties.getArtifactParameter(), "value");
144140
assertThat(filter.requiresAuthentication(request, response)).isTrue();
@@ -156,9 +152,8 @@ public void testRequiresAuthenticationAuthAll() {
156152
@Test
157153
public void testAuthenticateProxyUrl() throws Exception {
158154
CasAuthenticationFilter filter = new CasAuthenticationFilter();
159-
MockHttpServletRequest request = new MockHttpServletRequest();
155+
MockHttpServletRequest request = get("/pgtCallback").build();
160156
MockHttpServletResponse response = new MockHttpServletResponse();
161-
request.setServletPath("/pgtCallback");
162157
filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
163158
filter.setProxyReceptorUrl(request.getServletPath());
164159
assertThat(filter.attemptAuthentication(request, response)).isNull();
@@ -172,9 +167,7 @@ public void testDoFilterAuthenticateAll() throws Exception {
172167
given(manager.authenticate(any(Authentication.class))).willReturn(authentication);
173168
ServiceProperties serviceProperties = new ServiceProperties();
174169
serviceProperties.setAuthenticateAllArtifacts(true);
175-
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/authenticate");
176-
request.setParameter("ticket", "ST-1-123");
177-
request.setServletPath("/authenticate");
170+
MockHttpServletRequest request = post("/authenticate").param("ticket", "ST-1-123").build();
178171
MockHttpServletResponse response = new MockHttpServletResponse();
179172
FilterChain chain = mock(FilterChain.class);
180173
CasAuthenticationFilter filter = new CasAuthenticationFilter();
@@ -200,10 +193,9 @@ public void testDoFilterAuthenticateAll() throws Exception {
200193
@Test
201194
public void testChainNotInvokedForProxyReceptor() throws Exception {
202195
CasAuthenticationFilter filter = new CasAuthenticationFilter();
203-
MockHttpServletRequest request = new MockHttpServletRequest();
196+
MockHttpServletRequest request = get("/pgtCallback").build();
204197
MockHttpServletResponse response = new MockHttpServletResponse();
205198
FilterChain chain = mock(FilterChain.class);
206-
request.setServletPath("/pgtCallback");
207199
filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
208200
filter.setProxyReceptorUrl(request.getServletPath());
209201
filter.doFilter(request, response, chain);
@@ -271,16 +263,14 @@ void successfulAuthenticationWhenSecurityContextHolderStrategySetThenUses() thro
271263
@Test
272264
public void requiresAuthenticationWhenProxyRequestMatcherThenMatches() {
273265
CasAuthenticationFilter filter = new CasAuthenticationFilter();
274-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/pgtCallback");
266+
MockHttpServletRequest request = get("/pgtCallback").build();
275267
MockHttpServletResponse response = new MockHttpServletResponse();
276-
request.setServletPath("/pgtCallback");
277268
assertThat(filter.requiresAuthentication(request, response)).isFalse();
278269
filter.setProxyReceptorMatcher(PathPatternRequestMatcher.withDefaults().matcher(request.getServletPath()));
279270
assertThat(filter.requiresAuthentication(request, response)).isFalse();
280271
filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
281272
assertThat(filter.requiresAuthentication(request, response)).isTrue();
282-
request.setRequestURI("/other");
283-
request.setServletPath("/other");
273+
request = get("/other").build();
284274
assertThat(filter.requiresAuthentication(request, response)).isFalse();
285275
}
286276

config/src/test/java/org/springframework/security/config/FilterChainProxyConfigTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static org.mockito.ArgumentMatchers.any;
4545
import static org.mockito.Mockito.mock;
4646
import static org.mockito.Mockito.verify;
47+
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.get;
4748

4849
/**
4950
* Tests {@link FilterChainProxy}.
@@ -143,13 +144,12 @@ private void checkPathAndFilterOrder(FilterChainProxy filterChainProxy) {
143144
}
144145

145146
private void doNormalOperation(FilterChainProxy filterChainProxy) throws Exception {
146-
MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
147-
request.setServletPath("/foo/secure/super/somefile.html");
147+
MockHttpServletRequest request = get("/foo/secure/super/somefile.html").build();
148148
MockHttpServletResponse response = new MockHttpServletResponse();
149149
FilterChain chain = mock(FilterChain.class);
150150
filterChainProxy.doFilter(request, response, chain);
151151
verify(chain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
152-
request.setServletPath("/a/path/which/doesnt/match/any/filter.html");
152+
request = get("/a/path/which/doesnt/match/any/filter.html").build();
153153
chain = mock(FilterChain.class);
154154
filterChainProxy.doFilter(request, response, chain);
155155
verify(chain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));

config/src/test/java/org/springframework/security/config/annotation/web/configurers/AuthorizeRequestsTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public class AuthorizeRequestsTests {
7777
public void setup() {
7878
this.servletContext = spy(MockServletContext.mvc());
7979
this.request = new MockHttpServletRequest(this.servletContext, "GET", "");
80-
this.request.setMethod("GET");
8180
this.response = new MockHttpServletResponse();
8281
this.chain = new MockFilterChain();
8382
}
@@ -111,10 +110,12 @@ public void postWhenPostDenyAllInLambdaThenRespondsWithForbidden() throws Except
111110
public void antMatchersPathVariables() throws Exception {
112111
loadConfig(AntPatchersPathVariables.class);
113112
this.request.setServletPath("/user/user");
113+
this.request.setRequestURI("/user/user");
114114
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
115115
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
116116
this.setup();
117117
this.request.setServletPath("/user/deny");
118+
this.request.setRequestURI("/user/deny");
118119
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
119120
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN);
120121
}
@@ -123,10 +124,12 @@ public void antMatchersPathVariables() throws Exception {
123124
@Test
124125
public void antMatchersPathVariablesCaseInsensitive() throws Exception {
125126
loadConfig(AntPatchersPathVariables.class);
127+
this.request.setRequestURI("/USER/user");
126128
this.request.setServletPath("/USER/user");
127129
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
128130
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
129131
this.setup();
132+
this.request.setRequestURI("/USER/deny");
130133
this.request.setServletPath("/USER/deny");
131134
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
132135
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN);
@@ -137,10 +140,12 @@ public void antMatchersPathVariablesCaseInsensitive() throws Exception {
137140
public void antMatchersPathVariablesCaseInsensitiveCamelCaseVariables() throws Exception {
138141
loadConfig(AntMatchersPathVariablesCamelCaseVariables.class);
139142
this.request.setServletPath("/USER/user");
143+
this.request.setRequestURI("/USER/user");
140144
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
141145
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
142146
this.setup();
143147
this.request.setServletPath("/USER/deny");
148+
this.request.setRequestURI("/USER/deny");
144149
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
145150
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN);
146151
}

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecurityLogoutTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
4040

4141
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.post;
4243

4344
/**
4445
* @author Rob Winch
@@ -48,8 +49,6 @@ public class HttpSecurityLogoutTests {
4849

4950
AnnotationConfigWebApplicationContext context;
5051

51-
MockHttpServletRequest request;
52-
5352
MockHttpServletResponse response;
5453

5554
MockFilterChain chain;
@@ -59,7 +58,6 @@ public class HttpSecurityLogoutTests {
5958

6059
@BeforeEach
6160
public void setup() {
62-
this.request = new MockHttpServletRequest("GET", "");
6361
this.response = new MockHttpServletResponse();
6462
this.chain = new MockFilterChain();
6563
}
@@ -77,11 +75,10 @@ public void clearAuthenticationFalse() throws Exception {
7775
loadConfig(ClearAuthenticationFalseConfig.class);
7876
SecurityContext currentContext = SecurityContextHolder.createEmptyContext();
7977
currentContext.setAuthentication(new TestingAuthenticationToken("user", "password", "ROLE_USER"));
80-
this.request.getSession()
78+
MockHttpServletRequest request = post("/logout").build();
79+
request.getSession()
8180
.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, currentContext);
82-
this.request.setMethod("POST");
83-
this.request.setServletPath("/logout");
84-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
81+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
8582
assertThat(currentContext.getAuthentication()).isNotNull();
8683
}
8784

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecurityRequestMatchersTests.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
import static org.assertj.core.api.Assertions.assertThat;
4747
import static org.springframework.security.config.Customizer.withDefaults;
48+
import static org.springframework.security.web.servlet.TestMockHttpServletRequests.get;
4849

4950
/**
5051
* @author Rob Winch
@@ -54,8 +55,6 @@ public class HttpSecurityRequestMatchersTests {
5455

5556
AnnotationConfigWebApplicationContext context;
5657

57-
MockHttpServletRequest request;
58-
5958
MockHttpServletResponse response;
6059

6160
MockFilterChain chain;
@@ -65,8 +64,6 @@ public class HttpSecurityRequestMatchersTests {
6564

6665
@BeforeEach
6766
public void setup() {
68-
this.request = new MockHttpServletRequest("GET", "");
69-
this.request.setMethod("GET");
7067
this.response = new MockHttpServletResponse();
7168
this.chain = new MockFilterChain();
7269
}
@@ -87,70 +84,64 @@ public void mvcMatcherGetFiltersNoUnsupportedMethodExceptionFromDummyRequest() {
8784
@Test
8885
public void requestMatchersMvcMatcherServletPath() throws Exception {
8986
loadConfig(RequestMatchersMvcMatcherServeltPathConfig.class);
90-
this.request.setServletPath("/spring");
91-
this.request.setRequestURI("/spring/path");
92-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
87+
MockHttpServletRequest request = get().requestUri(null, "/spring", "/path").build();
88+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
9389
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
9490
setup();
95-
this.request.setServletPath("");
96-
this.request.setRequestURI("/path");
97-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
91+
request = get().requestUri(null, "", "/path").build();
92+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
9893
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
9994
setup();
100-
this.request.setServletPath("/other");
101-
this.request.setRequestURI("/other/path");
102-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
95+
request = get().requestUri(null, "/other", "/path").build();
96+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
10397
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
10498
}
10599

106100
@Test
107101
public void requestMatcherWhensMvcMatcherServletPathInLambdaThenPathIsSecured() throws Exception {
108102
loadConfig(RequestMatchersMvcMatcherServletPathInLambdaConfig.class);
109-
this.request.setServletPath("/spring");
110-
this.request.setRequestURI("/spring/path");
111-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
103+
MockHttpServletRequest request = get().requestUri(null, "/spring", "/path").build();
104+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
112105
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
113106
setup();
114-
this.request.setServletPath("");
115-
this.request.setRequestURI("/path");
116-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
107+
request = get().requestUri(null, "", "/path").build();
108+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
117109
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
118110
setup();
119-
this.request.setServletPath("/other");
120-
this.request.setRequestURI("/other/path");
121-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
111+
request = get().requestUri(null, "/other", "/path").build();
112+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
122113
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
123114
}
124115

125116
@Test
126117
public void requestMatcherWhenMultiMvcMatcherInLambdaThenAllPathsAreDenied() throws Exception {
127118
loadConfig(MultiMvcMatcherInLambdaConfig.class);
128-
this.request.setRequestURI("/test-1");
129-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
119+
MockHttpServletRequest request = get("/test-1").build();
120+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
130121
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
131122
setup();
132-
this.request.setRequestURI("/test-2");
133-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
123+
request = get("/test-2").build();
124+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
134125
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
135126
setup();
136-
this.request.setRequestURI("/test-3");
137-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
127+
request = get("/test-3").build();
128+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
138129
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
139130
}
140131

141132
@Test
142133
public void requestMatcherWhenMultiMvcMatcherThenAllPathsAreDenied() throws Exception {
143134
loadConfig(MultiMvcMatcherConfig.class);
144-
this.request.setRequestURI("/test-1");
145-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
135+
MockHttpServletRequest request = get("/test-1").build();
136+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
146137
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
147138
setup();
148-
this.request.setRequestURI("/test-2");
149-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
139+
request = get("/test-2").build();
140+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
150141
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
151142
setup();
152-
this.request.setRequestURI("/test-3");
153-
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
143+
request = get("/test-3").build();
144+
this.springSecurityFilterChain.doFilter(request, this.response, this.chain);
154145
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
155146
}
156147

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersNoMvcTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class HttpSecuritySecurityMatchersNoMvcTests {
6767

6868
@BeforeEach
6969
public void setup() throws Exception {
70-
this.request = new MockHttpServletRequest("GET", "");
70+
this.request = new MockHttpServletRequest();
7171
this.request.setMethod("GET");
7272
this.response = new MockHttpServletResponse();
7373
this.chain = new MockFilterChain();
@@ -83,15 +83,15 @@ public void cleanup() {
8383
@Test
8484
public void securityMatcherWhenNoMvcThenAntMatcher() throws Exception {
8585
loadConfig(SecurityMatcherNoMvcConfig.class);
86-
this.request.setServletPath("/path");
86+
this.request.setRequestURI("/path");
8787
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
8888
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
8989
setup();
90-
this.request.setServletPath("/path.html");
90+
this.request.setRequestURI("/path.html");
9191
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
9292
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
9393
setup();
94-
this.request.setServletPath("/path/");
94+
this.request.setRequestURI("/path/");
9595
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
9696
List<RequestMatcher> requestMatchers = this.springSecurityFilterChain.getFilterChains()
9797
.stream()

0 commit comments

Comments
 (0)