SEC-1998: Async tests with SecurityContextHolderAwareReqeustFilter
This commit is contained in:
parent
c8d45397fe
commit
9c4563285e
|
@ -140,6 +140,11 @@ final class HttpServlet3RequestFactory implements HttpServletRequestFactory {
|
||||||
this.response = response;
|
this.response = response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AsyncContext getAsyncContext() {
|
||||||
|
AsyncContext asyncContext = super.getAsyncContext();
|
||||||
|
return new SecurityContextAsyncContext(asyncContext);
|
||||||
|
}
|
||||||
|
|
||||||
public AsyncContext startAsync() {
|
public AsyncContext startAsync() {
|
||||||
AsyncContext startAsync = super.startAsync();
|
AsyncContext startAsync = super.startAsync();
|
||||||
return new SecurityContextAsyncContext(startAsync);
|
return new SecurityContextAsyncContext(startAsync);
|
||||||
|
|
|
@ -248,6 +248,72 @@ public class SecurityContextHolderAwareRequestFilterTests {
|
||||||
verifyZeroInteractions(authenticationEntryPoint, authenticationManager, logoutHandler);
|
verifyZeroInteractions(authenticationEntryPoint, authenticationManager, logoutHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAsyncContextStart() throws Exception {
|
||||||
|
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||||
|
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||||
|
TestingAuthenticationToken expectedAuth = new TestingAuthenticationToken("user", "password","ROLE_USER");
|
||||||
|
context.setAuthentication(expectedAuth);
|
||||||
|
SecurityContextHolder.setContext(context);
|
||||||
|
AsyncContext asyncContext = mock(AsyncContext.class);
|
||||||
|
when(request.getAsyncContext()).thenReturn(asyncContext);
|
||||||
|
Runnable runnable = new Runnable() {
|
||||||
|
public void run() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
wrappedRequest().getAsyncContext().start(runnable);
|
||||||
|
|
||||||
|
verifyZeroInteractions(authenticationManager, logoutHandler);
|
||||||
|
verify(asyncContext).start(runnableCaptor.capture());
|
||||||
|
DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
|
||||||
|
assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, SecurityContext.class)).isEqualTo(context);
|
||||||
|
assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, Runnable.class)).isEqualTo(runnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void startAsyncStart() throws Exception {
|
||||||
|
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||||
|
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||||
|
TestingAuthenticationToken expectedAuth = new TestingAuthenticationToken("user", "password","ROLE_USER");
|
||||||
|
context.setAuthentication(expectedAuth);
|
||||||
|
SecurityContextHolder.setContext(context);
|
||||||
|
AsyncContext asyncContext = mock(AsyncContext.class);
|
||||||
|
when(request.startAsync()).thenReturn(asyncContext);
|
||||||
|
Runnable runnable = new Runnable() {
|
||||||
|
public void run() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
wrappedRequest().startAsync().start(runnable);
|
||||||
|
|
||||||
|
verifyZeroInteractions(authenticationManager, logoutHandler);
|
||||||
|
verify(asyncContext).start(runnableCaptor.capture());
|
||||||
|
DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
|
||||||
|
assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, SecurityContext.class)).isEqualTo(context);
|
||||||
|
assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, Runnable.class)).isEqualTo(runnable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void startAsyncWithRequestResponseStart() throws Exception {
|
||||||
|
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||||
|
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||||
|
TestingAuthenticationToken expectedAuth = new TestingAuthenticationToken("user", "password","ROLE_USER");
|
||||||
|
context.setAuthentication(expectedAuth);
|
||||||
|
SecurityContextHolder.setContext(context);
|
||||||
|
AsyncContext asyncContext = mock(AsyncContext.class);
|
||||||
|
when(request.startAsync(request,response)).thenReturn(asyncContext);
|
||||||
|
Runnable runnable = new Runnable() {
|
||||||
|
public void run() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
wrappedRequest().startAsync(request, response).start(runnable);
|
||||||
|
|
||||||
|
verifyZeroInteractions(authenticationManager, logoutHandler);
|
||||||
|
verify(asyncContext).start(runnableCaptor.capture());
|
||||||
|
DelegatingSecurityContextRunnable wrappedRunnable = (DelegatingSecurityContextRunnable) runnableCaptor.getValue();
|
||||||
|
assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, SecurityContext.class)).isEqualTo(context);
|
||||||
|
assertThat(WhiteboxImpl.getInternalState(wrappedRunnable, Runnable.class)).isEqualTo(runnable);
|
||||||
|
}
|
||||||
|
|
||||||
private HttpServletRequest wrappedRequest() throws Exception {
|
private HttpServletRequest wrappedRequest() throws Exception {
|
||||||
filter.doFilter(request, response, filterChain);
|
filter.doFilter(request, response, filterChain);
|
||||||
verify(filterChain).doFilter(requestCaptor.capture(), any(HttpServletResponse.class));
|
verify(filterChain).doFilter(requestCaptor.capture(), any(HttpServletResponse.class));
|
||||||
|
|
Loading…
Reference in New Issue