Search in sources :

Example 86 with FilterChain

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);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 87 with FilterChain

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);
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 88 with FilterChain

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;
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain)

Example 89 with FilterChain

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));
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 90 with FilterChain

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);
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) HashMap(java.util.HashMap) FilterChain(javax.servlet.FilterChain) GrantedAuthority(org.springframework.security.core.GrantedAuthority) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) InvalidOAuthParametersException(org.springframework.security.oauth.provider.InvalidOAuthParametersException) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) Authentication(org.springframework.security.core.Authentication) ConsumerAuthentication(org.springframework.security.oauth.provider.ConsumerAuthentication) HashMap(java.util.HashMap) Map(java.util.Map) ConsumerDetails(org.springframework.security.oauth.provider.ConsumerDetails) Test(org.junit.Test)

Aggregations

FilterChain (javax.servlet.FilterChain)418 HttpServletRequest (javax.servlet.http.HttpServletRequest)317 HttpServletResponse (javax.servlet.http.HttpServletResponse)269 Test (org.junit.Test)246 ServletResponse (javax.servlet.ServletResponse)135 ServletRequest (javax.servlet.ServletRequest)118 FilterConfig (javax.servlet.FilterConfig)80 Filter (javax.servlet.Filter)68 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)48 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)46 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Injector (com.google.inject.Injector)32 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)25 ServletContext (javax.servlet.ServletContext)25 Test (org.testng.annotations.Test)25 HttpSession (javax.servlet.http.HttpSession)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)22 Properties (java.util.Properties)19