Search in sources :

Example 46 with FilterChain

use of javax.servlet.FilterChain in project spring-boot by spring-projects.

the class MetricFilterAutoConfigurationTests method additionallyRecordsMetricsWithHttpMethodNameIfConfigured.

@Test
public void additionallyRecordsMetricsWithHttpMethodNameIfConfigured() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class, MetricFilterAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(context, "endpoints.metrics.filter.gauge-submissions=merged,per-http-method", "endpoints.metrics.filter.counter-submissions=merged,per-http-method");
    context.refresh();
    Filter filter = context.getBean(Filter.class);
    final MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/test/path");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    willAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            response.setStatus(200);
            return null;
        }
    }).given(chain).doFilter(request, response);
    filter.doFilter(request, response, chain);
    verify(context.getBean(GaugeService.class)).submit(eq("response.test.path"), anyDouble());
    verify(context.getBean(GaugeService.class)).submit(eq("response.PUT.test.path"), anyDouble());
    verify(context.getBean(CounterService.class)).increment(eq("status.200.test.path"));
    verify(context.getBean(CounterService.class)).increment(eq("status.PUT.200.test.path"));
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 47 with FilterChain

use of javax.servlet.FilterChain in project spring-security by spring-projects.

the class OpenIDAuthenticationFilterTests method testFilterOperation.

@Test
public void testFilterOperation() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setServletPath(REQUEST_PATH);
    req.setRequestURI(REQUEST_PATH);
    req.setServerPort(8080);
    MockHttpServletResponse response = new MockHttpServletResponse();
    req.setParameter("openid_identifier", " " + CLAIMED_IDENTITY_URL);
    req.setRemoteHost("www.example.com");
    filter.setConsumer(new MockOpenIDConsumer() {

        public String beginConsumption(HttpServletRequest req, String claimedIdentity, String returnToUrl, String realm) throws OpenIDConsumerException {
            assertThat(claimedIdentity).isEqualTo(CLAIMED_IDENTITY_URL);
            assertThat(returnToUrl).isEqualTo(DEFAULT_TARGET_URL);
            assertThat(realm).isEqualTo("http://localhost:8080/");
            return REDIRECT_URL;
        }
    });
    FilterChain fc = mock(FilterChain.class);
    filter.doFilter(req, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo(REDIRECT_URL);
    // Filter chain shouldn't proceed
    verify(fc, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 48 with FilterChain

use of javax.servlet.FilterChain in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testChainNotInvokedForProxyReceptor.

// SEC-1592
@Test
public void testChainNotInvokedForProxyReceptor() throws Exception {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    request.setServletPath("/pgtCallback");
    filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
    filter.setProxyReceptorUrl(request.getServletPath());
    filter.doFilter(request, response, chain);
    verifyZeroInteractions(chain);
}
Also used : ProxyGrantingTicketStorage(org.jasig.cas.client.proxy.ProxyGrantingTicketStorage) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 49 with FilterChain

use of javax.servlet.FilterChain in project hadoop by apache.

the class TestHostnameFilter method testMissingHostname.

@Test
public void testMissingHostname() throws Exception {
    ServletRequest request = Mockito.mock(ServletRequest.class);
    Mockito.when(request.getRemoteAddr()).thenReturn(null);
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertTrue(HostnameFilter.get().contains("???"));
            invoked.set(true);
        }
    };
    Filter filter = new HostnameFilter();
    filter.init(null);
    assertNull(HostnameFilter.get());
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(HostnameFilter.get());
    filter.destroy();
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 50 with FilterChain

use of javax.servlet.FilterChain in project hadoop by apache.

the class TestMDCFilter method mdc.

@Test
public void mdc() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getUserPrincipal()).thenReturn(null);
    Mockito.when(request.getMethod()).thenReturn("METHOD");
    Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), null);
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    MDC.clear();
    Filter filter = new MDCFilter();
    filter.init(null);
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(MDC.get("hostname"));
    assertNull(MDC.get("user"));
    assertNull(MDC.get("method"));
    assertNull(MDC.get("path"));
    Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {

        @Override
        public String getName() {
            return "name";
        }
    });
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.set("HOST");
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), "HOST");
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.remove();
    filter.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Principal(java.security.Principal) 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