Skip to content

Commit d4cad5e

Browse files
committed
Polish "Servlet path not explicitly required for EndpointRequest"
Closes gh-14503
1 parent 0fd873f commit d4cad5e

File tree

3 files changed

+31
-64
lines changed

3 files changed

+31
-64
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/AbstractEndpointRequestIntegrationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.junit.Test;
2323

24+
import org.springframework.boot.actuate.endpoint.EndpointId;
2425
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
2526
import org.springframework.boot.actuate.endpoint.Operation;
2627
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@@ -118,7 +119,7 @@ public PathMappedEndpoints pathMappedEndpoints() {
118119

119120
private TestPathMappedEndpoint mockEndpoint(String id) {
120121
TestPathMappedEndpoint endpoint = mock(TestPathMappedEndpoint.class);
121-
given(endpoint.getId()).willReturn(id);
122+
given(endpoint.getEndpointId()).willReturn(EndpointId.of(id));
122123
given(endpoint.getRootPath()).willReturn(id);
123124
return endpoint;
124125
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequestTests.java

+17-63
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints;
3434
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint;
3535
import org.springframework.boot.autoconfigure.security.servlet.RequestMatcherProvider;
36-
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
3736
import org.springframework.mock.web.MockHttpServletRequest;
3837
import org.springframework.mock.web.MockServletContext;
3938
import org.springframework.security.web.util.matcher.RequestMatcher;
@@ -55,19 +54,19 @@ public class EndpointRequestTests {
5554
@Test
5655
public void toAnyEndpointShouldMatchEndpointPath() {
5756
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
58-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo");
59-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo/zoo/");
60-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar");
61-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar/baz");
62-
assertMatcher(matcher, "/actuator", "/").matches("/actuator");
57+
assertMatcher(matcher, "/actuator").matches("/actuator/foo");
58+
assertMatcher(matcher, "/actuator").matches("/actuator/foo/zoo/");
59+
assertMatcher(matcher, "/actuator").matches("/actuator/bar");
60+
assertMatcher(matcher, "/actuator").matches("/actuator/bar/baz");
61+
assertMatcher(matcher, "/actuator").matches("/actuator");
6362
}
6463

6564
@Test
6665
public void toAnyEndpointShouldMatchEndpointPathWithTrailingSlash() {
6766
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
68-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/foo/");
69-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/bar/");
70-
assertMatcher(matcher, "/actuator", "/").matches("/actuator/");
67+
assertMatcher(matcher, "/actuator").matches("/actuator/foo/");
68+
assertMatcher(matcher, "/actuator").matches("/actuator/bar/");
69+
assertMatcher(matcher, "/actuator").matches("/actuator/");
7170
}
7271

7372
@Test
@@ -85,26 +84,13 @@ public void toAnyEndpointShouldNotMatchOtherPath() {
8584
assertMatcher(matcher).doesNotMatch("/actuator/baz");
8685
}
8786

88-
@Test
89-
public void toAnyEndpointWhenServletPathNotEmptyShouldMatch() {
90-
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
91-
assertMatcher(matcher, "/actuator", "/spring").matches("/spring",
92-
"/actuator/foo");
93-
assertMatcher(matcher, "/actuator", "/spring").matches("/spring",
94-
"/actuator/bar");
95-
assertMatcher(matcher, "/actuator", "/spring").matches("/spring", "/actuator");
96-
assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("/spring",
97-
"/actuator/baz");
98-
assertMatcher(matcher, "/actuator", "/spring").doesNotMatch("", "/actuator/foo");
99-
}
100-
10187
@Test
10288
public void toAnyEndpointWhenDispatcherServletPathProviderNotAvailableUsesEmptyPath() {
10389
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
104-
assertMatcher(matcher, "/actuator", null).matches("/actuator/foo");
105-
assertMatcher(matcher, "/actuator", null).matches("/actuator/bar");
106-
assertMatcher(matcher, "/actuator", null).matches("/actuator");
107-
assertMatcher(matcher, "/actuator", null).doesNotMatch("/actuator/baz");
90+
assertMatcher(matcher, "/actuator").matches("/actuator/foo");
91+
assertMatcher(matcher, "/actuator").matches("/actuator/bar");
92+
assertMatcher(matcher, "/actuator").matches("/actuator");
93+
assertMatcher(matcher, "/actuator").doesNotMatch("/actuator/baz");
10894
}
10995

11096
@Test
@@ -151,14 +137,6 @@ public void toLinksWhenBasePathEmptyShouldNotMatch() {
151137
assertMatcher.doesNotMatch("/");
152138
}
153139

154-
@Test
155-
public void toLinksWhenServletPathNotEmptyShouldMatch() {
156-
RequestMatcher matcher = EndpointRequest.toLinks();
157-
RequestMatcherAssert assertMatcher = assertMatcher(matcher, "/actuator",
158-
"/spring");
159-
assertMatcher.matches("/spring/actuator");
160-
}
161-
162140
@Test
163141
public void excludeByClassShouldNotMatchExcluded() {
164142
RequestMatcher matcher = EndpointRequest.toAnyEndpoint()
@@ -221,7 +199,7 @@ public void endpointRequestMatcherShouldUseCustomRequestMatcherProvider() {
221199
RequestMatcher matcher = EndpointRequest.toAnyEndpoint();
222200
RequestMatcher mockRequestMatcher = (request) -> false;
223201
RequestMatcherAssert assertMatcher = assertMatcher(matcher,
224-
mockPathMappedEndpoints(""), "", (pattern) -> mockRequestMatcher);
202+
mockPathMappedEndpoints(""), (pattern) -> mockRequestMatcher);
225203
assertMatcher.doesNotMatch("/foo");
226204
assertMatcher.doesNotMatch("/bar");
227205
}
@@ -231,8 +209,7 @@ public void linksRequestMatcherShouldUseCustomRequestMatcherProvider() {
231209
RequestMatcher matcher = EndpointRequest.toLinks();
232210
RequestMatcher mockRequestMatcher = (request) -> false;
233211
RequestMatcherAssert assertMatcher = assertMatcher(matcher,
234-
mockPathMappedEndpoints("/actuator"), "",
235-
(pattern) -> mockRequestMatcher);
212+
mockPathMappedEndpoints("/actuator"), (pattern) -> mockRequestMatcher);
236213
assertMatcher.doesNotMatch("/actuator");
237214
}
238215

@@ -248,13 +225,7 @@ private RequestMatcherAssert assertMatcher(RequestMatcher matcher) {
248225
}
249226

250227
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath) {
251-
return assertMatcher(matcher, mockPathMappedEndpoints(basePath));
252-
}
253-
254-
private RequestMatcherAssert assertMatcher(RequestMatcher matcher, String basePath,
255-
String servletPath) {
256-
return assertMatcher(matcher, mockPathMappedEndpoints(basePath), servletPath,
257-
null);
228+
return assertMatcher(matcher, mockPathMappedEndpoints(basePath), null);
258229
}
259230

260231
private PathMappedEndpoints mockPathMappedEndpoints(String basePath) {
@@ -273,11 +244,11 @@ private TestEndpoint mockEndpoint(EndpointId id, String rootPath) {
273244

274245
private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
275246
PathMappedEndpoints pathMappedEndpoints) {
276-
return assertMatcher(matcher, pathMappedEndpoints, "", null);
247+
return assertMatcher(matcher, pathMappedEndpoints, null);
277248
}
278249

279250
private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
280-
PathMappedEndpoints pathMappedEndpoints, String dispatcherServletPath,
251+
PathMappedEndpoints pathMappedEndpoints,
281252
RequestMatcherProvider matcherProvider) {
282253
StaticWebApplicationContext context = new StaticWebApplicationContext();
283254
context.registerBean(WebEndpointProperties.class);
@@ -289,10 +260,6 @@ private RequestMatcherAssert assertMatcher(RequestMatcher matcher,
289260
properties.setBasePath(pathMappedEndpoints.getBasePath());
290261
}
291262
}
292-
if (dispatcherServletPath != null) {
293-
DispatcherServletPath path = () -> dispatcherServletPath;
294-
context.registerBean(DispatcherServletPath.class, () -> path);
295-
}
296263
if (matcherProvider != null) {
297264
context.registerBean(RequestMatcherProvider.class, () -> matcherProvider);
298265
}
@@ -314,10 +281,6 @@ public void matches(String servletPath) {
314281
matches(mockRequest(servletPath));
315282
}
316283

317-
public void matches(String servletPath, String pathInfo) {
318-
matches(mockRequest(servletPath, pathInfo));
319-
}
320-
321284
private void matches(HttpServletRequest request) {
322285
assertThat(this.matcher.matches(request))
323286
.as("Matches " + getRequestPath(request)).isTrue();
@@ -327,20 +290,12 @@ public void doesNotMatch(String servletPath) {
327290
doesNotMatch(mockRequest(servletPath));
328291
}
329292

330-
public void doesNotMatch(String servletPath, String pathInfo) {
331-
doesNotMatch(mockRequest(servletPath, pathInfo));
332-
}
333-
334293
private void doesNotMatch(HttpServletRequest request) {
335294
assertThat(this.matcher.matches(request))
336295
.as("Does not match " + getRequestPath(request)).isFalse();
337296
}
338297

339298
private MockHttpServletRequest mockRequest(String servletPath) {
340-
return mockRequest(servletPath, null);
341-
}
342-
343-
private MockHttpServletRequest mockRequest(String servletPath, String path) {
344299
MockServletContext servletContext = new MockServletContext();
345300
servletContext.setAttribute(
346301
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
@@ -349,7 +304,6 @@ private MockHttpServletRequest mockRequest(String servletPath, String path) {
349304
if (servletPath != null) {
350305
request.setServletPath(servletPath);
351306
}
352-
request.setPathInfo(path);
353307
return request;
354308
}
355309

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/security/servlet/MvcEndpointRequestIntegrationTests.java

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@
5757
public class MvcEndpointRequestIntegrationTests
5858
extends AbstractEndpointRequestIntegrationTests {
5959

60+
@Test
61+
public void toLinksWhenServletPathSetShouldMatch() {
62+
getContextRunner().withPropertyValues("server.servlet.path=/admin")
63+
.run((context) -> {
64+
WebTestClient webTestClient = getWebTestClient(context);
65+
webTestClient.get().uri("/admin/actuator/").exchange().expectStatus()
66+
.isOk();
67+
webTestClient.get().uri("/admin/actuator").exchange().expectStatus()
68+
.isOk();
69+
});
70+
}
71+
6072
@Test
6173
public void toEndpointWhenServletPathSetShouldMatch() {
6274
getContextRunner().withPropertyValues("server.servlet.path=/admin")

0 commit comments

Comments
 (0)