Polish "Servlet path not explicitly required for EndpointRequest"

Closes gh-14503
pull/14914/head
Andy Wilkinson 6 years ago
parent 0fd873f0f9
commit d4cad5e916

@ -21,6 +21,7 @@ import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.endpoint.EndpointId;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint; import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.Operation; import org.springframework.boot.actuate.endpoint.Operation;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@ -118,7 +119,7 @@ public abstract class AbstractEndpointRequestIntegrationTests {
private TestPathMappedEndpoint mockEndpoint(String id) { private TestPathMappedEndpoint mockEndpoint(String id) {
TestPathMappedEndpoint endpoint = mock(TestPathMappedEndpoint.class); TestPathMappedEndpoint endpoint = mock(TestPathMappedEndpoint.class);
given(endpoint.getId()).willReturn(id); given(endpoint.getEndpointId()).willReturn(EndpointId.of(id));
given(endpoint.getRootPath()).willReturn(id); given(endpoint.getRootPath()).willReturn(id);
return endpoint; return endpoint;
} }

@ -33,7 +33,6 @@ import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoint;
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints; import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint; import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint;
import org.springframework.boot.autoconfigure.security.servlet.RequestMatcherProvider; import org.springframework.boot.autoconfigure.security.servlet.RequestMatcherProvider;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.security.web.util.matcher.RequestMatcher; import org.springframework.security.web.util.matcher.RequestMatcher;
@ -55,19 +54,19 @@ public class EndpointRequestTests {
@Test @Test
public void toAnyEndpointShouldMatchEndpointPath() { public void toAnyEndpointShouldMatchEndpointPath() {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint(); RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo"); assertMatcher(matcher, "/actuator").matches("/actuator/foo");
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo/zoo/"); assertMatcher(matcher, "/actuator").matches("/actuator/foo/zoo/");
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar"); assertMatcher(matcher, "/actuator").matches("/actuator/bar");
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar/baz"); assertMatcher(matcher, "/actuator").matches("/actuator/bar/baz");
assertMatcher(matcher, "/actuator", "/").matches("/actuator"); assertMatcher(matcher, "/actuator").matches("/actuator");
} }
@Test @Test
public void toAnyEndpointShouldMatchEndpointPathWithTrailingSlash() { public void toAnyEndpointShouldMatchEndpointPathWithTrailingSlash() {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint(); RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo/"); assertMatcher(matcher, "/actuator").matches("/actuator/foo/");
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar/"); assertMatcher(matcher, "/actuator").matches("/actuator/bar/");
assertMatcher(matcher, "/actuator", "/").matches("/actuator/"); assertMatcher(matcher, "/actuator").matches("/actuator/");
} }
@Test @Test
@ -85,26 +84,13 @@ public class EndpointRequestTests {
assertMatcher(matcher).doesNotMatch("/actuator/baz"); assertMatcher(matcher).doesNotMatch("/actuator/baz");
} }
@Test
public void toAnyEndpointWhenServletPathNotEmptyShouldMatch() {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
assertMatcher(matcher, "/actuator", "/spring").matches("/spring",
"/actuator/foo");
assertMatcher(matcher, "/actuator", "/spring").matches("/spring",
"/actuator/bar");
assertMatcher(matcher, "/actuator", "/spring").matches("/spring", "/actuator");
assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("/spring",
"/actuator/baz");
assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("", "/actuator/foo");
}
@Test @Test
public void toAnyEndpointWhenDispatcherServletPathProviderNotAvailableUsesEmptyPath() { public void toAnyEndpointWhenDispatcherServletPathProviderNotAvailableUsesEmptyPath() {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint(); RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
assertMatcher(matcher, "/actuator", null).matches("/actuator/foo"); assertMatcher(matcher, "/actuator").matches("/actuator/foo");
assertMatcher(matcher, "/actuator", null).matches("/actuator/bar"); assertMatcher(matcher, "/actuator").matches("/actuator/bar");
assertMatcher(matcher, "/actuator", null).matches("/actuator"); assertMatcher(matcher, "/actuator").matches("/actuator");
assertMatcher(matcher, "/actuator", null).doesNotMatch("/actuator/baz"); assertMatcher(matcher, "/actuator").doesNotMatch("/actuator/baz");
} }
@Test @Test
@ -151,14 +137,6 @@ public class EndpointRequestTests {
assertMatcher.doesNotMatch("/"); assertMatcher.doesNotMatch("/");
} }
@Test
public void toLinksWhenServletPathNotEmptyShouldMatch() {
RequestMatcher matcher = EndpointRequest.toLinks();
RequestMatcherAssert assertMatcher = assertMatcher(matcher, "/actuator",
"/spring");
assertMatcher.matches("/spring/actuator");
}
@Test @Test
public void excludeByClassShouldNotMatchExcluded() { public void excludeByClassShouldNotMatchExcluded() {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint() RequestMatcher matcher = EndpointRequest.toAnyEndpoint()
@ -221,7 +199,7 @@ public class EndpointRequestTests {
RequestMatcher matcher = EndpointRequest.toAnyEndpoint(); RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
RequestMatcher mockRequestMatcher = (request) -> false; RequestMatcher mockRequestMatcher = (request) -> false;
RequestMatcherAssert assertMatcher = assertMatcher(matcher, RequestMatcherAssert assertMatcher = assertMatcher(matcher,
mockPathMappedEndpoints(""), "", (pattern) -> mockRequestMatcher); mockPathMappedEndpoints(""), (pattern) -> mockRequestMatcher);
assertMatcher.doesNotMatch("/foo"); assertMatcher.doesNotMatch("/foo");
assertMatcher.doesNotMatch("/bar"); assertMatcher.doesNotMatch("/bar");
} }
@ -231,8 +209,7 @@ public class EndpointRequestTests {
RequestMatcher matcher = EndpointRequest.toLinks(); RequestMatcher matcher = EndpointRequest.toLinks();
RequestMatcher mockRequestMatcher = (request) -> false; RequestMatcher mockRequestMatcher = (request) -> false;
RequestMatcherAssert assertMatcher = assertMatcher(matcher, RequestMatcherAssert assertMatcher = assertMatcher(matcher,
mockPathMappedEndpoints("/actuator"), "", mockPathMappedEndpoints("/actuator"), (pattern) -> mockRequestMatcher);
(pattern) -> mockRequestMatcher);
assertMatcher.doesNotMatch("/actuator"); assertMatcher.doesNotMatch("/actuator");
} }
@ -248,13 +225,7 @@ public class EndpointRequestTests {
} }
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath) { private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath) {
return assertMatcher(matcher, mockPathMappedEndpoints(basePath)); return assertMatcher(matcher, mockPathMappedEndpoints(basePath), null);
}
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath,
String servletPath) {
return assertMatcher(matcher, mockPathMappedEndpoints(basePath), servletPath,
null);
} }
private PathMappedEndpoints mockPathMappedEndpoints(String basePath) { private PathMappedEndpoints mockPathMappedEndpoints(String basePath) {
@ -273,11 +244,11 @@ public class EndpointRequestTests {
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
PathMappedEndpoints pathMappedEndpoints) { PathMappedEndpoints pathMappedEndpoints) {
return assertMatcher(matcher, pathMappedEndpoints, "", null); return assertMatcher(matcher, pathMappedEndpoints, null);
} }
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
PathMappedEndpoints pathMappedEndpoints, String dispatcherServletPath, PathMappedEndpoints pathMappedEndpoints,
RequestMatcherProvider matcherProvider) { RequestMatcherProvider matcherProvider) {
StaticWebApplicationContext context = new StaticWebApplicationContext(); StaticWebApplicationContext context = new StaticWebApplicationContext();
context.registerBean(WebEndpointProperties.class); context.registerBean(WebEndpointProperties.class);
@ -289,10 +260,6 @@ public class EndpointRequestTests {
properties.setBasePath(pathMappedEndpoints.getBasePath()); properties.setBasePath(pathMappedEndpoints.getBasePath());
} }
} }
if (dispatcherServletPath != null) {
DispatcherServletPath path = () -> dispatcherServletPath;
context.registerBean(DispatcherServletPath.class, () -> path);
}
if (matcherProvider != null) { if (matcherProvider != null) {
context.registerBean(RequestMatcherProvider.class, () -> matcherProvider); context.registerBean(RequestMatcherProvider.class, () -> matcherProvider);
} }
@ -314,10 +281,6 @@ public class EndpointRequestTests {
matches(mockRequest(servletPath)); matches(mockRequest(servletPath));
} }
public void matches(String servletPath, String pathInfo) {
matches(mockRequest(servletPath, pathInfo));
}
private void matches(HttpServletRequest request) { private void matches(HttpServletRequest request) {
assertThat(this.matcher.matches(request)) assertThat(this.matcher.matches(request))
.as("Matches " + getRequestPath(request)).isTrue(); .as("Matches " + getRequestPath(request)).isTrue();
@ -327,20 +290,12 @@ public class EndpointRequestTests {
doesNotMatch(mockRequest(servletPath)); doesNotMatch(mockRequest(servletPath));
} }
public void doesNotMatch(String servletPath, String pathInfo) {
doesNotMatch(mockRequest(servletPath, pathInfo));
}
private void doesNotMatch(HttpServletRequest request) { private void doesNotMatch(HttpServletRequest request) {
assertThat(this.matcher.matches(request)) assertThat(this.matcher.matches(request))
.as("Does not match " + getRequestPath(request)).isFalse(); .as("Does not match " + getRequestPath(request)).isFalse();
} }
private MockHttpServletRequest mockRequest(String servletPath) { private MockHttpServletRequest mockRequest(String servletPath) {
return mockRequest(servletPath, null);
}
private MockHttpServletRequest mockRequest(String servletPath, String path) {
MockServletContext servletContext = new MockServletContext(); MockServletContext servletContext = new MockServletContext();
servletContext.setAttribute( servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
@ -349,7 +304,6 @@ public class EndpointRequestTests {
if (servletPath != null) { if (servletPath != null) {
request.setServletPath(servletPath); request.setServletPath(servletPath);
} }
request.setPathInfo(path);
return request; return request;
} }

@ -57,6 +57,18 @@ import org.springframework.web.cors.CorsConfiguration;
public class MvcEndpointRequestIntegrationTests public class MvcEndpointRequestIntegrationTests
extends AbstractEndpointRequestIntegrationTests { extends AbstractEndpointRequestIntegrationTests {
@Test
public void toLinksWhenServletPathSetShouldMatch() {
getContextRunner().withPropertyValues("server.servlet.path=/admin")
.run((context) -> {
WebTestClient webTestClient = getWebTestClient(context);
webTestClient.get().uri("/admin/actuator/").exchange().expectStatus()
.isOk();
webTestClient.get().uri("/admin/actuator").exchange().expectStatus()
.isOk();
});
}
@Test @Test
public void toEndpointWhenServletPathSetShouldMatch() { public void toEndpointWhenServletPathSetShouldMatch() {
getContextRunner().withPropertyValues("server.servlet.path=/admin") getContextRunner().withPropertyValues("server.servlet.path=/admin")

Loading…
Cancel
Save