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

See gh-16165
pull/16219/head
Mohamed Rifni 6 years ago committed by Stephane Nicoll
parent 347daf69fb
commit bde2f850b2

@ -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;
}

@ -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");

Loading…
Cancel
Save