Trim trailing whitespace from spring.server.servlet.context-path

See gh-16165
This commit is contained in:
Mohamed Rifni
2019-03-07 14:40:27 +00:00
committed by Stephane Nicoll
parent 347daf69fb
commit bde2f850b2
2 changed files with 34 additions and 2 deletions

View File

@@ -231,8 +231,14 @@ public class ServerProperties {
}
private String cleanContextPath(String contextPath) {
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) {
return contextPath.substring(0, contextPath.length() - 1);
if (StringUtils.hasLength(contextPath)) {
// remove leading and trailing whitespaces if any exists
String ctxPath = StringUtils.trimWhitespace(contextPath);
if (ctxPath.endsWith("/")) {
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
}
return ctxPath;
}
return contextPath;
}

View File

@@ -149,6 +149,32 @@ public class ServerPropertiesTests {
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("");
}
@Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case1() {
bind("server.servlet.context-path", " /assets");
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
}
@Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case2() {
bind("server.servlet.context-path", " /assets ");
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
}
@Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case3() {
bind("server.servlet.context-path", "/assets/copy/ ");
assertThat(this.properties.getServlet().getContextPath())
.isEqualTo("/assets/copy");
}
@Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case4() {
bind("server.servlet.context-path", " /assets /copy/ ");
assertThat(this.properties.getServlet().getContextPath())
.isEqualTo("/assets /copy");
}
@Test
public void testCustomizeUriEncoding() {
bind("server.tomcat.uri-encoding", "US-ASCII");