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) { private RequestMatcher createDelegate(WebApplicationContext context) {
try { try {
RequestMatcherFactory requestMatcherFactory = new RequestMatcherFactory( String servletPath = getServletPath(context);
context.getBean(DispatcherServletPathProvider.class) RequestMatcherFactory requestMatcherFactory = (StringUtils
.getServletPath()); .hasText(servletPath) ? new RequestMatcherFactory(servletPath)
: RequestMatcherFactory.withEmptyServletPath());
return createDelegate(context, requestMatcherFactory); return createDelegate(context, requestMatcherFactory);
} }
catch (NoSuchBeanDefinitionException ex) { 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, protected abstract RequestMatcher createDelegate(WebApplicationContext context,
RequestMatcherFactory requestMatcherFactory); RequestMatcherFactory requestMatcherFactory);
@ -279,11 +290,14 @@ public final class EndpointRequest {
private final String servletPath; private final String servletPath;
private static final RequestMatcherFactory EMPTY_SERVLET_PATH = new RequestMatcherFactory(
"");
RequestMatcherFactory(String servletPath) { RequestMatcherFactory(String servletPath) {
this.servletPath = servletPath; this.servletPath = servletPath;
} }
public RequestMatcher antPath(String... parts) { RequestMatcher antPath(String... parts) {
String pattern = (this.servletPath.equals("/") ? "" : this.servletPath); String pattern = (this.servletPath.equals("/") ? "" : this.servletPath);
for (String part : parts) { for (String part : parts) {
pattern += part; pattern += part;
@ -291,6 +305,10 @@ public final class EndpointRequest {
return new AntPathRequestMatcher(pattern); 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"); 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 @Test
public void toEndpointClassShouldMatchEndpointPath() { public void toEndpointClassShouldMatchEndpointPath() {
RequestMatcher matcher = EndpointRequest.to(FooEndpoint.class); RequestMatcher matcher = EndpointRequest.to(FooEndpoint.class);
@ -245,8 +254,10 @@ public class EndpointRequestTests {
properties.setBasePath(pathMappedEndpoints.getBasePath()); properties.setBasePath(pathMappedEndpoints.getBasePath());
} }
} }
DispatcherServletPathProvider pathProvider = () -> servletPath; if (servletPath != null) {
context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider); DispatcherServletPathProvider pathProvider = () -> servletPath;
context.registerBean(DispatcherServletPathProvider.class, () -> pathProvider);
}
return assertThat(new RequestMatcherAssert(context, matcher)); return assertThat(new RequestMatcherAssert(context, matcher));
} }

Loading…
Cancel
Save