|
15 | 15 | */
|
16 | 16 | package org.springframework.security.web.access;
|
17 | 17 |
|
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.assertj.core.api.Assertions.fail; |
| 21 | +import static org.mockito.Matchers.any; |
| 22 | +import static org.mockito.Mockito.doThrow; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.verifyZeroInteractions; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Locale; |
| 28 | + |
| 29 | +import javax.servlet.FilterChain; |
| 30 | +import javax.servlet.ServletException; |
| 31 | +import javax.servlet.http.HttpServletRequest; |
| 32 | +import javax.servlet.http.HttpServletResponse; |
| 33 | +import javax.servlet.http.HttpSession; |
| 34 | + |
18 | 35 | import org.junit.After;
|
19 | 36 | import org.junit.Before;
|
20 | 37 | import org.junit.Test;
|
|
36 | 53 | import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
37 | 54 | import org.springframework.security.web.savedrequest.SavedRequest;
|
38 | 55 |
|
39 |
| -import javax.servlet.FilterChain; |
40 |
| -import javax.servlet.ServletException; |
41 |
| -import javax.servlet.http.HttpServletRequest; |
42 |
| -import javax.servlet.http.HttpServletResponse; |
43 |
| -import javax.servlet.http.HttpSession; |
44 |
| -import java.io.IOException; |
45 |
| -import java.util.Locale; |
46 |
| - |
47 |
| -import static org.assertj.core.api.Assertions.assertThat; |
48 |
| -import static org.assertj.core.api.Assertions.fail; |
49 |
| -import static org.mockito.Matchers.any; |
50 |
| -import static org.mockito.Mockito.doThrow; |
51 |
| -import static org.mockito.Mockito.mock; |
52 |
| - |
53 | 56 | /**
|
54 | 57 | * Tests {@link ExceptionTranslationFilter}.
|
55 | 58 | *
|
@@ -302,7 +305,26 @@ public void thrownIOExceptionServletExceptionAndRuntimeExceptionsAreRethrown()
|
302 | 305 | }
|
303 | 306 | }
|
304 | 307 |
|
305 |
| - private final AuthenticationEntryPoint mockEntryPoint = new AuthenticationEntryPoint() { |
| 308 | + @Test |
| 309 | + public void doFilterWhenResponseCommittedThenRethrowsException() throws Exception { |
| 310 | + this.mockEntryPoint = mock(AuthenticationEntryPoint.class); |
| 311 | + FilterChain chain = (request, response) -> { |
| 312 | + HttpServletResponse httpResponse = (HttpServletResponse) response; |
| 313 | + httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST); |
| 314 | + throw new AccessDeniedException("Denied"); |
| 315 | + }; |
| 316 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 317 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 318 | + ExceptionTranslationFilter filter = new ExceptionTranslationFilter(mockEntryPoint); |
| 319 | + |
| 320 | + assertThatThrownBy(() -> filter.doFilter(request, response, chain)) |
| 321 | + .isInstanceOf(ServletException.class) |
| 322 | + .hasCauseInstanceOf(AccessDeniedException.class); |
| 323 | + |
| 324 | + verifyZeroInteractions(mockEntryPoint); |
| 325 | + } |
| 326 | + |
| 327 | + private AuthenticationEntryPoint mockEntryPoint = new AuthenticationEntryPoint() { |
306 | 328 | public void commence(HttpServletRequest request, HttpServletResponse response,
|
307 | 329 | AuthenticationException authException) throws IOException,
|
308 | 330 | ServletException {
|
|
0 commit comments