Commit b2290107 authored by Stephane Nicoll's avatar Stephane Nicoll

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

Closes gh-16165
parent bde2f850
...@@ -231,16 +231,11 @@ public class ServerProperties { ...@@ -231,16 +231,11 @@ public class ServerProperties {
} }
private String cleanContextPath(String contextPath) { private String cleanContextPath(String contextPath) {
if (StringUtils.hasLength(contextPath)) { String candidate = StringUtils.trimWhitespace(contextPath);
// remove leading and trailing whitespaces if any exists if (StringUtils.hasText(candidate) && candidate.endsWith("/")) {
String ctxPath = StringUtils.trimWhitespace(contextPath); return candidate.substring(0, candidate.length() - 1);
if (ctxPath.endsWith("/")) {
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
}
return ctxPath;
} }
return contextPath; return candidate;
} }
public String getApplicationDisplayName() { public String getApplicationDisplayName() {
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -150,26 +150,26 @@ public class ServerPropertiesTests { ...@@ -150,26 +150,26 @@ public class ServerPropertiesTests {
} }
@Test @Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case1() { public void testContextPathWithLeadingWhitespace() {
bind("server.servlet.context-path", " /assets"); bind("server.servlet.context-path", " /assets");
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets"); assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
} }
@Test @Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case2() { public void testContextPathWithTrailingWhitespace() {
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/ "); bind("server.servlet.context-path", "/assets/copy/ ");
assertThat(this.properties.getServlet().getContextPath()) assertThat(this.properties.getServlet().getContextPath())
.isEqualTo("/assets/copy"); .isEqualTo("/assets/copy");
} }
@Test @Test
public void makeSureTrailingAndLeadingWhitespacesRemoved_case4() { public void testContextPathWithLeadingAndTrailingWhitespace() {
bind("server.servlet.context-path", " /assets ");
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
}
@Test
public void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
bind("server.servlet.context-path", " /assets /copy/ "); bind("server.servlet.context-path", " /assets /copy/ ");
assertThat(this.properties.getServlet().getContextPath()) assertThat(this.properties.getServlet().getContextPath())
.isEqualTo("/assets /copy"); .isEqualTo("/assets /copy");
......
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