Search in sources :

Example 41 with FilterChain

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

the class ForwardedHeaderFilterTests method doWithFiltersAndGetResponse.

@SuppressWarnings("serial")
private MockHttpServletResponse doWithFiltersAndGetResponse(Filter... filters) throws ServletException, IOException {
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new MockFilterChain(new HttpServlet() {
    }, filters);
    filterChain.doFilter(request, response);
    return response;
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.test.MockFilterChain) MockFilterChain(org.springframework.mock.web.test.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 42 with FilterChain

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

the class HiddenHttpMethodFilterTests method filterWithParameter.

@Test
public void filterWithParameter() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    request.addParameter("_method", "delete");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException {
            assertEquals("Invalid method", "DELETE", ((HttpServletRequest) filterRequest).getMethod());
        }
    };
    filter.doFilter(request, response, filterChain);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 43 with FilterChain

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

the class HiddenHttpMethodFilterTests method filterWithNoParameter.

@Test
public void filterWithNoParameter() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException {
            assertEquals("Invalid method", "POST", ((HttpServletRequest) filterRequest).getMethod());
        }
    };
    filter.doFilter(request, response, filterChain);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 44 with FilterChain

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

the class MetricFilterAutoConfigurationTests method doesNotRecordRolledUpMetricsIfConfigured.

@Test
public void doesNotRecordRolledUpMetricsIfConfigured() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class, MetricFilterAutoConfiguration.class);
    EnvironmentTestUtils.addEnvironment(context, "endpoints.metrics.filter.gauge-submissions=", "endpoints.metrics.filter.counter-submissions=");
    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), never()).submit(anyString(), anyDouble());
    verify(context.getBean(CounterService.class), never()).increment(anyString());
    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 45 with FilterChain

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

the class MetricFilterAutoConfigurationTests method whenExceptionIsThrownResponseStatusIsUsedWhenResponseHasBeenCommitted.

@Test
public void whenExceptionIsThrownResponseStatusIsUsedWhenResponseHasBeenCommitted() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class, MetricFilterAutoConfiguration.class);
    context.refresh();
    Filter filter = context.getBean(Filter.class);
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/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);
            response.setCommitted(true);
            throw new IOException();
        }
    }).given(chain).doFilter(request, response);
    try {
        filter.doFilter(request, response, chain);
        fail();
    } catch (IOException ex) {
    // Continue
    }
    verify(context.getBean(CounterService.class)).increment(eq("status.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) IOException(java.io.IOException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) 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