Merge branch '2.0.x'

pull/13344/merge
Madhura Bhave 7 years ago
commit 6eaae608b5

@ -137,9 +137,10 @@ public final class EndpointRequest {
private RequestMatcher createDelegate(WebApplicationContext context) {
try {
RequestMatcherFactory requestMatcherFactory = new RequestMatcherFactory(
context.getBean(DispatcherServletPathProvider.class)
.getServletPath());
String servletPath = getServletPath(context);
RequestMatcherFactory requestMatcherFactory = (StringUtils
.hasText(servletPath) ? new RequestMatcherFactory(servletPath)
: RequestMatcherFactory.withEmptyServletPath());
return createDelegate(context, requestMatcherFactory);
}
catch (NoSuchBeanDefinitionException ex) {
@ -147,6 +148,16 @@ public final class EndpointRequest {
}
}
private String getServletPath(WebApplicationContext context) {
try {
return context.getBean(DispatcherServletPathProvider.class)
.getServletPath();
}
catch (NoSuchBeanDefinitionException ex) {
return "";
}
}
protected abstract RequestMatcher createDelegate(WebApplicationContext context,
RequestMatcherFactory requestMatcherFactory);
@ -279,11 +290,14 @@ public final class EndpointRequest {
private final String servletPath;
private static final RequestMatcherFactory EMPTY_SERVLET_PATH = new RequestMatcherFactory(
"");
RequestMatcherFactory(String servletPath) {
this.servletPath = servletPath;
}
public RequestMatcher antPath(String... parts) {
RequestMatcher antPath(String... parts) {
String pattern = (this.servletPath.equals("/") ? "" : this.servletPath);
for (String part : parts) {
pattern += part;
@ -291,6 +305,10 @@ public final class EndpointRequest {
return new AntPathRequestMatcher(pattern);
}
static RequestMatcherFactory withEmptyServletPath() {
return EMPTY_SERVLET_PATH;
}
}
}

@ -86,6 +86,15 @@ public class EndpointRequestTests {
assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("", "/actuator/foo");
}
@Test
public void toAnyEndpointWhenDispatcherServletPathProviderNotAvailableUsesEmptyPath() {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
assertMatcher(matcher, "/actuator", null).matches("/actuator/foo");
assertMatcher(matcher, "/actuator", null).matches("/actuator/bar");
assertMatcher(matcher, "/actuator", null).matches("/actuator");
assertMatcher(matcher, "/actuator", null).doesNotMatch("/actuator/baz");
}
@Test
public void toEndpointClassShouldMatchEndpointPath() {
RequestMatcher matcher = EndpointRequest.to(FooEndpoint.class);
@ -245,8 +254,10 @@ public class EndpointRequestTests {
properties.setBasePath(pathMappedEndpoints.getBasePath());
}
}
DispatcherServletPathProvider pathProvider = () -> servletPath;
context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider);
if (servletPath != null) {
DispatcherServletPathProvider pathProvider = () -> servletPath;
context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider);
}
return assertThat(new RequestMatcherAssert(context, matcher));
}

Loading…
Cancel
Save