diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java index 06a9223eaf62..2ecd45da2f2a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java @@ -67,7 +67,7 @@ public void setBasePath(String basePath) { } private String cleanBasePath(String basePath) { - if (StringUtils.hasText(basePath) && basePath.endsWith("/")) { + if (StringUtils.hasText(basePath) && basePath.endsWith("/") && StringUtils.cleanPath(basePath).length() != 1) { return basePath.substring(0, basePath.length() - 1); } return basePath; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java index 441d15bf6645..1e0e607175ec 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointPropertiesTests.java @@ -16,10 +16,18 @@ package org.springframework.boot.actuate.autoconfigure.endpoint.web; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.junit.Assert.assertEquals; /** * Tests for {@link WebEndpointProperties}. @@ -28,6 +36,8 @@ */ class WebEndpointPropertiesTests { + private static final String BLANK_ERR_MSG = "Base path must start with '/' or be empty"; + @Test void defaultBasePathShouldBeApplication() { WebEndpointProperties properties = new WebEndpointProperties(); @@ -38,7 +48,7 @@ void defaultBasePathShouldBeApplication() { void basePathShouldBeCleaned() { WebEndpointProperties properties = new WebEndpointProperties(); properties.setBasePath("/"); - assertThat(properties.getBasePath()).isEmpty(); + assertThat(properties.getBasePath()).isNotEmpty(); properties.setBasePath("/actuator/"); assertThat(properties.getBasePath()).isEqualTo("/actuator"); } @@ -57,4 +67,60 @@ void basePathCanBeEmpty() { assertThat(properties.getBasePath()).isEmpty(); } + @ParameterizedTest + @MethodSource("notToBeCleanedArguments") + void basePathShouldNotBeCleaned(String path, String expected) { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(path); + assertThat(properties.getBasePath()).isNotEmpty(); + assertEquals(properties.getBasePath(), expected); + } + + private static Stream notToBeCleanedArguments() { + return Stream.of( + Arguments.of("/path", "/path"), + Arguments.of("/path/", "/path"), + Arguments.of("/", "/"), + Arguments.of("/path/path2", "/path/path2"), + Arguments.of("/path/path2/", "/path/path2")); + } + + @ParameterizedTest + @MethodSource("notEmptyPaths") + void basePathShouldNotBeEmpty(String path) { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(path); + assertThat(properties.getBasePath()).isNotEmpty(); + } + + private static Stream notEmptyPaths() { + return Stream.of(Arguments.of("/path"), Arguments.of("/path/")); + } + + @ParameterizedTest + @ValueSource(strings = {""}) + void basePathShouldBeEmpty(String path) { + WebEndpointProperties properties = new WebEndpointProperties(); + properties.setBasePath(path); + assertThat(properties.getBasePath()).isEmpty(); + } + + @ParameterizedTest + @MethodSource("argumentsForExceptions") + void basePathShouldThrowException(String input, Class expectedEx, String expectedExMsg) { + Assertions.assertThrows( + expectedEx, () -> new WebEndpointProperties().setBasePath(input), expectedExMsg); + } + + private static Stream argumentsForExceptions() { + return Stream.of( + Arguments.of( + "invalidpath", + IllegalArgumentException.class, + BLANK_ERR_MSG), + Arguments.of( + " ", IllegalArgumentException.class, BLANK_ERR_MSG), + Arguments.of( + "null", IllegalArgumentException.class, BLANK_ERR_MSG)); + } }