Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public void testAccessDeniedWhenAnonymous() throws Exception {
request.setContextPath("/mycontext");
request.setRequestURI("/mycontext/secure/page.html");
// Setup the FilterChain to thrown an access denied exception
FilterChain fc = mock(FilterChain.class);
willThrow(new AccessDeniedException("")).given(fc)
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
// Setup SecurityContextHolder, as filter needs to check if user is
// anonymous
SecurityContextHolder.getContext()
Expand All @@ -119,9 +117,7 @@ public void testAccessDeniedWithRememberMe() throws Exception {
request.setContextPath("/mycontext");
request.setRequestURI("/mycontext/secure/page.html");
// Setup the FilterChain to thrown an access denied exception
FilterChain fc = mock(FilterChain.class);
willThrow(new AccessDeniedException("")).given(fc)
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
// Setup SecurityContextHolder, as filter needs to check if user is remembered
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
securityContext.setAuthentication(
Expand All @@ -142,9 +138,7 @@ public void testAccessDeniedWhenNonAnonymous() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServletPath("/secure/page.html");
// Setup the FilterChain to thrown an access denied exception
FilterChain fc = mock(FilterChain.class);
willThrow(new AccessDeniedException("")).given(fc)
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
// Setup SecurityContextHolder, as filter needs to check if user is
// anonymous
SecurityContextHolder.clearContext();
Expand All @@ -167,9 +161,7 @@ public void testLocalizedErrorMessages() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setServletPath("/secure/page.html");
// Setup the FilterChain to thrown an access denied exception
FilterChain fc = mock(FilterChain.class);
willThrow(new AccessDeniedException("")).given(fc)
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(new AccessDeniedException(""));
// Setup SecurityContextHolder, as filter needs to check if user is
// anonymous
SecurityContextHolder.getContext()
Expand Down Expand Up @@ -198,9 +190,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWhenAuthentication
request.setContextPath("/mycontext");
request.setRequestURI("/mycontext/secure/page.html");
// Setup the FilterChain to thrown an authentication failure exception
FilterChain fc = mock(FilterChain.class);
willThrow(new BadCredentialsException("")).given(fc)
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(new BadCredentialsException(""));
// Test
RequestCache requestCache = new HttpSessionRequestCache();
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint, requestCache);
Expand All @@ -223,9 +213,7 @@ public void redirectedToLoginFormAndSessionShowsOriginalTargetWithExoticPortWhen
request.setContextPath("/mycontext");
request.setRequestURI("/mycontext/secure/page.html");
// Setup the FilterChain to thrown an authentication failure exception
FilterChain fc = mock(FilterChain.class);
willThrow(new BadCredentialsException("")).given(fc)
.doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(new BadCredentialsException(""));
// Test
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
ExceptionTranslationFilter filter = new ExceptionTranslationFilter(this.mockEntryPoint, requestCache);
Expand Down Expand Up @@ -265,8 +253,7 @@ public void thrownIOExceptionServletExceptionAndRuntimeExceptionsAreRethrown() t
filter.afterPropertiesSet();
Exception[] exceptions = { new IOException(), new ServletException(), new RuntimeException() };
for (Exception exception : exceptions) {
FilterChain fc = mock(FilterChain.class);
willThrow(exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
FilterChain fc = mockFilterChainWiehException(exception);
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), fc))
.isSameAs(exception);
Expand Down Expand Up @@ -304,7 +291,11 @@ public void setMessageSourceWhenNotNullThenCanGet() {
filter.messages.getMessage(code);
verify(source).getMessage(eq(code), any(), any());
}

private FilterChain mockFilterChainWiehException(Object exception) throws ServletException, IOException {
FilterChain fc = mock(FilterChain.class);
willThrow((Throwable) exception).given(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
return fc;
}
private AuthenticationEntryPoint mockEntryPoint = (request, response, authException) -> response
.sendRedirect(request.getContextPath() + "/login.jsp");

Expand Down