Search in sources :

Example 66 with ServletResponse

use of javax.servlet.ServletResponse 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 67 with ServletResponse

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

the class LoginFilterTest method testBadSubject.

/**
     * Test with a bad subject - shouldn't call the filter chain, just returns.
     *
     * @throws IOException
     * @throws ServletException
     */
@Test
public void testBadSubject() throws IOException, ServletException {
    FilterConfig filterConfig = mock(FilterConfig.class);
    LoginFilter loginFilter = new LoginFilter();
    loginFilter.setSessionFactory(sessionFactory);
    try {
        loginFilter.init(filterConfig);
    } catch (ServletException e) {
        fail(e.getMessage());
    }
    HttpServletRequest servletRequest = new TestHttpServletRequest();
    servletRequest.setAttribute("ddf.security.securityToken", mock(SecurityToken.class));
    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            fail("Should not have continued down the filter chain without a valid Subject");
        }
    };
    loginFilter.doFilter(servletRequest, servletResponse, filterChain);
}
Also used : ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 68 with ServletResponse

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

the class DelegateServletFilterTest method testDoFilterWithFilters.

/**
     * Tests the main logic of performing the filter with adding filters.
     *
     * @throws ServletException
     * @throws IOException
     * @throws InvalidSyntaxException
     */
@Test
public void testDoFilterWithFilters() throws IOException, ServletException, InvalidSyntaxException {
    ServletRequest request = mock(HttpServletRequest.class);
    ServletResponse response = mock(HttpServletResponse.class);
    final BundleContext context = createMockContext(true);
    DelegateServletFilter filter = new DelegateServletFilter() {

        @Override
        protected BundleContext getContext() {
            return context;
        }
    };
    filter.doFilter(request, response, initialChain);
    verifyFiltersCalled(request, response, initialChain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 69 with ServletResponse

use of javax.servlet.ServletResponse 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 70 with ServletResponse

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

ServletResponse (javax.servlet.ServletResponse)155 ServletRequest (javax.servlet.ServletRequest)129 HttpServletResponse (javax.servlet.http.HttpServletResponse)104 HttpServletRequest (javax.servlet.http.HttpServletRequest)90 FilterChain (javax.servlet.FilterChain)81 Test (org.junit.Test)76 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)26 Injector (com.google.inject.Injector)25 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 FilterConfig (javax.servlet.FilterConfig)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14