Clean server.context-path if necessary

While the doc states that the default value is '/', setting that value
explicitly will lead to an error since we enforce that the default root
is the empty string.

Changing the doc will probably be more confusing than anything else so
we're now cleaning the user's provided value if necessary

Closes gh-3554
pull/3588/head
Stephane Nicoll 9 years ago
parent 37edee4f5e
commit 3b5fa5a6a3

@ -139,7 +139,14 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
this.contextPath = cleanContextPath(contextPath);
}
private String cleanContextPath(String contextPath) {
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) {
return contextPath.substring(0, contextPath.length() - 1);
}
return contextPath;
}
public String getDisplayName() {

@ -41,6 +41,7 @@ import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletCont
import org.springframework.boot.context.embedded.ServletContextInitializer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -123,6 +124,20 @@ public class ServerPropertiesTests {
.getInternalProxies());
}
@Test
public void testTrailingSlashOfContextPathIsRemoved() {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
Collections.singletonMap("server.contextPath", "/foo/")));
assertThat(this.properties.getContextPath(), equalTo("/foo"));
}
@Test
public void testSlashOfContextPathIsDefaultValue() {
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
Collections.singletonMap("server.contextPath", "/")));
assertThat(this.properties.getContextPath(), equalTo(""));
}
@Test
public void testCustomizeTomcat() throws Exception {
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);

Loading…
Cancel
Save