1616
1717package org .springframework .boot .actuate .endpoint .web ;
1818
19- import java .io .IOException ;
2019import java .util .Collections ;
2120
2221import javax .servlet .GenericServlet ;
4746 * Tests for {@link ServletEndpointRegistrar}.
4847 *
4948 * @author Phillip Webb
49+ * @author Stephane Nicoll
5050 */
5151public 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