Merge branch '2.0.x'

pull/13963/merge
Stephane Nicoll 6 years ago
commit 0c1b229764

@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* {@link ServletContextInitializer} to register {@link ExposableServletEndpoint servlet * {@link ServletContextInitializer} to register {@link ExposableServletEndpoint servlet
@ -47,10 +48,17 @@ public class ServletEndpointRegistrar implements ServletContextInitializer {
public ServletEndpointRegistrar(String basePath, public ServletEndpointRegistrar(String basePath,
Collection<ExposableServletEndpoint> servletEndpoints) { Collection<ExposableServletEndpoint> servletEndpoints) {
Assert.notNull(servletEndpoints, "ServletEndpoints must not be null"); Assert.notNull(servletEndpoints, "ServletEndpoints must not be null");
this.basePath = (basePath != null) ? basePath : ""; this.basePath = cleanBasePath(basePath);
this.servletEndpoints = servletEndpoints; this.servletEndpoints = servletEndpoints;
} }
private static String cleanBasePath(String basePath) {
if (StringUtils.hasText(basePath) && basePath.endsWith("/")) {
return basePath.substring(0, basePath.length() - 1);
}
return (basePath != null) ? basePath : "";
}
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { public void onStartup(ServletContext servletContext) throws ServletException {
this.servletEndpoints this.servletEndpoints

@ -16,7 +16,6 @@
package org.springframework.boot.actuate.endpoint.web; package org.springframework.boot.actuate.endpoint.web;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import javax.servlet.GenericServlet; import javax.servlet.GenericServlet;
@ -47,6 +46,7 @@ import static org.mockito.Mockito.verify;
* Tests for {@link ServletEndpointRegistrar}. * Tests for {@link ServletEndpointRegistrar}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll
*/ */
public class ServletEndpointRegistrarTests { public class ServletEndpointRegistrarTests {
@ -77,29 +77,38 @@ public class ServletEndpointRegistrarTests {
} }
@Test @Test
public void onStartupShouldRegisterServlets() throws Exception { public void onStartupShouldRegisterServlets() throws ServletException {
ExposableServletEndpoint endpoint = mockEndpoint( assertBasePath(null, "/test/*");
new EndpointServlet(TestServlet.class)); }
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(null,
Collections.singleton(endpoint)); @Test
registrar.onStartup(this.servletContext); public void onStartupWhenHasBasePathShouldIncludeBasePath() throws ServletException {
verify(this.servletContext).addServlet(eq("test-actuator-endpoint"), assertBasePath("/actuator", "/actuator/test/*");
this.servlet.capture()); }
assertThat(this.servlet.getValue()).isInstanceOf(TestServlet.class);
verify(this.dynamic).addMapping("/test/*"); @Test
public void onStartupWhenHasEmptyBasePathShouldPrefixWithSlash()
throws ServletException {
assertBasePath("", "/test/*");
} }
@Test @Test
public void onStartupWhenHasBasePathShouldIncludeBasePath() throws Exception { public void onStartupWhenHasRootBasePathShouldNotAddDuplicateSlash()
throws ServletException {
assertBasePath("/", "/test/*");
}
private void assertBasePath(String basePath, String expectedMapping)
throws ServletException {
ExposableServletEndpoint endpoint = mockEndpoint( ExposableServletEndpoint endpoint = mockEndpoint(
new EndpointServlet(TestServlet.class)); new EndpointServlet(TestServlet.class));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePath,
Collections.singleton(endpoint)); Collections.singleton(endpoint));
registrar.onStartup(this.servletContext); registrar.onStartup(this.servletContext);
verify(this.servletContext).addServlet(eq("test-actuator-endpoint"), verify(this.servletContext).addServlet(eq("test-actuator-endpoint"),
this.servlet.capture()); this.servlet.capture());
assertThat(this.servlet.getValue()).isInstanceOf(TestServlet.class); assertThat(this.servlet.getValue()).isInstanceOf(TestServlet.class);
verify(this.dynamic).addMapping("/actuator/test/*"); verify(this.dynamic).addMapping(expectedMapping);
} }
@Test @Test
@ -124,8 +133,7 @@ public class ServletEndpointRegistrarTests {
public static class TestServlet extends GenericServlet { public static class TestServlet extends GenericServlet {
@Override @Override
public void service(ServletRequest req, ServletResponse res) public void service(ServletRequest req, ServletResponse res) {
throws ServletException, IOException {
} }
} }

Loading…
Cancel
Save