Search in sources :

Example 11 with Filter

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

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

Example 13 with Filter

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

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

the class MetricFilterAutoConfigurationTests method recordsHttpInteractionsWithRegexTemplateVariable.

@Test
public void recordsHttpInteractionsWithRegexTemplateVariable() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
    Filter filter = context.getBean(Filter.class);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
    mvc.perform(get("/templateVarRegexTest/foo")).andExpect(status().isOk());
    verify(context.getBean(CounterService.class)).increment("status.200.templateVarRegexTest.someVariable");
    verify(context.getBean(GaugeService.class)).submit(eq("response.templateVarRegexTest.someVariable"), anyDouble());
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 15 with Filter

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

the class MetricFilterAutoConfigurationTests method controllerMethodThatThrowsUnhandledException.

@Test
public void controllerMethodThatThrowsUnhandledException() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
    Filter filter = context.getBean(Filter.class);
    MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build();
    try {
        mvc.perform(get("/unhandledException")).andExpect(status().isInternalServerError());
    } catch (NestedServletException ex) {
    // Expected
    }
    verify(context.getBean(CounterService.class)).increment("status.500.unhandledException");
    verify(context.getBean(GaugeService.class)).submit(eq("response.unhandledException"), anyDouble());
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) NestedServletException(org.springframework.web.util.NestedServletException) Filter(javax.servlet.Filter) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

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