use of javax.servlet.FilterChain in project ddf by codice.
the class ProxyFilterChainTest method testDoFilter.
/**
* Tests that all of the filters are properly called.
*
* @throws ServletException
* @throws IOException
*/
@Test
public void testDoFilter() throws IOException, ServletException {
FilterChain initialChain = mock(FilterChain.class);
ProxyFilterChain proxyChain = new ProxyFilterChain(initialChain);
Filter filter1 = createMockFilter("filter1");
Filter filter2 = createMockFilter("filter2");
Filter filter3 = createMockFilter("filter3");
ServletRequest request = mock(ServletRequest.class);
ServletResponse response = mock(ServletResponse.class);
proxyChain.addFilter(filter1);
proxyChain.addFilters(Arrays.asList(filter2, filter3));
proxyChain.doFilter(request, response);
// Verify that all of the filters were called once.
verify(filter1).doFilter(request, response, proxyChain);
verify(filter2).doFilter(request, response, proxyChain);
verify(filter3).doFilter(request, response, proxyChain);
// the initial chain should have also been called once (at the end).
verify(initialChain).doFilter(request, response);
}
use of javax.servlet.FilterChain in project ddf by codice.
the class ProxyFilterChainTest method testAddFilterAfterDo.
/**
* Tests that an exception is thrown if a new filter is attempted to be
* added after the filter has been run.
*
* @throws IOException
* @throws ServletException
*/
@Test(expected = IllegalStateException.class)
public void testAddFilterAfterDo() throws IOException, ServletException {
FilterChain initialChain = mock(FilterChain.class);
ProxyFilterChain proxyChain = new ProxyFilterChain(initialChain);
Filter filter1 = mock(Filter.class);
proxyChain.doFilter(mock(ServletRequest.class), mock(ServletResponse.class));
proxyChain.addFilter(filter1);
}
use of javax.servlet.FilterChain in project ddf by codice.
the class ProxyFilterChainTest method createMockFilter.
private Filter createMockFilter(final String name) throws IOException, ServletException {
Filter mockFilter = mock(Filter.class);
Mockito.when(mockFilter.toString()).thenReturn(name);
Mockito.doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
LOGGER.debug("{} was called.", name);
((FilterChain) args[2]).doFilter(((ServletRequest) args[0]), ((ServletResponse) args[1]));
return null;
}
}).when(mockFilter).doFilter(any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class));
return mockFilter;
}
use of javax.servlet.FilterChain in project ddf by codice.
the class ProxyFilterChainTest method testAddFiltersAfterDo.
/**
* Tests that an exception is thrown if more filters are attempted to be
* added after the filter has been run.
*
* @throws IOException
* @throws ServletException
*/
@Test(expected = IllegalStateException.class)
public void testAddFiltersAfterDo() throws IOException, ServletException {
FilterChain initialChain = mock(FilterChain.class);
ProxyFilterChain proxyChain = new ProxyFilterChain(initialChain);
Filter filter2 = mock(Filter.class);
Filter filter3 = mock(Filter.class);
proxyChain.doFilter(mock(ServletRequest.class), mock(ServletResponse.class));
proxyChain.addFilters(Arrays.asList(filter2, filter3));
}
use of javax.servlet.FilterChain in project spring-security-oauth by spring-projects.
the class OAuthProcessingFilterTests method testDoFilter.
/**
* tests do filter.
*/
@Test
public void testDoFilter() throws Exception {
final boolean[] triggers = new boolean[2];
Arrays.fill(triggers, false);
OAuthProviderProcessingFilter filter = new OAuthProviderProcessingFilter() {
@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
return true;
}
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(null, null);
}
@Override
protected void validateOAuthParams(ConsumerDetails consumerDetails, Map<String, String> oauthParams) throws InvalidOAuthParametersException {
triggers[0] = true;
}
@Override
protected void validateSignature(ConsumerAuthentication authentication) throws AuthenticationException {
triggers[1] = true;
}
@Override
protected void fail(HttpServletRequest request, HttpServletResponse response, AuthenticationException failure) throws IOException, ServletException {
throw failure;
}
@Override
protected Object createDetails(HttpServletRequest request, ConsumerDetails consumerDetails) {
return null;
}
@Override
protected void resetPreviousAuthentication(Authentication previousAuthentication) {
// no-op
}
@Override
protected boolean skipProcessing(HttpServletRequest request) {
return false;
}
};
filter.setProviderSupport(providerSupport);
filter.setConsumerDetailsService(consumerDetailsService);
filter.setNonceServices(nonceServices);
filter.setSignatureMethodFactory(signatureFactory);
filter.setTokenServices(tokenServices);
when(request.getMethod()).thenReturn("DELETE");
filter.doFilter(request, response, filterChain);
verify(response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
assertFalse(triggers[0]);
assertFalse(triggers[1]);
Arrays.fill(triggers, false);
when(request.getMethod()).thenReturn("GET");
HashMap<String, String> requestParams = new HashMap<String, String>();
when(providerSupport.parseParameters(request)).thenReturn(requestParams);
try {
filter.doFilter(request, response, filterChain);
fail("should have required a consumer key.");
} catch (InvalidOAuthParametersException e) {
assertFalse(triggers[0]);
assertFalse(triggers[1]);
Arrays.fill(triggers, false);
}
when(request.getMethod()).thenReturn("GET");
requestParams = new HashMap<String, String>();
requestParams.put(OAuthConsumerParameter.oauth_consumer_key.toString(), "consumerKey");
when(providerSupport.parseParameters(request)).thenReturn(requestParams);
ConsumerDetails consumerDetails = mock(ConsumerDetails.class);
when(consumerDetails.getAuthorities()).thenReturn(new ArrayList<GrantedAuthority>());
when(consumerDetailsService.loadConsumerByConsumerKey("consumerKey")).thenReturn(consumerDetails);
requestParams.put(OAuthConsumerParameter.oauth_token.toString(), "tokenvalue");
requestParams.put(OAuthConsumerParameter.oauth_signature_method.toString(), "methodvalue");
requestParams.put(OAuthConsumerParameter.oauth_signature.toString(), "signaturevalue");
when(providerSupport.getSignatureBaseString(request)).thenReturn("sigbasestring");
filter.doFilter(request, response, filterChain);
verify(filterChain).doFilter(null, null);
verify(request).setAttribute(OAuthProviderProcessingFilter.OAUTH_PROCESSING_HANDLED, Boolean.TRUE);
ConsumerAuthentication authentication = (ConsumerAuthentication) SecurityContextHolder.getContext().getAuthentication();
assertSame(consumerDetails, authentication.getConsumerDetails());
assertEquals("tokenvalue", authentication.getConsumerCredentials().getToken());
assertEquals("methodvalue", authentication.getConsumerCredentials().getSignatureMethod());
assertEquals("signaturevalue", authentication.getConsumerCredentials().getSignature());
assertEquals("sigbasestring", authentication.getConsumerCredentials().getSignatureBaseString());
assertEquals("consumerKey", authentication.getConsumerCredentials().getConsumerKey());
assertTrue(authentication.isSignatureValidated());
SecurityContextHolder.getContext().setAuthentication(null);
assertTrue(triggers[0]);
assertTrue(triggers[1]);
Arrays.fill(triggers, false);
}
Aggregations