diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index ebe9d5133e5..f5009604064 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -85,7 +85,7 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable { private static final String HOST_PATTERN = "(" + HOST_IPV6_PATTERN + "|" + HOST_IPV4_PATTERN + ")"; - private static final String PORT_PATTERN = "(\\d*(?:\\{[^/]+?})?)"; + private static final String PORT_PATTERN = "(.[^/]*(?:\\{[^/]+?})?)"; private static final String PATH_PATTERN = "([^?#]*)"; diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index 1db9b40628c..7577f54429a 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -38,6 +38,7 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.fail; /** * Unit tests for {@link UriComponentsBuilder}. @@ -1272,4 +1273,42 @@ class UriComponentsBuilderTests { assertThat(path).isEqualTo("/home/path"); } + + @Test + void verifyNonNumberPort() { + UriComponents numberPort = UriComponentsBuilder.fromUriString("http://localhost:8080/path").build(); + assertThat(numberPort.getScheme()).isEqualTo("http"); + assertThat(numberPort.getHost()).isEqualTo("localhost"); + assertThat(numberPort.getPort()).isEqualTo(8080); + assertThat(numberPort.getPath()).isEqualTo("/path"); + + UriComponents stringPort = UriComponentsBuilder.fromUriString("http://localhost:port/path").build(); + try{ + stringPort.getPort(); + fail("port must be a number"); + }catch (NumberFormatException e){ + + } + assertThat(stringPort.getScheme()).isEqualTo("http"); + assertThat(stringPort.getHost()).isEqualTo("localhost"); + assertThat(stringPort.getPath()).isEqualTo("/path"); + + UriComponents httpNumberPort = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/path").build(); + assertThat(httpNumberPort.getScheme()).isEqualTo("http"); + assertThat(httpNumberPort.getPort()).isEqualTo(8080); + assertThat(httpNumberPort.getHost()).isEqualTo("localhost"); + assertThat(httpNumberPort.getPath()).isEqualTo("/path"); + + UriComponents httpStringPort= UriComponentsBuilder.fromHttpUrl("http://localhost:port/path").build(); + try{ + httpStringPort.getPort(); + fail("port must be a number"); + }catch (NumberFormatException e){ + + } + assertThat(httpStringPort.getScheme()).isEqualTo("http"); + assertThat(httpStringPort.getHost()).isEqualTo("localhost"); + assertThat(httpStringPort.getPath()).isEqualTo("/path"); + } + }