Skip to content

EndpointRequest.toLinks() does not match when management.endpoints.web.base-path is '/' #35595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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();
Expand All @@ -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");
}
Expand All @@ -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<Arguments> 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<Arguments> 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<Exception> expectedEx, String expectedExMsg) {
Assertions.assertThrows(
expectedEx, () -> new WebEndpointProperties().setBasePath(input), expectedExMsg);
}

private static Stream<Arguments> 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));
}
}