mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish "Servlet path not explicitly required for EndpointRequest"
Closes gh-14503
This commit is contained in:
parent
0fd873f0f9
commit
d4cad5e916
@ -21,6 +21,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.EndpointId;
|
||||
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.Operation;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
@ -118,7 +119,7 @@ public abstract class AbstractEndpointRequestIntegrationTests {
|
||||
|
||||
private TestPathMappedEndpoint mockEndpoint(String id) {
|
||||
TestPathMappedEndpoint endpoint = mock(TestPathMappedEndpoint.class);
|
||||
given(endpoint.getId()).willReturn(id);
|
||||
given(endpoint.getEndpointId()).willReturn(EndpointId.of(id));
|
||||
given(endpoint.getRootPath()).willReturn(id);
|
||||
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.annotation.ServletEndpoint;
|
||||
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.MockServletContext;
|
||||
import org.springframework.security.web.util.matcher.RequestMatcher;
|
||||
@ -55,19 +54,19 @@ public class EndpointRequestTests {
|
||||
@Test
|
||||
public void toAnyEndpointShouldMatchEndpointPath() {
|
||||
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo");
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo/zoo/");
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar");
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar/baz");
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/foo");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/foo/zoo/");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/bar");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/bar/baz");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toAnyEndpointShouldMatchEndpointPathWithTrailingSlash() {
|
||||
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo/");
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar/");
|
||||
assertMatcher(matcher, "/actuator", "/").matches("/actuator/");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/foo/");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/bar/");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -85,26 +84,13 @@ public class EndpointRequestTests {
|
||||
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
|
||||
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");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/foo");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator/bar");
|
||||
assertMatcher(matcher, "/actuator").matches("/actuator");
|
||||
assertMatcher(matcher, "/actuator").doesNotMatch("/actuator/baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -151,14 +137,6 @@ public class EndpointRequestTests {
|
||||
assertMatcher.doesNotMatch("/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toLinksWhenServletPathNotEmptyShouldMatch() {
|
||||
RequestMatcher matcher = EndpointRequest.toLinks();
|
||||
RequestMatcherAssert assertMatcher = assertMatcher(matcher, "/actuator",
|
||||
"/spring");
|
||||
assertMatcher.matches("/spring/actuator");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void excludeByClassShouldNotMatchExcluded() {
|
||||
RequestMatcher matcher = EndpointRequest.toAnyEndpoint()
|
||||
@ -221,7 +199,7 @@ public class EndpointRequestTests {
|
||||
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
|
||||
RequestMatcher mockRequestMatcher = (request) -> false;
|
||||
RequestMatcherAssert assertMatcher = assertMatcher(matcher,
|
||||
mockPathMappedEndpoints(""), "", (pattern) -> mockRequestMatcher);
|
||||
mockPathMappedEndpoints(""), (pattern) -> mockRequestMatcher);
|
||||
assertMatcher.doesNotMatch("/foo");
|
||||
assertMatcher.doesNotMatch("/bar");
|
||||
}
|
||||
@ -231,8 +209,7 @@ public class EndpointRequestTests {
|
||||
RequestMatcher matcher = EndpointRequest.toLinks();
|
||||
RequestMatcher mockRequestMatcher = (request) -> false;
|
||||
RequestMatcherAssert assertMatcher = assertMatcher(matcher,
|
||||
mockPathMappedEndpoints("/actuator"), "",
|
||||
(pattern) -> mockRequestMatcher);
|
||||
mockPathMappedEndpoints("/actuator"), (pattern) -> mockRequestMatcher);
|
||||
assertMatcher.doesNotMatch("/actuator");
|
||||
}
|
||||
|
||||
@ -248,13 +225,7 @@ public class EndpointRequestTests {
|
||||
}
|
||||
|
||||
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath) {
|
||||
return assertMatcher(matcher, mockPathMappedEndpoints(basePath));
|
||||
}
|
||||
|
||||
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath,
|
||||
String servletPath) {
|
||||
return assertMatcher(matcher, mockPathMappedEndpoints(basePath), servletPath,
|
||||
null);
|
||||
return assertMatcher(matcher, mockPathMappedEndpoints(basePath), null);
|
||||
}
|
||||
|
||||
private PathMappedEndpoints mockPathMappedEndpoints(String basePath) {
|
||||
@ -273,11 +244,11 @@ public class EndpointRequestTests {
|
||||
|
||||
private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
|
||||
PathMappedEndpoints pathMappedEndpoints) {
|
||||
return assertMatcher(matcher, pathMappedEndpoints, "", null);
|
||||
return assertMatcher(matcher, pathMappedEndpoints, null);
|
||||
}
|
||||
|
||||
private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
|
||||
PathMappedEndpoints pathMappedEndpoints, String dispatcherServletPath,
|
||||
PathMappedEndpoints pathMappedEndpoints,
|
||||
RequestMatcherProvider matcherProvider) {
|
||||
StaticWebApplicationContext context = new StaticWebApplicationContext();
|
||||
context.registerBean(WebEndpointProperties.class);
|
||||
@ -289,10 +260,6 @@ public class EndpointRequestTests {
|
||||
properties.setBasePath(pathMappedEndpoints.getBasePath());
|
||||
}
|
||||
}
|
||||
if (dispatcherServletPath != null) {
|
||||
DispatcherServletPath path = () -> dispatcherServletPath;
|
||||
context.registerBean(DispatcherServletPath.class, () -> path);
|
||||
}
|
||||
if (matcherProvider != null) {
|
||||
context.registerBean(RequestMatcherProvider.class, () -> matcherProvider);
|
||||
}
|
||||
@ -314,10 +281,6 @@ public class EndpointRequestTests {
|
||||
matches(mockRequest(servletPath));
|
||||
}
|
||||
|
||||
public void matches(String servletPath, String pathInfo) {
|
||||
matches(mockRequest(servletPath, pathInfo));
|
||||
}
|
||||
|
||||
private void matches(HttpServletRequest request) {
|
||||
assertThat(this.matcher.matches(request))
|
||||
.as("Matches " + getRequestPath(request)).isTrue();
|
||||
@ -327,20 +290,12 @@ public class EndpointRequestTests {
|
||||
doesNotMatch(mockRequest(servletPath));
|
||||
}
|
||||
|
||||
public void doesNotMatch(String servletPath, String pathInfo) {
|
||||
doesNotMatch(mockRequest(servletPath, pathInfo));
|
||||
}
|
||||
|
||||
private void doesNotMatch(HttpServletRequest request) {
|
||||
assertThat(this.matcher.matches(request))
|
||||
.as("Does not match " + getRequestPath(request)).isFalse();
|
||||
}
|
||||
|
||||
private MockHttpServletRequest mockRequest(String servletPath) {
|
||||
return mockRequest(servletPath, null);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest mockRequest(String servletPath, String path) {
|
||||
MockServletContext servletContext = new MockServletContext();
|
||||
servletContext.setAttribute(
|
||||
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
|
||||
@ -349,7 +304,6 @@ public class EndpointRequestTests {
|
||||
if (servletPath != null) {
|
||||
request.setServletPath(servletPath);
|
||||
}
|
||||
request.setPathInfo(path);
|
||||
return request;
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,18 @@ import org.springframework.web.cors.CorsConfiguration;
|
||||
public class MvcEndpointRequestIntegrationTests
|
||||
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
|
||||
public void toEndpointWhenServletPathSetShouldMatch() {
|
||||
getContextRunner().withPropertyValues("server.servlet.path=/admin")
|
||||
|
Loading…
Reference in New Issue
Block a user