Skip to content

Commit 55e5baf

Browse files
committed
Merge pull request #16165 from mrifni
* pr/16165: Polish "Trim trailing whitespace from spring.server.servlet.context-path" Trim trailing whitespace from spring.server.servlet.context-path
2 parents 347daf6 + b229010 commit 55e5baf

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ public void setContextPath(String contextPath) {
231231
}
232232

233233
private String cleanContextPath(String contextPath) {
234-
if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) {
235-
return contextPath.substring(0, contextPath.length() - 1);
234+
String candidate = StringUtils.trimWhitespace(contextPath);
235+
if (StringUtils.hasText(candidate) && candidate.endsWith("/")) {
236+
return candidate.substring(0, candidate.length() - 1);
236237
}
237-
return contextPath;
238+
return candidate;
238239
}
239240

240241
public String getApplicationDisplayName() {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,6 +149,32 @@ public void testSlashOfContextPathIsDefaultValue() {
149149
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("");
150150
}
151151

152+
@Test
153+
public void testContextPathWithLeadingWhitespace() {
154+
bind("server.servlet.context-path", " /assets");
155+
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
156+
}
157+
158+
@Test
159+
public void testContextPathWithTrailingWhitespace() {
160+
bind("server.servlet.context-path", "/assets/copy/ ");
161+
assertThat(this.properties.getServlet().getContextPath())
162+
.isEqualTo("/assets/copy");
163+
}
164+
165+
@Test
166+
public void testContextPathWithLeadingAndTrailingWhitespace() {
167+
bind("server.servlet.context-path", " /assets ");
168+
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
169+
}
170+
171+
@Test
172+
public void testContextPathWithLeadingAndTrailingWhitespaceAndContextWithSpace() {
173+
bind("server.servlet.context-path", " /assets /copy/ ");
174+
assertThat(this.properties.getServlet().getContextPath())
175+
.isEqualTo("/assets /copy");
176+
}
177+
152178
@Test
153179
public void testCustomizeUriEncoding() {
154180
bind("server.tomcat.uri-encoding", "US-ASCII");

0 commit comments

Comments
 (0)