Fix conditions on auto-configured WebMvcMetricsFilter

Closes gh-31150
pull/31258/head
Andy Wilkinson 3 years ago
parent 16f49745ef
commit 24dc525127

@ -41,6 +41,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -80,7 +81,7 @@ public class WebMvcMetricsAutoConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingFilterBean
public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(MeterRegistry registry, public FilterRegistrationBean<WebMvcMetricsFilter> webMvcMetricsFilter(MeterRegistry registry,
WebMvcTagsProvider tagsProvider) { WebMvcTagsProvider tagsProvider) {
ServerRequest request = this.properties.getWeb().getServer().getRequest(); ServerRequest request = this.properties.getWeb().getServer().getRequest();

@ -122,14 +122,34 @@ class WebMvcMetricsAutoConfigurationTests {
} }
@Test @Test
void filterRegistrationBacksOff() { void filterRegistrationBacksOffWithAnotherWebMvcMetricsFilterRegistration() {
this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterRegistrationConfiguration.class)
.run((context) -> {
assertThat(context).hasSingleBean(FilterRegistrationBean.class); assertThat(context).hasSingleBean(FilterRegistrationBean.class);
assertThat(context.getBean(FilterRegistrationBean.class)) assertThat(context.getBean(FilterRegistrationBean.class))
.isSameAs(context.getBean("testWebMvcMetricsFilter")); .isSameAs(context.getBean("testWebMvcMetricsFilter"));
}); });
} }
@Test
void filterRegistrationBacksOffWithAnotherWebMvcMetricsFilter() {
this.contextRunner.withUserConfiguration(TestWebMvcMetricsFilterConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(FilterRegistrationBean.class)
.hasSingleBean(WebMvcMetricsFilter.class));
}
@Test
void filterRegistrationDoesNotBackOffWithOtherFilterRegistration() {
this.contextRunner.withUserConfiguration(TestFilterRegistrationConfiguration.class)
.run((context) -> assertThat(context).hasBean("testFilter").hasBean("webMvcMetricsFilter"));
}
@Test
void filterRegistrationDoesNotBackOffWithOtherFilter() {
this.contextRunner.withUserConfiguration(TestFilterConfiguration.class)
.run((context) -> assertThat(context).hasBean("testFilter").hasBean("webMvcMetricsFilter"));
}
@Test @Test
void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) { void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput output) {
this.contextRunner.withUserConfiguration(TestController.class) this.contextRunner.withUserConfiguration(TestController.class)
@ -258,7 +278,7 @@ class WebMvcMetricsAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class TestWebMvcMetricsFilterConfiguration { static class TestWebMvcMetricsFilterRegistrationConfiguration {
@Bean @Bean
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -268,4 +288,35 @@ class WebMvcMetricsAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class TestWebMvcMetricsFilterConfiguration {
@Bean
WebMvcMetricsFilter testWebMvcMetricsFilter() {
return new WebMvcMetricsFilter(null, null, null, null);
}
}
@Configuration(proxyBeanMethods = false)
static class TestFilterRegistrationConfiguration {
@Bean
@SuppressWarnings("unchecked")
FilterRegistrationBean<Filter> testFilter() {
return mock(FilterRegistrationBean.class);
}
}
@Configuration(proxyBeanMethods = false)
static class TestFilterConfiguration {
@Bean
Filter testFilter() {
return mock(Filter.class);
}
}
} }

Loading…
Cancel
Save