Assert endpoints basePath starts with '/' or is empty

Fixes gh-12489
This commit is contained in:
Madhura Bhave
2018-03-15 11:33:59 -07:00
parent b8e8647391
commit e6149fda1c
2 changed files with 25 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
@@ -56,6 +57,8 @@ public class WebEndpointProperties {
}
public void setBasePath(String basePath) {
Assert.isTrue(basePath.isEmpty() || basePath.startsWith("/"),
"Base path must start with '/' or be empty");
this.basePath = cleanBasePath(basePath);
}

View File

@@ -16,7 +16,9 @@
package org.springframework.boot.actuate.autoconfigure.endpoint.web;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
@@ -27,6 +29,9 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class WebEndpointPropertiesTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void defaultBasePathShouldBeApplication() {
WebEndpointProperties properties = new WebEndpointProperties();
@@ -38,6 +43,23 @@ public class WebEndpointPropertiesTests {
WebEndpointProperties properties = new WebEndpointProperties();
properties.setBasePath("/");
assertThat(properties.getBasePath()).isEqualTo("");
properties.setBasePath("/actuator/");
assertThat(properties.getBasePath()).isEqualTo("/actuator");
}
@Test
public void basePathMustStartWithSlash() {
WebEndpointProperties properties = new WebEndpointProperties();
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Base path must start with '/' or be empty");
properties.setBasePath("admin");
}
@Test
public void basePathCanBeEmpty() {
WebEndpointProperties properties = new WebEndpointProperties();
properties.setBasePath("");
assertThat(properties.getBasePath()).isEqualTo("");
}
}