1
1
/*
2
- * Copyright 2004-2020 the original author or authors.
2
+ * Copyright 2004-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
61
61
* Tests {@link ExceptionTranslationFilter}.
62
62
*
63
63
* @author Ben Alex
64
+ * @author Gengwu Zhao
64
65
*/
65
66
public class ExceptionTranslationFilterTests {
66
67
@@ -91,7 +92,7 @@ public void testAccessDeniedWhenAnonymous() throws Exception {
91
92
request .setContextPath ("/mycontext" );
92
93
request .setRequestURI ("/mycontext/secure/page.html" );
93
94
// Setup the FilterChain to thrown an access denied exception
94
- FilterChain fc = mockFilterChainWiehException (new AccessDeniedException ("" ));
95
+ FilterChain fc = mockFilterChainWithException (new AccessDeniedException ("" ));
95
96
// Setup SecurityContextHolder, as filter needs to check if user is
96
97
// anonymous
97
98
SecurityContextHolder .getContext ()
@@ -117,7 +118,7 @@ public void testAccessDeniedWithRememberMe() throws Exception {
117
118
request .setContextPath ("/mycontext" );
118
119
request .setRequestURI ("/mycontext/secure/page.html" );
119
120
// Setup the FilterChain to thrown an access denied exception
120
- FilterChain fc = mockFilterChainWiehException (new AccessDeniedException ("" ));
121
+ FilterChain fc = mockFilterChainWithException (new AccessDeniedException ("" ));
121
122
// Setup SecurityContextHolder, as filter needs to check if user is remembered
122
123
SecurityContext securityContext = SecurityContextHolder .createEmptyContext ();
123
124
securityContext .setAuthentication (
@@ -138,7 +139,7 @@ public void testAccessDeniedWhenNonAnonymous() throws Exception {
138
139
MockHttpServletRequest request = new MockHttpServletRequest ();
139
140
request .setServletPath ("/secure/page.html" );
140
141
// Setup the FilterChain to thrown an access denied exception
141
- FilterChain fc = mockFilterChainWiehException (new AccessDeniedException ("" ));
142
+ FilterChain fc = mockFilterChainWithException (new AccessDeniedException ("" ));
142
143
// Setup SecurityContextHolder, as filter needs to check if user is
143
144
// anonymous
144
145
SecurityContextHolder .clearContext ();
@@ -161,7 +162,7 @@ public void testLocalizedErrorMessages() throws Exception {
161
162
MockHttpServletRequest request = new MockHttpServletRequest ();
162
163
request .setServletPath ("/secure/page.html" );
163
164
// Setup the FilterChain to thrown an access denied exception
164
- FilterChain fc = mockFilterChainWiehException (new AccessDeniedException ("" ));
165
+ FilterChain fc = mockFilterChainWithException (new AccessDeniedException ("" ));
165
166
// Setup SecurityContextHolder, as filter needs to check if user is
166
167
// anonymous
167
168
SecurityContextHolder .getContext ()
@@ -190,7 +191,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWhenAuthentication
190
191
request .setContextPath ("/mycontext" );
191
192
request .setRequestURI ("/mycontext/secure/page.html" );
192
193
// Setup the FilterChain to thrown an authentication failure exception
193
- FilterChain fc = mockFilterChainWiehException (new BadCredentialsException ("" ));
194
+ FilterChain fc = mockFilterChainWithException (new BadCredentialsException ("" ));
194
195
// Test
195
196
RequestCache requestCache = new HttpSessionRequestCache ();
196
197
ExceptionTranslationFilter filter = new ExceptionTranslationFilter (this .mockEntryPoint , requestCache );
@@ -213,7 +214,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhen
213
214
request .setContextPath ("/mycontext" );
214
215
request .setRequestURI ("/mycontext/secure/page.html" );
215
216
// Setup the FilterChain to thrown an authentication failure exception
216
- FilterChain fc = mockFilterChainWiehException (new BadCredentialsException ("" ));
217
+ FilterChain fc = mockFilterChainWithException (new BadCredentialsException ("" ));
217
218
// Test
218
219
HttpSessionRequestCache requestCache = new HttpSessionRequestCache ();
219
220
ExceptionTranslationFilter filter = new ExceptionTranslationFilter (this .mockEntryPoint , requestCache );
@@ -253,7 +254,7 @@ public void thrownIOExceptionServletExceptionAndRuntimeExceptionsAreRethrown() t
253
254
filter .afterPropertiesSet ();
254
255
Exception [] exceptions = { new IOException (), new ServletException (), new RuntimeException () };
255
256
for (Exception exception : exceptions ) {
256
- FilterChain fc = mockFilterChainWiehException (exception );
257
+ FilterChain fc = mockFilterChainWithException (exception );
257
258
assertThatExceptionOfType (Exception .class )
258
259
.isThrownBy (() -> filter .doFilter (new MockHttpServletRequest (), new MockHttpServletResponse (), fc ))
259
260
.isSameAs (exception );
@@ -291,11 +292,14 @@ public void setMessageSourceWhenNotNullThenCanGet() {
291
292
filter .messages .getMessage (code );
292
293
verify (source ).getMessage (eq (code ), any (), any ());
293
294
}
294
- private FilterChain mockFilterChainWiehException (Object exception ) throws ServletException , IOException {
295
+
296
+ private FilterChain mockFilterChainWithException (Object exception ) throws ServletException , IOException {
295
297
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 ));
297
300
return fc ;
298
301
}
302
+
299
303
private AuthenticationEntryPoint mockEntryPoint = (request , response , authException ) -> response
300
304
.sendRedirect (request .getContextPath () + "/login.jsp" );
301
305
0 commit comments