Commit bde2f850 authored by Mohamed Rifni's avatar Mohamed Rifni Committed by Stephane Nicoll

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

See gh-16165
parent 347daf69
...@@ -231,8 +231,14 @@ public class ServerProperties { ...@@ -231,8 +231,14 @@ public class ServerProperties {
} }
private String cleanContextPath(String contextPath) { private String cleanContextPath(String contextPath) {
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { if (StringUtils.hasLength(contextPath)) {
return contextPath.substring(0, contextPath.length() - 1); // 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; return contextPath;
} }
......
...@@ -149,6 +149,32 @@ public class ServerPropertiesTests { ...@@ -149,6 +149,32 @@ public class ServerPropertiesTests {
assertThat(this.properties.getServlet().getContextPath()).isEqualTo(""); 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 @Test
public void testCustomizeUriEncoding() { public void testCustomizeUriEncoding() {
bind("server.tomcat.uri-encoding", "US-ASCII"); bind("server.tomcat.uri-encoding", "US-ASCII");
......
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