|
22 | 22 | import static org.mockito.Matchers.anyDouble;
|
23 | 23 | import static org.mockito.Matchers.eq;
|
24 | 24 | import static org.mockito.Mockito.mock;
|
| 25 | +import static org.mockito.Mockito.times; |
25 | 26 | import static org.mockito.Mockito.verify;
|
26 | 27 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
27 | 28 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
37 | 38 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
38 | 39 | import org.springframework.context.annotation.Bean;
|
39 | 40 | import org.springframework.context.annotation.Configuration;
|
| 41 | +import org.springframework.http.HttpStatus; |
40 | 42 | import org.springframework.mock.web.MockHttpServletRequest;
|
41 | 43 | import org.springframework.mock.web.MockHttpServletResponse;
|
42 | 44 | import org.springframework.test.web.servlet.MockMvc;
|
43 | 45 | import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
| 46 | +import org.springframework.web.bind.annotation.PathVariable; |
44 | 47 | import org.springframework.web.bind.annotation.RequestMapping;
|
| 48 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 49 | +import org.springframework.web.bind.annotation.ResponseStatus; |
45 | 50 | import org.springframework.web.bind.annotation.RestController;
|
46 | 51 |
|
47 | 52 | /**
|
@@ -89,6 +94,39 @@ public void recordsHttpInteractionsWithTemplateVariable() throws Exception {
|
89 | 94 | anyDouble());
|
90 | 95 | context.close();
|
91 | 96 | }
|
| 97 | + |
| 98 | + @Test |
| 99 | + public void recordsKnown404HttpInteractionsAsSingleMetricWithPathAndTemplateVariable() throws Exception { |
| 100 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 101 | + Config.class, MetricFilterAutoConfiguration.class); |
| 102 | + Filter filter = context.getBean(Filter.class); |
| 103 | + MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build(); |
| 104 | + mvc.perform(get("/knownPath/foo")) |
| 105 | + .andExpect(status().isNotFound()); |
| 106 | + |
| 107 | + verify(context.getBean(CounterService.class)).increment("status.404.knownPath.-someVariable-"); |
| 108 | + verify(context.getBean(GaugeService.class)).submit(eq("response.knownPath.-someVariable-"), |
| 109 | + anyDouble()); |
| 110 | + context.close(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void records404HttpInteractionsAsSingleMetric() throws Exception { |
| 115 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 116 | + Config.class, MetricFilterAutoConfiguration.class); |
| 117 | + Filter filter = context.getBean(Filter.class); |
| 118 | + MockMvc mvc = MockMvcBuilders.standaloneSetup(new MetricFilterTestController()).addFilter(filter).build(); |
| 119 | + mvc.perform(get("/unknownPath/1")) |
| 120 | + .andExpect(status().isNotFound()); |
| 121 | + |
| 122 | + mvc.perform(get("/unknownPath/2")) |
| 123 | + .andExpect(status().isNotFound()); |
| 124 | + |
| 125 | + verify(context.getBean(CounterService.class), times(2)).increment("status.404.unknownPath"); |
| 126 | + verify(context.getBean(GaugeService.class), times(2)).submit(eq("response.unknownPath"), |
| 127 | + anyDouble()); |
| 128 | + context.close(); |
| 129 | + } |
92 | 130 |
|
93 | 131 | @Test
|
94 | 132 | public void skipsFilterIfMissingServices() throws Exception {
|
@@ -121,7 +159,15 @@ class MetricFilterTestController
|
121 | 159 | {
|
122 | 160 |
|
123 | 161 | @RequestMapping("templateVarTest/{someVariable}")
|
124 |
| - public String testTemplateVariableResolution(String someVariable) |
| 162 | + public String testTemplateVariableResolution(@PathVariable String someVariable) |
| 163 | + { |
| 164 | + return someVariable; |
| 165 | + } |
| 166 | + |
| 167 | + @RequestMapping("knownPath/{someVariable}") |
| 168 | + @ResponseStatus(HttpStatus.NOT_FOUND) |
| 169 | + @ResponseBody |
| 170 | + public String testKnownPathWith404Response(@PathVariable String someVariable) |
125 | 171 | {
|
126 | 172 | return someVariable;
|
127 | 173 | }
|
|
0 commit comments