Skip to content

Commit b1d6c1d

Browse files
committed
Use mockFilterChainWithException Issue gh-14768
1 parent 6558b78 commit b1d6c1d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

web/src/test/java/org/springframework/security/web/access/ExceptionTranslationFilterTests.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004-2020 the original author or authors.
2+
* Copyright 2004-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,6 +61,7 @@
6161
* Tests {@link ExceptionTranslationFilter}.
6262
*
6363
* @author Ben Alex
64+
* @author Gengwu Zhao
6465
*/
6566
public class ExceptionTranslationFilterTests {
6667

@@ -91,7 +92,7 @@ public void testAccessDeniedWhenAnonymous() throws Exception {
9192
request.setContextPath("/mycontext");
9293
request.setRequestURI("/mycontext/secure/page.html");
9394
// Setup the FilterChain to thrown an access denied exception
94-
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
95+
FilterChain fc = mockFilterChainWithException(new AccessDeniedException(""));
9596
// Setup SecurityContextHolder, as filter needs to check if user is
9697
// anonymous
9798
SecurityContextHolder.getContext()
@@ -117,7 +118,7 @@ public void testAccessDeniedWithRememberMe() throws Exception {
117118
request.setContextPath("/mycontext");
118119
request.setRequestURI("/mycontext/secure/page.html");
119120
// Setup the FilterChain to thrown an access denied exception
120-
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
121+
FilterChain fc = mockFilterChainWithException(new AccessDeniedException(""));
121122
// Setup SecurityContextHolder, as filter needs to check if user is remembered
122123
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
123124
securityContext.setAuthentication(
@@ -138,7 +139,7 @@ public void testAccessDeniedWhenNonAnonymous() throws Exception {
138139
MockHttpServletRequest request = new MockHttpServletRequest();
139140
request.setServletPath("/secure/page.html");
140141
// Setup the FilterChain to thrown an access denied exception
141-
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
142+
FilterChain fc = mockFilterChainWithException(new AccessDeniedException(""));
142143
// Setup SecurityContextHolder, as filter needs to check if user is
143144
// anonymous
144145
SecurityContextHolder.clearContext();
@@ -161,7 +162,7 @@ public void testLocalizedErrorMessages() throws Exception {
161162
MockHttpServletRequest request = new MockHttpServletRequest();
162163
request.setServletPath("/secure/page.html");
163164
// Setup the FilterChain to thrown an access denied exception
164-
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
165+
FilterChain fc = mockFilterChainWithException(new AccessDeniedException(""));
165166
// Setup SecurityContextHolder, as filter needs to check if user is
166167
// anonymous
167168
SecurityContextHolder.getContext()
@@ -190,7 +191,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWhenAuthentication
190191
request.setContextPath("/mycontext");
191192
request.setRequestURI("/mycontext/secure/page.html");
192193
// Setup the FilterChain to thrown an authentication failure exception
193-
FilterChain fc = mockFilterChainWiehException(new BadCredentialsException(""));
194+
FilterChain fc = mockFilterChainWithException(new BadCredentialsException(""));
194195
// Test
195196
RequestCache requestCache = new HttpSessionRequestCache();
196197
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint, requestCache);
@@ -213,7 +214,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhen
213214
request.setContextPath("/mycontext");
214215
request.setRequestURI("/mycontext/secure/page.html");
215216
// Setup the FilterChain to thrown an authentication failure exception
216-
FilterChain fc = mockFilterChainWiehException(new BadCredentialsException(""));
217+
FilterChain fc = mockFilterChainWithException(new BadCredentialsException(""));
217218
// Test
218219
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
219220
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint, requestCache);
@@ -253,7 +254,7 @@ public void thrownIOExceptionServletExceptionAndRuntimeExceptionsAreRethrown() t
253254
filter.afterPropertiesSet();
254255
Exception[] exceptions = { new IOException(), new ServletException(), new RuntimeException() };
255256
for (Exception exception : exceptions) {
256-
FilterChain fc = mockFilterChainWiehException(exception);
257+
FilterChain fc = mockFilterChainWithException(exception);
257258
assertThatExceptionOfType(Exception.class)
258259
.isThrownBy(() -> filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), fc))
259260
.isSameAs(exception);
@@ -291,11 +292,14 @@ public void setMessageSourceWhenNotNullThenCanGet() {
291292
filter.messages.getMessage(code);
292293
verify(source).getMessage(eq(code), any(), any());
293294
}
294-
private FilterChain mockFilterChainWiehException(Object exception) throws ServletException, IOException {
295+
296+
private FilterChain mockFilterChainWithException(Object exception) throws ServletException, IOException {
295297
FilterChain fc = mock(FilterChain.class);
296-
willThrow((Throwable) exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
298+
willThrow((Throwable) exception).given(fc)
299+
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
297300
return fc;
298301
}
302+
299303
private AuthenticationEntryPoint mockEntryPoint = (request, response, authException) -> response
300304
.sendRedirect(request.getContextPath() + "/login.jsp");
301305

0 commit comments

Comments
 (0)