Commit e6149fda authored by Madhura Bhave's avatar Madhura Bhave

Assert endpoints basePath starts with '/' or is empty

Fixes gh-12489
parent b8e86473
......@@ -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);
}
......
......@@ -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("");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment