UrlBuilder的toURI方法将url重复编码

This commit is contained in:
Looly
2022-08-09 21:14:49 +08:00
parent c4a33819b3
commit 0fdec254ed
4 changed files with 25 additions and 6 deletions

View File

@@ -544,12 +544,7 @@ public final class UrlBuilder implements Builder<String> {
*/
public URI toURI() {
try {
return new URI(
getSchemeWithDefault(),
getAuthority(),
getPathStr(),
getQueryStr(),
getFragmentEncoded());
return toURL().toURI();
} catch (URISyntaxException e) {
return null;
}

View File

@@ -445,4 +445,21 @@ public class UrlBuilderTest {
final UrlBuilder of = UrlBuilder.of(url, null);
Assert.assertEquals(url.replace("&amp;", "&"), of.toString());
}
@SuppressWarnings("ConstantConditions")
@Test
public void issues2503Test() throws URISyntaxException {
String duplicate = UrlBuilder.ofHttp("127.0.0.1:8080")
.addQuery("param[0].field", "编码")
.toURI()
.toString();
Assert.assertEquals("http://127.0.0.1:8080?param%5B0%5D.field=%E7%BC%96%E7%A0%81", duplicate);
String normal = UrlBuilder.ofHttp("127.0.0.1:8080")
.addQuery("param[0].field", "编码")
.toURL()
.toURI()
.toString();
Assert.assertEquals(duplicate, normal);
}
}