Skip to content

Commit 4e45f36

Browse files
committed
Clean basePath if necessary
This commit makes sure that ServletEndpointRegistrar does not add a duplicate `/` if the basePath is suffixed with it already Close gh-13964
1 parent 9de3d33 commit 4e45f36

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrar.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.boot.web.servlet.ServletContextInitializer;
2929
import org.springframework.util.Assert;
30+
import org.springframework.util.StringUtils;
3031

3132
/**
3233
* {@link ServletContextInitializer} to register {@link ExposableServletEndpoint servlet
@@ -47,10 +48,17 @@ public class ServletEndpointRegistrar implements ServletContextInitializer {
4748
public ServletEndpointRegistrar(String basePath,
4849
Collection<ExposableServletEndpoint> servletEndpoints) {
4950
Assert.notNull(servletEndpoints, "ServletEndpoints must not be null");
50-
this.basePath = (basePath != null) ? basePath : "";
51+
this.basePath = cleanBasePath(basePath);
5152
this.servletEndpoints = servletEndpoints;
5253
}
5354

55+
private static String cleanBasePath(String basePath) {
56+
if (StringUtils.hasText(basePath) && basePath.endsWith("/")) {
57+
return basePath.substring(0, basePath.length() - 1);
58+
}
59+
return (basePath != null) ? basePath : "";
60+
}
61+
5462
@Override
5563
public void onStartup(ServletContext servletContext) throws ServletException {
5664
this.servletEndpoints

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/ServletEndpointRegistrarTests.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.actuate.endpoint.web;
1818

19-
import java.io.IOException;
2019
import java.util.Collections;
2120

2221
import javax.servlet.GenericServlet;
@@ -47,6 +46,7 @@
4746
* Tests for {@link ServletEndpointRegistrar}.
4847
*
4948
* @author Phillip Webb
49+
* @author Stephane Nicoll
5050
*/
5151
public class ServletEndpointRegistrarTests {
5252

@@ -77,29 +77,38 @@ public void createWhenServletEndpointsIsNullShouldThrowException() {
7777
}
7878

7979
@Test
80-
public void onStartupShouldRegisterServlets() throws Exception {
81-
ExposableServletEndpoint endpoint = mockEndpoint(
82-
new EndpointServlet(TestServlet.class));
83-
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(null,
84-
Collections.singleton(endpoint));
85-
registrar.onStartup(this.servletContext);
86-
verify(this.servletContext).addServlet(eq("test-actuator-endpoint"),
87-
this.servlet.capture());
88-
assertThat(this.servlet.getValue()).isInstanceOf(TestServlet.class);
89-
verify(this.dynamic).addMapping("/test/*");
80+
public void onStartupShouldRegisterServlets() throws ServletException {
81+
assertBasePath(null, "/test/*");
82+
}
83+
84+
@Test
85+
public void onStartupWhenHasBasePathShouldIncludeBasePath() throws ServletException {
86+
assertBasePath("/actuator", "/actuator/test/*");
87+
}
88+
89+
@Test
90+
public void onStartupWhenHasEmptyBasePathShouldPrefixWithSlash()
91+
throws ServletException {
92+
assertBasePath("", "/test/*");
9093
}
9194

9295
@Test
93-
public void onStartupWhenHasBasePathShouldIncludeBasePath() throws Exception {
96+
public void onStartupWhenHasRootBasePathShouldNotAddDuplicateSlash()
97+
throws ServletException {
98+
assertBasePath("/", "/test/*");
99+
}
100+
101+
private void assertBasePath(String basePath, String expectedMapping)
102+
throws ServletException {
94103
ExposableServletEndpoint endpoint = mockEndpoint(
95104
new EndpointServlet(TestServlet.class));
96-
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator",
105+
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePath,
97106
Collections.singleton(endpoint));
98107
registrar.onStartup(this.servletContext);
99108
verify(this.servletContext).addServlet(eq("test-actuator-endpoint"),
100109
this.servlet.capture());
101110
assertThat(this.servlet.getValue()).isInstanceOf(TestServlet.class);
102-
verify(this.dynamic).addMapping("/actuator/test/*");
111+
verify(this.dynamic).addMapping(expectedMapping);
103112
}
104113

105114
@Test
@@ -124,8 +133,7 @@ private ExposableServletEndpoint mockEndpoint(EndpointServlet endpointServlet) {
124133
public static class TestServlet extends GenericServlet {
125134

126135
@Override
127-
public void service(ServletRequest req, ServletResponse res)
128-
throws ServletException, IOException {
136+
public void service(ServletRequest req, ServletResponse res) {
129137
}
130138

131139
}

0 commit comments

Comments
 (0)