Skip to content

Commit b229010

Browse files
committed
Polish "Trim trailing whitespace from spring.server.servlet.context-path"
Closes gh-16165
1 parent bde2f85 commit b229010

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

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

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

233233
private String cleanContextPath(String contextPath) {
234-
if (StringUtils.hasLength(contextPath)) {
235-
// remove leading and trailing whitespaces if any exists
236-
String ctxPath = StringUtils.trimWhitespace(contextPath);
237-
238-
if (ctxPath.endsWith("/")) {
239-
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
240-
}
241-
return ctxPath;
234+
String candidate = StringUtils.trimWhitespace(contextPath);
235+
if (StringUtils.hasText(candidate) && candidate.endsWith("/")) {
236+
return candidate.substring(0, candidate.length() - 1);
242237
}
243-
return contextPath;
238+
return candidate;
244239
}
245240

246241
public String getApplicationDisplayName() {

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

Lines changed: 10 additions & 10 deletions
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.
@@ -150,26 +150,26 @@ public void testSlashOfContextPathIsDefaultValue() {
150150
}
151151

152152
@Test
153-
public void makeSureTrailingAndLeadingWhitespacesRemoved_case1() {
153+
public void testContextPathWithLeadingWhitespace() {
154154
bind("server.servlet.context-path", " /assets");
155155
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
156156
}
157157

158158
@Test
159-
public void makeSureTrailingAndLeadingWhitespacesRemoved_case2() {
160-
bind("server.servlet.context-path", " /assets ");
161-
assertThat(this.properties.getServlet().getContextPath()).isEqualTo("/assets");
162-
}
163-
164-
@Test
165-
public void makeSureTrailingAndLeadingWhitespacesRemoved_case3() {
159+
public void testContextPathWithTrailingWhitespace() {
166160
bind("server.servlet.context-path", "/assets/copy/ ");
167161
assertThat(this.properties.getServlet().getContextPath())
168162
.isEqualTo("/assets/copy");
169163
}
170164

171165
@Test
172-
public void makeSureTrailingAndLeadingWhitespacesRemoved_case4() {
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() {
173173
bind("server.servlet.context-path", " /assets /copy/ ");
174174
assertThat(this.properties.getServlet().getContextPath())
175175
.isEqualTo("/assets /copy");

0 commit comments

Comments
 (0)