Skip to content

Commit

Permalink
Lenient port handling in HierarchicalUriComponents#toUriString
Browse files Browse the repository at this point in the history
Closes gh-32003
  • Loading branch information
rstoyanchev committed Jan 10, 2024
1 parent 2b4ffe0 commit 50fad9e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -485,7 +485,7 @@ public String toUriString() {
if (this.host != null) {
uriBuilder.append(this.host);
}
if (getPort() != -1) {
if (StringUtils.hasText(this.port) && !this.port.equals("-1")) {
uriBuilder.append(':').append(this.port);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ void toUriWithIpv6HostAlreadyEncoded() {
URI.create("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"));
}

@Test
void toUriStringWithPortVariable() {
String url = "http://localhost:{port}/first";
assertThat(UriComponentsBuilder.fromUriString(url).build().toUriString()).isEqualTo(url);
}

@Test
void expand() {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com").path("/{foo} {bar}").build();
Expand Down Expand Up @@ -166,12 +172,6 @@ private void assertExceptionsForInvalidPort(UriComponents uriComponents) {
assertThatIllegalStateException()
.isThrownBy(uriComponents::toUri)
.withMessage("The port must be an integer: XXX");
assertThatIllegalStateException()
.isThrownBy(uriComponents::toUriString)
.withMessage("The port must be an integer: XXX");
assertThatIllegalStateException()
.isThrownBy(uriComponents::toString)
.withMessage("The port must be an integer: XXX");
}

@Test
Expand Down

0 comments on commit 50fad9e

Please sign in to comment.