From bde2f850b2182dff7ac35223d723d50af4ddc944 Mon Sep 17 00:00:00 2001 From: Mohamed Rifni Date: Thu, 7 Mar 2019 14:40:27 +0000 Subject: [PATCH 1/2] Trim trailing whitespace from spring.server.servlet.context-path See gh-16165 --- .../autoconfigure/web/ServerProperties.java | 10 +++++-- .../web/ServerPropertiesTests.java | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 86d9814c75..a27cd7d248 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -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; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index d09db2f56e..4b7f561c3b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -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"); From b2290107db9fdb9ab7c023d67d537c91d8e6903c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 12 Mar 2019 17:46:17 +0100 Subject: [PATCH 2/2] Polish "Trim trailing whitespace from spring.server.servlet.context-path" Closes gh-16165 --- .../autoconfigure/web/ServerProperties.java | 13 ++++-------- .../web/ServerPropertiesTests.java | 20 +++++++++---------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index a27cd7d248..38bfdf617a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -231,16 +231,11 @@ public class ServerProperties { } private String cleanContextPath(String contextPath) { - 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; + String candidate = StringUtils.trimWhitespace(contextPath); + if (StringUtils.hasText(candidate) && candidate.endsWith("/")) { + return candidate.substring(0, candidate.length() - 1); } - return contextPath; + return candidate; } public String getApplicationDisplayName() { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 4b7f561c3b..28b373677e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -1,5 +1,5 @@ /* - * 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"); * you may not use this file except in compliance with the License. @@ -150,26 +150,26 @@ public class ServerPropertiesTests { } @Test - public void makeSureTrailingAndLeadingWhitespacesRemoved_case1() { + public void testContextPathWithLeadingWhitespace() { 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() { + public void testContextPathWithTrailingWhitespace() { bind("server.servlet.context-path", "/assets/copy/ "); assertThat(this.properties.getServlet().getContextPath()) .isEqualTo("/assets/copy"); } @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/ "); assertThat(this.properties.getServlet().getContextPath()) .isEqualTo("/assets /copy");