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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations