Search in sources :

Example 41 with Filter

use of javax.servlet.Filter in project sling by apache.

the class LogTracerTest method noTurboFilterRegisteredUnlessTracingRequested.

@Test
public void noTurboFilterRegisteredUnlessTracingRequested() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    activateTracer();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNull(context.getService(TurboFilter.class));
        }
    };
    Filter filter = getFilter(false);
    filter.doFilter(request, response, chain);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 42 with Filter

use of javax.servlet.Filter in project ddf by codice.

the class DelegateServletFilterTest method createMockContext.

private BundleContext createMockContext(boolean includeFilters) throws InvalidSyntaxException, IOException, ServletException {
    BundleContext context = mock(BundleContext.class);
    List<Filter> mockFilters = mockFilters(includeFilters);
    List<ServiceReference<Filter>> referenceList = new ArrayList<ServiceReference<Filter>>();
    for (Filter curFilter : mockFilters) {
        ServiceReference<Filter> mockRef = mock(ServiceReference.class);
        when(context.getService(mockRef)).thenReturn(curFilter);
        referenceList.add(mockRef);
    }
    when(context.getServiceReferences(Filter.class, null)).thenReturn(referenceList);
    return context;
}
Also used : Filter(javax.servlet.Filter) ArrayList(java.util.ArrayList) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 43 with Filter

use of javax.servlet.Filter 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 44 with Filter

use of javax.servlet.Filter 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 45 with Filter

use of javax.servlet.Filter 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)

Aggregations

Filter (javax.servlet.Filter)185 FilterChain (javax.servlet.FilterChain)67 Test (org.junit.Test)57 HttpServletRequest (javax.servlet.http.HttpServletRequest)53 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 ServletRequest (javax.servlet.ServletRequest)43 ServletResponse (javax.servlet.ServletResponse)43 ServletException (javax.servlet.ServletException)28 FilterConfig (javax.servlet.FilterConfig)25 IOException (java.io.IOException)23 ServletContext (javax.servlet.ServletContext)20 Injector (com.google.inject.Injector)17 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)16 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Map (java.util.Map)9 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)9 ArrayList (java.util.ArrayList)8 Hashtable (java.util.Hashtable)8 DispatcherType (javax.servlet.DispatcherType)8