Enable PathPattern based matching for MVC actuators
Closes gh-24645
This commit is contained in:
@@ -56,8 +56,11 @@ import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.handler.RequestMatchResult;
|
||||
import org.springframework.web.util.ServletRequestPathUtils;
|
||||
import org.springframework.web.util.pattern.PathPatternParser;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Integration tests for web endpoints exposed using Spring MVC.
|
||||
@@ -104,24 +107,37 @@ class MvcWebEndpointIntegrationTests
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchWhenPathPatternParserShouldThrowException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> getMatchResult("/spring/", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchWhenRequestHasTrailingSlashShouldNotBeNull() {
|
||||
assertThat(getMatchResult("/spring/")).isNotNull();
|
||||
assertThat(getMatchResult("/spring/", false)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchWhenRequestHasSuffixShouldBeNull() {
|
||||
assertThat(getMatchResult("/spring.do")).isNull();
|
||||
assertThat(getMatchResult("/spring.do", false)).isNull();
|
||||
}
|
||||
|
||||
private RequestMatchResult getMatchResult(String servletPath) {
|
||||
private RequestMatchResult getMatchResult(String servletPath, boolean isPatternParser) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setServletPath(servletPath);
|
||||
AnnotationConfigServletWebServerApplicationContext context = createApplicationContext();
|
||||
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
|
||||
if (isPatternParser) {
|
||||
context.register(WebMvcConfiguration.class);
|
||||
}
|
||||
else {
|
||||
context.register(PathMatcherWebMvcConfiguration.class);
|
||||
}
|
||||
context.register(TestEndpointConfiguration.class);
|
||||
context.refresh();
|
||||
WebMvcEndpointHandlerMapping bean = context.getBean(WebMvcEndpointHandlerMapping.class);
|
||||
try {
|
||||
// Setup request attributes
|
||||
ServletRequestPathUtils.parseAndCache(request);
|
||||
// Trigger initLookupPath
|
||||
bean.getHandler(request);
|
||||
}
|
||||
@@ -156,7 +172,35 @@ class MvcWebEndpointIntegrationTests
|
||||
String endpointPath = environment.getProperty("endpointPath");
|
||||
return new WebMvcEndpointHandlerMapping(new EndpointMapping(endpointPath),
|
||||
endpointDiscoverer.getEndpoints(), endpointMediaTypes, corsConfiguration,
|
||||
new EndpointLinksResolver(endpointDiscoverer.getEndpoints()), StringUtils.hasText(endpointPath));
|
||||
new EndpointLinksResolver(endpointDiscoverer.getEndpoints()), StringUtils.hasText(endpointPath),
|
||||
new PathPatternParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ImportAutoConfiguration({ JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
|
||||
ServletWebServerFactoryAutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class })
|
||||
static class PathMatcherWebMvcConfiguration {
|
||||
|
||||
@Bean
|
||||
TomcatServletWebServerFactory tomcat() {
|
||||
return new TomcatServletWebServerFactory(0);
|
||||
}
|
||||
|
||||
@Bean
|
||||
WebMvcEndpointHandlerMapping webEndpointHandlerMapping(Environment environment,
|
||||
WebEndpointDiscoverer endpointDiscoverer, EndpointMediaTypes endpointMediaTypes) {
|
||||
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
corsConfiguration.setAllowedOrigins(Arrays.asList("https://example.com"));
|
||||
corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST"));
|
||||
String endpointPath = environment.getProperty("endpointPath");
|
||||
WebMvcEndpointHandlerMapping handlerMapping = new WebMvcEndpointHandlerMapping(
|
||||
new EndpointMapping(endpointPath), endpointDiscoverer.getEndpoints(), endpointMediaTypes,
|
||||
corsConfiguration, new EndpointLinksResolver(endpointDiscoverer.getEndpoints()),
|
||||
StringUtils.hasText(endpointPath));
|
||||
return handlerMapping;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user