Parameterize URI parsing tests

Closes gh-33639
This commit is contained in:
rstoyanchev 2024-10-07 18:32:09 +01:00
parent d6fcad9ad7
commit 2389748e25
3 changed files with 231 additions and 176 deletions

View File

@ -518,9 +518,9 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
} }
path(record.path().toString()); path(record.path().toString());
query(record.query()); query(record.query());
if (StringUtils.hasText(record.fragment())) { }
fragment(record.fragment()); if (StringUtils.hasText(record.fragment())) {
} fragment(record.fragment());
} }
return this; return this;
} }

View File

@ -27,9 +27,12 @@ import java.util.Optional;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder.ParserType;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@ -47,8 +50,9 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/ */
class UriComponentsBuilderTests { class UriComponentsBuilderTests {
@Test // see gh-26453 @ParameterizedTest // see gh-26453
void examplesInReferenceManual() { @EnumSource(value = ParserType.class)
void examplesInReferenceManual(ParserType parserType) {
final String expected = "/hotel%20list/New%20York?q=foo%2Bbar"; final String expected = "/hotel%20list/New%20York?q=foo%2Bbar";
URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}") URI uri = UriComponentsBuilder.fromPath("/hotel list/{city}")
@ -63,7 +67,7 @@ class UriComponentsBuilderTests {
.build("New York", "foo+bar"); .build("New York", "foo+bar");
assertThat(uri).asString().isEqualTo(expected); assertThat(uri).asString().isEqualTo(expected);
uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}") uri = UriComponentsBuilder.fromUriString("/hotel list/{city}?q={q}", parserType)
.build("New York", "foo+bar"); .build("New York", "foo+bar");
assertThat(uri).asString().isEqualTo(expected); assertThat(uri).asString().isEqualTo(expected);
} }
@ -150,19 +154,21 @@ class UriComponentsBuilderTests {
assertThat(result.toUri()).as("Invalid result URI").isEqualTo(uri); assertThat(result.toUri()).as("Invalid result URI").isEqualTo(uri);
} }
@Test // SPR-9317 @ParameterizedTest // see gh-9317
void fromUriEncodedQuery() { @EnumSource(value = ParserType.class)
void fromUriEncodedQuery(ParserType parserType) {
URI uri = URI.create("https://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D"); URI uri = URI.create("https://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D");
String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0); String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0);
String fromUriString = UriComponentsBuilder.fromUriString(uri.toString()) String fromUriString = UriComponentsBuilder.fromUriString(uri.toString(), parserType)
.build().getQueryParams().get("param").get(0); .build().getQueryParams().get("param").get(0);
assertThat(fromUriString).isEqualTo(fromUri); assertThat(fromUriString).isEqualTo(fromUri);
} }
@Test @ParameterizedTest
void fromUriString() { @EnumSource(value = ParserType.class)
UriComponents result = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc3986.txt").build(); void fromUriString(ParserType parserType) {
UriComponents result = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc3986.txt", parserType).build();
assertThat(result.getScheme()).isEqualTo("https"); assertThat(result.getScheme()).isEqualTo("https");
assertThat(result.getUserInfo()).isNull(); assertThat(result.getUserInfo()).isNull();
assertThat(result.getHost()).isEqualTo("www.ietf.org"); assertThat(result.getHost()).isEqualTo("www.ietf.org");
@ -174,7 +180,7 @@ class UriComponentsBuilderTests {
String url = "https://arjen:foobar@java.sun.com:80" + String url = "https://arjen:foobar@java.sun.com:80" +
"/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)"; "/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)";
result = UriComponentsBuilder.fromUriString(url).build(); result = UriComponentsBuilder.fromUriString(url, parserType).build();
assertThat(result.getScheme()).isEqualTo("https"); assertThat(result.getScheme()).isEqualTo("https");
assertThat(result.getUserInfo()).isEqualTo("arjen:foobar"); assertThat(result.getUserInfo()).isEqualTo("arjen:foobar");
assertThat(result.getHost()).isEqualTo("java.sun.com"); assertThat(result.getHost()).isEqualTo("java.sun.com");
@ -186,7 +192,7 @@ class UriComponentsBuilderTests {
assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams); assertThat(result.getQueryParams()).isEqualTo(expectedQueryParams);
assertThat(result.getFragment()).isEqualTo("and(java.util.BitSet)"); assertThat(result.getFragment()).isEqualTo("and(java.util.BitSet)");
result = UriComponentsBuilder.fromUriString("mailto:java-net@java.sun.com#baz").build(); result = UriComponentsBuilder.fromUriString("mailto:java-net@java.sun.com#baz", parserType).build();
assertThat(result.getScheme()).isEqualTo("mailto"); assertThat(result.getScheme()).isEqualTo("mailto");
assertThat(result.getUserInfo()).isNull(); assertThat(result.getUserInfo()).isNull();
assertThat(result.getHost()).isNull(); assertThat(result.getHost()).isNull();
@ -196,7 +202,7 @@ class UriComponentsBuilderTests {
assertThat(result.getQuery()).isNull(); assertThat(result.getQuery()).isNull();
assertThat(result.getFragment()).isEqualTo("baz"); assertThat(result.getFragment()).isEqualTo("baz");
result = UriComponentsBuilder.fromUriString("mailto:user@example.com?subject=foo").build(); result = UriComponentsBuilder.fromUriString("mailto:user@example.com?subject=foo", parserType).build();
assertThat(result.getScheme()).isEqualTo("mailto"); assertThat(result.getScheme()).isEqualTo("mailto");
assertThat(result.getUserInfo()).isNull(); assertThat(result.getUserInfo()).isNull();
assertThat(result.getHost()).isNull(); assertThat(result.getHost()).isNull();
@ -206,7 +212,7 @@ class UriComponentsBuilderTests {
assertThat(result.getQuery()).isNull(); assertThat(result.getQuery()).isNull();
assertThat(result.getFragment()).isNull(); assertThat(result.getFragment()).isNull();
result = UriComponentsBuilder.fromUriString("docs/guide/collections/designfaq.html#28").build(); result = UriComponentsBuilder.fromUriString("docs/guide/collections/designfaq.html#28", parserType).build();
assertThat(result.getScheme()).isNull(); assertThat(result.getScheme()).isNull();
assertThat(result.getUserInfo()).isNull(); assertThat(result.getUserInfo()).isNull();
assertThat(result.getHost()).isNull(); assertThat(result.getHost()).isNull();
@ -216,69 +222,80 @@ class UriComponentsBuilderTests {
assertThat(result.getFragment()).isEqualTo("28"); assertThat(result.getFragment()).isEqualTo("28");
} }
@Test // SPR-9832 @ParameterizedTest // see SPR-9832
void fromUriStringQueryParamWithReservedCharInValue() { @EnumSource(value = ParserType.class)
void fromUriStringQueryParamWithReservedCharInValue(ParserType parserType) {
String uri = "https://www.google.com/ig/calculator?q=1USD=?EUR"; String uri = "https://www.google.com/ig/calculator?q=1USD=?EUR";
UriComponents result = UriComponentsBuilder.fromUriString(uri).build(); UriComponents result = UriComponentsBuilder.fromUriString(uri, parserType).build();
assertThat(result.getQuery()).isEqualTo("q=1USD=?EUR"); assertThat(result.getQuery()).isEqualTo("q=1USD=?EUR");
assertThat(result.getQueryParams().getFirst("q")).isEqualTo("1USD=?EUR"); assertThat(result.getQueryParams().getFirst("q")).isEqualTo("1USD=?EUR");
} }
@Test // SPR-14828 @ParameterizedTest // see SPR-14828
void fromUriStringQueryParamEncodedAndContainingPlus() { @EnumSource(value = ParserType.class)
void fromUriStringQueryParamEncodedAndContainingPlus(ParserType parserType) {
String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98"; String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98";
URI uri = UriComponentsBuilder.fromUriString(httpUrl).build(true).toUri(); URI uri = UriComponentsBuilder.fromUriString(httpUrl, parserType).build(true).toUri();
assertThat(uri.toString()).isEqualTo(httpUrl); assertThat(uri.toString()).isEqualTo(httpUrl);
} }
@Test // SPR-10539 @ParameterizedTest // see SPR-10539
void fromUriStringIPv6Host() { @EnumSource(value = ParserType.class)
void fromUriStringIPv6Host(ParserType parserType) {
UriComponents result = UriComponentsBuilder UriComponents result = UriComponentsBuilder
.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc]:8080/resource").build().encode(); .fromUriString("http://[1abc:2abc:3abc::5ABC:6abc]:8080/resource", parserType).build().encode();
assertThat(result.getHost()).isEqualToIgnoringCase("[1abc:2abc:3abc::5ABC:6abc]"); assertThat(result.getHost()).isEqualToIgnoringCase("[1abc:2abc:3abc::5ABC:6abc]");
} }
@Test @ParameterizedTest
void fromUriStringInvalidIPv6Host() { @EnumSource(value = ParserType.class)
void fromUriStringInvalidIPv6Host(ParserType parserType) {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource")); UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource", parserType));
} }
@Test // SPR-11970 @ParameterizedTest // see SPR-11970
void fromUriStringNoPathWithReservedCharInQuery() { @EnumSource(value = ParserType.class)
UriComponents result = UriComponentsBuilder.fromUriString("https://example.com?foo=bar@baz").build(); void fromUriStringNoPathWithReservedCharInQuery(ParserType parserType) {
UriComponents result = UriComponentsBuilder.fromUriString("https://example.com?foo=bar@baz", parserType).build();
assertThat(result.getUserInfo()).isNull(); assertThat(result.getUserInfo()).isNull();
assertThat(result.getHost()).isEqualTo("example.com"); assertThat(result.getHost()).isEqualTo("example.com");
assertThat(result.getQueryParams()).containsKey("foo"); assertThat(result.getQueryParams()).containsKey("foo");
assertThat(result.getQueryParams().getFirst("foo")).isEqualTo("bar@baz"); assertThat(result.getQueryParams().getFirst("foo")).isEqualTo("bar@baz");
} }
@Test // SPR-14828 @ParameterizedTest // see SPR-1428
void fromHttpUrlQueryParamEncodedAndContainingPlus() { @EnumSource(value = ParserType.class)
void fromHttpUrlQueryParamEncodedAndContainingPlus(ParserType parserType) {
String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98"; String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98";
URI uri = UriComponentsBuilder.fromUriString(httpUrl).build(true).toUri(); URI uri = UriComponentsBuilder.fromUriString(httpUrl, parserType).build(true).toUri();
assertThat(uri.toString()).isEqualTo(httpUrl); assertThat(uri.toString()).isEqualTo(httpUrl);
} }
@Test // SPR-10779 @ParameterizedTest // see SPR-10779
void fromHttpUrlCaseInsensitiveScheme() { @EnumSource(value = ParserType.class)
assertThat(UriComponentsBuilder.fromUriString("HTTP://www.google.com").build().getScheme()).isEqualTo("http"); void fromHttpUrlCaseInsensitiveScheme(ParserType parserType) {
assertThat(UriComponentsBuilder.fromUriString("HTTPS://www.google.com").build().getScheme()).isEqualTo("https"); assertThat(UriComponentsBuilder.fromUriString("HTTP://www.google.com", parserType).build().getScheme())
.isEqualTo("http");
assertThat(UriComponentsBuilder.fromUriString("HTTPS://www.google.com", parserType).build().getScheme())
.isEqualTo("https");
} }
@Test // SPR-10539 @ParameterizedTest // see SPR-10539
void fromHttpUrlInvalidIPv6Host() { @EnumSource(value = ParserType.class)
void fromHttpUrlInvalidIPv6Host(ParserType parserType) {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource")); UriComponentsBuilder.fromUriString("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource", parserType));
} }
@Test @ParameterizedTest
void fromHttpUrlWithoutFragment() { @EnumSource(value = ParserType.class)
void fromHttpUrlWithoutFragment(ParserType parserType) {
String httpUrl = "http://localhost:8080/test/print"; String httpUrl = "http://localhost:8080/test/print";
UriComponents uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build(); UriComponents uriComponents = UriComponentsBuilder.fromUriString(httpUrl, parserType).build();
assertThat(uriComponents.getScheme()).isEqualTo("http"); assertThat(uriComponents.getScheme()).isEqualTo("http");
assertThat(uriComponents.getUserInfo()).isNull(); assertThat(uriComponents.getUserInfo()).isNull();
assertThat(uriComponents.getHost()).isEqualTo("localhost"); assertThat(uriComponents.getHost()).isEqualTo("localhost");
@ -290,7 +307,7 @@ class UriComponentsBuilderTests {
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl); assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
httpUrl = "http://user:test@localhost:8080/test/print?foo=bar"; httpUrl = "http://user:test@localhost:8080/test/print?foo=bar";
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build(); uriComponents = UriComponentsBuilder.fromUriString(httpUrl, parserType).build();
assertThat(uriComponents.getScheme()).isEqualTo("http"); assertThat(uriComponents.getScheme()).isEqualTo("http");
assertThat(uriComponents.getUserInfo()).isEqualTo("user:test"); assertThat(uriComponents.getUserInfo()).isEqualTo("user:test");
assertThat(uriComponents.getHost()).isEqualTo("localhost"); assertThat(uriComponents.getHost()).isEqualTo("localhost");
@ -302,7 +319,7 @@ class UriComponentsBuilderTests {
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl); assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
httpUrl = "http://localhost:8080/test/print?foo=bar"; httpUrl = "http://localhost:8080/test/print?foo=bar";
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build(); uriComponents = UriComponentsBuilder.fromUriString(httpUrl, parserType).build();
assertThat(uriComponents.getScheme()).isEqualTo("http"); assertThat(uriComponents.getScheme()).isEqualTo("http");
assertThat(uriComponents.getUserInfo()).isNull(); assertThat(uriComponents.getUserInfo()).isNull();
assertThat(uriComponents.getHost()).isEqualTo("localhost"); assertThat(uriComponents.getHost()).isEqualTo("localhost");
@ -314,10 +331,11 @@ class UriComponentsBuilderTests {
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl); assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
} }
@Test // gh-25300 @ParameterizedTest // see gh-25300
void fromHttpUrlWithFragment() { @EnumSource(value = ParserType.class)
void fromHttpUrlWithFragment(ParserType parserType) {
String httpUrl = "https://example.com/#baz"; String httpUrl = "https://example.com/#baz";
UriComponents uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build(); UriComponents uriComponents = UriComponentsBuilder.fromUriString(httpUrl, parserType).build();
assertThat(uriComponents.getScheme()).isEqualTo("https"); assertThat(uriComponents.getScheme()).isEqualTo("https");
assertThat(uriComponents.getUserInfo()).isNull(); assertThat(uriComponents.getUserInfo()).isNull();
assertThat(uriComponents.getHost()).isEqualTo("example.com"); assertThat(uriComponents.getHost()).isEqualTo("example.com");
@ -329,7 +347,7 @@ class UriComponentsBuilderTests {
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl); assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
httpUrl = "http://localhost:8080/test/print#baz"; httpUrl = "http://localhost:8080/test/print#baz";
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build(); uriComponents = UriComponentsBuilder.fromUriString(httpUrl, parserType).build();
assertThat(uriComponents.getScheme()).isEqualTo("http"); assertThat(uriComponents.getScheme()).isEqualTo("http");
assertThat(uriComponents.getUserInfo()).isNull(); assertThat(uriComponents.getUserInfo()).isNull();
assertThat(uriComponents.getHost()).isEqualTo("localhost"); assertThat(uriComponents.getHost()).isEqualTo("localhost");
@ -341,7 +359,7 @@ class UriComponentsBuilderTests {
assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl); assertThat(uriComponents.toUri().toString()).isEqualTo(httpUrl);
httpUrl = "http://localhost:8080/test/print?foo=bar#baz"; httpUrl = "http://localhost:8080/test/print?foo=bar#baz";
uriComponents = UriComponentsBuilder.fromUriString(httpUrl).build(); uriComponents = UriComponentsBuilder.fromUriString(httpUrl, parserType).build();
assertThat(uriComponents.getScheme()).isEqualTo("http"); assertThat(uriComponents.getScheme()).isEqualTo("http");
assertThat(uriComponents.getUserInfo()).isNull(); assertThat(uriComponents.getUserInfo()).isNull();
assertThat(uriComponents.getHost()).isEqualTo("localhost"); assertThat(uriComponents.getHost()).isEqualTo("localhost");
@ -429,30 +447,32 @@ class UriComponentsBuilderTests {
assertThat(uriComponents.getPath()).isEqualTo("/foo/bar"); assertThat(uriComponents.getPath()).isEqualTo("/foo/bar");
} }
@Test @ParameterizedTest
void replacePath() { @EnumSource(value = ParserType.class)
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc2396.txt"); void replacePath(ParserType parserType) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc2396.txt", parserType);
builder.replacePath("/rfc/rfc3986.txt"); builder.replacePath("/rfc/rfc3986.txt");
UriComponents result = builder.build(); UriComponents result = builder.build();
assertThat(result.toUriString()).isEqualTo("https://www.ietf.org/rfc/rfc3986.txt"); assertThat(result.toUriString()).isEqualTo("https://www.ietf.org/rfc/rfc3986.txt");
builder = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc2396.txt"); builder = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc2396.txt", parserType);
builder.replacePath(null); builder.replacePath(null);
result = builder.build(); result = builder.build();
assertThat(result.toUriString()).isEqualTo("https://www.ietf.org"); assertThat(result.toUriString()).isEqualTo("https://www.ietf.org");
} }
@Test @ParameterizedTest
void replaceQuery() { @EnumSource(value = ParserType.class)
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.com/foo?foo=bar&baz=qux"); void replaceQuery(ParserType parserType) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.com/foo?foo=bar&baz=qux", parserType);
builder.replaceQuery("baz=42"); builder.replaceQuery("baz=42");
UriComponents result = builder.build(); UriComponents result = builder.build();
assertThat(result.toUriString()).isEqualTo("https://example.com/foo?baz=42"); assertThat(result.toUriString()).isEqualTo("https://example.com/foo?baz=42");
builder = UriComponentsBuilder.fromUriString("https://example.com/foo?foo=bar&baz=qux"); builder = UriComponentsBuilder.fromUriString("https://example.com/foo?foo=bar&baz=qux", parserType);
builder.replaceQuery(null); builder.replaceQuery(null);
result = builder.build(); result = builder.build();
@ -583,36 +603,40 @@ class UriComponentsBuilderTests {
assertThat(result.toUriString()).isEqualTo("/fooValue/barValue"); assertThat(result.toUriString()).isEqualTo("/fooValue/barValue");
} }
@Test @ParameterizedTest
void buildAndExpandOpaque() { @EnumSource(value = ParserType.class)
UriComponents result = UriComponentsBuilder.fromUriString("mailto:{user}@{domain}") void buildAndExpandOpaque(ParserType parserType) {
UriComponents result = UriComponentsBuilder.fromUriString("mailto:{user}@{domain}", parserType)
.buildAndExpand("foo", "example.com"); .buildAndExpand("foo", "example.com");
assertThat(result.toUriString()).isEqualTo("mailto:foo@example.com"); assertThat(result.toUriString()).isEqualTo("mailto:foo@example.com");
Map<String, String> values = new HashMap<>(); Map<String, String> values = new HashMap<>();
values.put("user", "foo"); values.put("user", "foo");
values.put("domain", "example.com"); values.put("domain", "example.com");
UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand(values); UriComponentsBuilder.fromUriString("mailto:{user}@{domain}", parserType).buildAndExpand(values);
assertThat(result.toUriString()).isEqualTo("mailto:foo@example.com"); assertThat(result.toUriString()).isEqualTo("mailto:foo@example.com");
} }
@Test @ParameterizedTest
void queryParamWithValueWithEquals() { @EnumSource(value = ParserType.class)
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar=baz").build(); void queryParamWithValueWithEquals(ParserType parserType) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar=baz", parserType).build();
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar=baz"); assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar=baz");
assertThat(uriComponents.getQueryParams().get("bar")).containsExactly("baz"); assertThat(uriComponents.getQueryParams().get("bar")).containsExactly("baz");
} }
@Test @ParameterizedTest
void queryParamWithoutValueWithEquals() { @EnumSource(value = ParserType.class)
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar=").build(); void queryParamWithoutValueWithEquals(ParserType parserType) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar=", parserType).build();
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar="); assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar=");
assertThat(uriComponents.getQueryParams().get("bar")).element(0).asString().isEmpty(); assertThat(uriComponents.getQueryParams().get("bar")).element(0).asString().isEmpty();
} }
@Test @ParameterizedTest
void queryParamWithoutValueWithoutEquals() { @EnumSource(value = ParserType.class)
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar").build(); void queryParamWithoutValueWithoutEquals(ParserType parserType) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar", parserType).build();
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar"); assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar");
// TODO [SPR-13537] Change equalTo(null) to equalTo(""). // TODO [SPR-13537] Change equalTo(null) to equalTo("").
@ -634,46 +658,50 @@ class UriComponentsBuilderTests {
assertThat(result.toUri()).isEqualTo(uri); assertThat(result.toUri()).isEqualTo(uri);
} }
@Test @ParameterizedTest
void relativeUrls() { @EnumSource(value = ParserType.class)
void relativeUrls(ParserType parserType) {
String baseUrl = "https://example.com"; String baseUrl = "https://example.com";
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toString()) assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar", parserType).build().toString())
.isEqualTo(baseUrl + (parserType == ParserType.WHAT_WG ? "/bar" : "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar", parserType).build().toUriString())
.isEqualTo(baseUrl + (parserType == ParserType.WHAT_WG ? "/bar" : "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar", parserType).build().toUri().getPath())
.isEqualTo((parserType == ParserType.WHAT_WG ? "/bar" : "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).path("foo/../bar").build().toString())
.isEqualTo(baseUrl + "/foo/../bar"); .isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUriString()) assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).path("foo/../bar").build().toUriString())
.isEqualTo(baseUrl + "/foo/../bar"); .isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUri().getPath()) assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).path("foo/../bar").build().toUri().getPath())
.isEqualTo("/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toString())
.isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUriString())
.isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUri().getPath())
.isEqualTo("/foo/../bar"); .isEqualTo("/foo/../bar");
} }
@Test @ParameterizedTest
void emptySegments() { @EnumSource(value = ParserType.class)
void emptySegments(ParserType parserType) {
String baseUrl = "https://example.com/abc/"; String baseUrl = "https://example.com/abc/";
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/y/z").build().toString()) assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).path("/x/y/z").build().toString())
.isEqualTo("https://example.com/abc/x/y/z"); .isEqualTo("https://example.com/abc/x/y/z");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x", "y", "z").build().toString()) assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).pathSegment("x", "y", "z").build().toString())
.isEqualTo("https://example.com/abc/x/y/z"); .isEqualTo("https://example.com/abc/x/y/z");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/").path("/y/z").build().toString()) assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).path("/x/").path("/y/z").build().toString())
.isEqualTo("https://example.com/abc/x/y/z"); .isEqualTo("https://example.com/abc/x/y/z");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x").path("y").build().toString()) assertThat(UriComponentsBuilder.fromUriString(baseUrl, parserType).pathSegment("x").path("y").build().toString())
.isEqualTo("https://example.com/abc/x/y"); .isEqualTo("https://example.com/abc/x/y");
} }
@Test @ParameterizedTest
void parsesEmptyFragment() { @EnumSource(value = ParserType.class)
UriComponents components = UriComponentsBuilder.fromUriString("/example#").build(); void parsesEmptyFragment(ParserType parserType) {
UriComponents components = UriComponentsBuilder.fromUriString("/example#", parserType).build();
assertThat(components.getFragment()).isNull(); assertThat(components.getFragment()).isNull();
assertThat(components.toString()).isEqualTo("/example"); assertThat(components.toString()).isEqualTo("/example");
} }
@Test // SPR-13257 @ParameterizedTest // SPR-13257
void parsesEmptyUri() { @EnumSource(value = ParserType.class)
UriComponents components = UriComponentsBuilder.fromUriString("").build(); void parsesEmptyUri(ParserType parserType) {
UriComponents components = UriComponentsBuilder.fromUriString("", parserType).build();
assertThat(components.toString()).isEmpty(); assertThat(components.toString()).isEmpty();
} }
@ -733,17 +761,18 @@ class UriComponentsBuilderTests {
assertThat(result1.getSchemeSpecificPart()).isNull(); assertThat(result1.getSchemeSpecificPart()).isNull();
} }
@Test // gh-26466 @ParameterizedTest // gh-26466
void encodeTemplateWithInvalidPlaceholderSyntax() { @EnumSource(value = ParserType.class)
void encodeTemplateWithInvalidPlaceholderSyntax(ParserType parserType) {
BiConsumer<String, String> tester = (in, out) -> BiConsumer<String, String> tester = (in, out) ->
assertThat(UriComponentsBuilder.fromUriString(in).encode().toUriString()).isEqualTo(out); assertThat(UriComponentsBuilder.fromUriString(in, parserType).encode().toUriString()).isEqualTo(out);
// empty // empty
tester.accept("{}", "%7B%7D"); tester.accept("{}", "%7B%7D");
tester.accept("{ \t}", "%7B%20%09%7D"); tester.accept("{ \t}", (parserType == ParserType.WHAT_WG ? "%7B%20%7D" : "%7B%20%09%7D"));
tester.accept("/a{}b", "/a%7B%7Db"); tester.accept("/a{}b", "/a%7B%7Db");
tester.accept("/a{ \t}b", "/a%7B%20%09%7Db"); tester.accept("/a{ \t}b", (parserType == ParserType.WHAT_WG ? "/a%7B%20%7Db" : "/a%7B%20%09%7Db"));
// nested, matching // nested, matching
tester.accept("{foo{}}", "%7Bfoo%7B%7D%7D"); tester.accept("{foo{}}", "%7Bfoo%7B%7D%7D");
@ -762,28 +791,32 @@ class UriComponentsBuilderTests {
tester.accept("/a{year:\\d{1,4}}b", "/a{year:\\d{1,4}}b"); tester.accept("/a{year:\\d{1,4}}b", "/a{year:\\d{1,4}}b");
} }
@Test // SPR-16364 @ParameterizedTest // SPR-16364
void uriComponentsNotEqualAfterNormalization() { @EnumSource(value = ParserType.class)
UriComponents uri1 = UriComponentsBuilder.fromUriString("http://test.com").build().normalize(); void uriComponentsNotEqualAfterNormalization(ParserType parserType) {
UriComponents uri2 = UriComponentsBuilder.fromUriString("http://test.com/").build(); UriComponents uri1 = UriComponentsBuilder.fromUriString("http://test.com", parserType).build().normalize();
UriComponents uri2 = UriComponentsBuilder.fromUriString("http://test.com/", parserType).build();
assertThat(uri1.getPathSegments()).isEmpty(); assertThat(uri1.getPathSegments()).isEmpty();
assertThat(uri2.getPathSegments()).isEmpty(); assertThat(uri2.getPathSegments()).isEmpty();
assertThat(uri2).isNotEqualTo(uri1); assertThat(uri2).isNotEqualTo(uri1);
} }
@Test // SPR-17256 @ParameterizedTest // SPR-17256
void uriComponentsWithMergedQueryParams() { @EnumSource(value = ParserType.class)
String uri = UriComponentsBuilder.fromUriString("http://localhost:8081") void uriComponentsWithMergedQueryParams(ParserType parserType) {
.uriComponents(UriComponentsBuilder.fromUriString("/{path}?sort={sort}").build()) String uri = UriComponentsBuilder.fromUriString("http://localhost:8081", parserType)
.uriComponents(UriComponentsBuilder.fromUriString("/{path}?sort={sort}", parserType).build())
.queryParam("sort", "another_value").build().toString(); .queryParam("sort", "another_value").build().toString();
assertThat(uri).isEqualTo("http://localhost:8081/{path}?sort={sort}&sort=another_value"); assertThat(uri).isEqualTo("http://localhost:8081/{path}?sort={sort}&sort=another_value");
} }
@Test // SPR-17630 @ParameterizedTest // SPR-17630
void toUriStringWithCurlyBraces() { @EnumSource(value = ParserType.class)
assertThat(UriComponentsBuilder.fromUriString("/path?q={asa}asa").toUriString()).isEqualTo("/path?q=%7Basa%7Dasa"); void toUriStringWithCurlyBraces(ParserType parserType) {
assertThat(UriComponentsBuilder.fromUriString("/path?q={asa}asa", parserType).toUriString())
.isEqualTo("/path?q=%7Basa%7Dasa");
} }
@Test // gh-26012 @Test // gh-26012
@ -792,34 +825,37 @@ class UriComponentsBuilderTests {
assertThat(path).isEqualTo("/home/path"); assertThat(path).isEqualTo("/home/path");
} }
@Test @ParameterizedTest
void validPort() { @EnumSource(value = ParserType.class)
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567/path").build(); void validPort(ParserType parserType) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567/path", parserType).build();
assertThat(uriComponents.getPort()).isEqualTo(52567); assertThat(uriComponents.getPort()).isEqualTo(52567);
assertThat(uriComponents.getPath()).isEqualTo("/path"); assertThat(uriComponents.getPath()).isEqualTo("/path");
uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567?trace=false").build(); uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567?trace=false", parserType).build();
assertThat(uriComponents.getPort()).isEqualTo(52567); assertThat(uriComponents.getPort()).isEqualTo(52567);
assertThat(uriComponents.getQuery()).isEqualTo("trace=false"); assertThat(uriComponents.getQuery()).isEqualTo("trace=false");
uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567#fragment").build(); uriComponents = UriComponentsBuilder.fromUriString("http://localhost:52567#fragment", parserType).build();
assertThat(uriComponents.getPort()).isEqualTo(52567); assertThat(uriComponents.getPort()).isEqualTo(52567);
assertThat(uriComponents.getFragment()).isEqualTo("fragment"); assertThat(uriComponents.getFragment()).isEqualTo("fragment");
} }
@Test @ParameterizedTest
void verifyInvalidPort() { @EnumSource(value = ParserType.class)
void verifyInvalidPort(ParserType parserType) {
String url = "http://localhost:XXX/path"; String url = "http://localhost:XXX/path";
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> UriComponentsBuilder.fromUriString(url).build().toUri()); .isThrownBy(() -> UriComponentsBuilder.fromUriString(url, parserType).build().toUri());
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
.isThrownBy(() -> UriComponentsBuilder.fromUriString(url).build().toUri()); .isThrownBy(() -> UriComponentsBuilder.fromUriString(url, parserType).build().toUri());
} }
@Test // gh-27039 @ParameterizedTest // gh-27039
void expandPortAndPathWithoutSeparator() { @EnumSource(value = ParserType.class)
void expandPortAndPathWithoutSeparator(ParserType parserType) {
URI uri = UriComponentsBuilder URI uri = UriComponentsBuilder
.fromUriString("ws://localhost:{port}/{path}") .fromUriString("ws://localhost:{port}/{path}", parserType)
.buildAndExpand(7777, "test") .buildAndExpand(7777, "test")
.toUri(); .toUri();
assertThat(uri.toString()).isEqualTo("ws://localhost:7777/test"); assertThat(uri.toString()).isEqualTo("ws://localhost:7777/test");

View File

@ -25,6 +25,10 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.web.util.UriComponentsBuilder.ParserType;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@ -74,78 +78,88 @@ class UriComponentsTests {
assertThat(uri.expand("JavaClass$1.class").toString()).isEqualTo("/path?q=JavaClass%241.class"); assertThat(uri.expand("JavaClass$1.class").toString()).isEqualTo("/path?q=JavaClass%241.class");
} }
@Test @ParameterizedTest
void toUriEncoded() { @EnumSource(value = ParserType.class)
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build(); void toUriEncoded(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich", parserType).build();
assertThat(uri.encode().toUri()).isEqualTo(URI.create("https://example.com/hotel%20list/Z%C3%BCrich")); assertThat(uri.encode().toUri()).isEqualTo(URI.create("https://example.com/hotel%20list/Z%C3%BCrich"));
} }
@Test @ParameterizedTest
void toUriNotEncoded() { @EnumSource(value = ParserType.class)
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build(); void toUriNotEncoded(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich", parserType).build();
assertThat(uri.toUri()).isEqualTo(URI.create("https://example.com/hotel%20list/Z\u00fcrich")); assertThat(uri.toUri()).isEqualTo(URI.create("https://example.com/hotel%20list/Z\u00fcrich"));
} }
@Test @ParameterizedTest
void toUriAlreadyEncoded() { @EnumSource(value = ParserType.class)
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel%20list/Z%C3%BCrich").build(true); void toUriAlreadyEncoded(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel%20list/Z%C3%BCrich", parserType).build(true);
assertThat(uri.encode().toUri()).isEqualTo(URI.create("https://example.com/hotel%20list/Z%C3%BCrich")); assertThat(uri.encode().toUri()).isEqualTo(URI.create("https://example.com/hotel%20list/Z%C3%BCrich"));
} }
@Test @ParameterizedTest
void toUriWithIpv6HostAlreadyEncoded() { @EnumSource(value = ParserType.class)
void toUriWithIpv6HostAlreadyEncoded(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString( UriComponents uri = UriComponentsBuilder.fromUriString(
"http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich").build(true); "http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich", parserType).build(true);
assertThat(uri.encode().toUri()).isEqualTo( assertThat(uri.encode().toUri()).isEqualTo(
URI.create("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich")); URI.create("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich"));
} }
@Test @ParameterizedTest
void toUriStringWithPortVariable() { @EnumSource(value = ParserType.class)
void toUriStringWithPortVariable(ParserType parserType) {
String url = "http://localhost:{port}/first"; String url = "http://localhost:{port}/first";
assertThat(UriComponentsBuilder.fromUriString(url).build().toUriString()).isEqualTo(url); assertThat(UriComponentsBuilder.fromUriString(url, parserType).build().toUriString()).isEqualTo(url);
} }
@Test @ParameterizedTest
void expand() { @EnumSource(value = ParserType.class)
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com").path("/{foo} {bar}").build(); void expand(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com", parserType).path("/{foo} {bar}").build();
uri = uri.expand("1 2", "3 4"); uri = uri.expand("1 2", "3 4");
assertThat(uri.getPath()).isEqualTo("/1 2 3 4"); assertThat(uri.getPath()).isEqualTo("/1 2 3 4");
assertThat(uri.toUriString()).isEqualTo("https://example.com/1 2 3 4"); assertThat(uri.toUriString()).isEqualTo("https://example.com/1 2 3 4");
} }
@Test // SPR-13311 @ParameterizedTest // SPR-13311
void expandWithRegexVar() { @EnumSource(value = ParserType.class)
void expandWithRegexVar(ParserType parserType) {
String template = "/myurl/{name:[a-z]{1,5}}/show"; String template = "/myurl/{name:[a-z]{1,5}}/show";
UriComponents uri = UriComponentsBuilder.fromUriString(template).build(); UriComponents uri = UriComponentsBuilder.fromUriString(template, parserType).build();
uri = uri.expand(Collections.singletonMap("name", "test")); uri = uri.expand(Collections.singletonMap("name", "test"));
assertThat(uri.getPath()).isEqualTo("/myurl/test/show"); assertThat(uri.getPath()).isEqualTo("/myurl/test/show");
} }
@Test // SPR-17630 @ParameterizedTest // SPR-17630
void uirTemplateExpandWithMismatchedCurlyBraces() { @EnumSource(value = ParserType.class)
UriComponents uri = UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build(); void uirTemplateExpandWithMismatchedCurlyBraces(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString("/myurl/?q={{{{", parserType).encode().build();
assertThat(uri.toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B"); assertThat(uri.toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B");
} }
@Test // gh-22447 @ParameterizedTest // gh-22447
void expandWithFragmentOrder() { @EnumSource(value = ParserType.class)
void expandWithFragmentOrder(ParserType parserType) {
UriComponents uri = UriComponentsBuilder UriComponents uri = UriComponentsBuilder
.fromUriString("https://{host}/{path}#{fragment}").build() .fromUriString("https://{host}/{path}#{fragment}", parserType).build()
.expand("example.com", "foo", "bar"); .expand("example.com", "foo", "bar");
assertThat(uri.toUriString()).isEqualTo("https://example.com/foo#bar"); assertThat(uri.toUriString()).isEqualTo("https://example.com/foo#bar");
} }
@Test // SPR-12123 @ParameterizedTest // SPR-12123
void port() { @EnumSource(value = ParserType.class)
UriComponents uri1 = fromUriString("https://example.com:8080/bar").build(); void port(ParserType parserType) {
UriComponents uri2 = fromUriString("https://example.com/bar").port(8080).build(); UriComponents uri1 = fromUriString("https://example.com:8080/bar", parserType).build();
UriComponents uri3 = fromUriString("https://example.com/bar").port("{port}").build().expand(8080); UriComponents uri2 = fromUriString("https://example.com/bar", parserType).port(8080).build();
UriComponents uri4 = fromUriString("https://example.com/bar").port("808{digit}").build().expand(0); UriComponents uri3 = fromUriString("https://example.com/bar", parserType).port("{port}").build().expand(8080);
UriComponents uri4 = fromUriString("https://example.com/bar", parserType).port("808{digit}").build().expand(0);
assertThat(uri1.getPort()).isEqualTo(8080); assertThat(uri1.getPort()).isEqualTo(8080);
assertThat(uri1.toUriString()).isEqualTo("https://example.com:8080/bar"); assertThat(uri1.toUriString()).isEqualTo("https://example.com:8080/bar");
@ -157,11 +171,12 @@ class UriComponentsTests {
assertThat(uri4.toUriString()).isEqualTo("https://example.com:8080/bar"); assertThat(uri4.toUriString()).isEqualTo("https://example.com:8080/bar");
} }
@Test // gh-28521 @ParameterizedTest // gh-28521
void invalidPort() { @EnumSource(value = ParserType.class)
void invalidPort(ParserType parserType) {
assertThatExceptionOfType(InvalidUrlException.class) assertThatExceptionOfType(InvalidUrlException.class)
.isThrownBy(() -> fromUriString("https://example.com:XXX/bar")); .isThrownBy(() -> fromUriString("https://example.com:XXX/bar", parserType));
assertExceptionsForInvalidPort(fromUriString("https://example.com/bar").port("XXX").build()); assertExceptionsForInvalidPort(fromUriString("https://example.com/bar", parserType).port("XXX").build());
} }
private void assertExceptionsForInvalidPort(UriComponents uriComponents) { private void assertExceptionsForInvalidPort(UriComponents uriComponents) {
@ -191,16 +206,18 @@ class UriComponentsTests {
UriComponentsBuilder.fromPath("/fo%2o").build(true)); UriComponentsBuilder.fromPath("/fo%2o").build(true));
} }
@Test @ParameterizedTest
void normalize() { @EnumSource(value = ParserType.class)
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build(); void normalize(ParserType parserType) {
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar", parserType).build();
assertThat(uri.normalize().toString()).isEqualTo("https://example.com/bar"); assertThat(uri.normalize().toString()).isEqualTo("https://example.com/bar");
} }
@Test @ParameterizedTest
void serializable() throws Exception { @EnumSource(value = ParserType.class)
void serializable(ParserType parserType) throws Exception {
UriComponents uri = UriComponentsBuilder.fromUriString( UriComponents uri = UriComponentsBuilder.fromUriString(
"https://example.com").path("/{foo}").query("bar={baz}").build(); "https://example.com", parserType).path("/{foo}").query("bar={baz}").build();
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos); ObjectOutputStream oos = new ObjectOutputStream(bos);
@ -222,12 +239,13 @@ class UriComponentsTests {
assertThat(result.getPathSegments()).isEqualTo(Arrays.asList("foo", "bar", "ba%2Fz")); assertThat(result.getPathSegments()).isEqualTo(Arrays.asList("foo", "bar", "ba%2Fz"));
} }
@Test @ParameterizedTest
void equalsHierarchicalUriComponents() { @EnumSource(value = ParserType.class)
void equalsHierarchicalUriComponents(ParserType parserType) {
String url = "https://example.com"; String url = "https://example.com";
UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); UriComponents uric1 = UriComponentsBuilder.fromUriString(url, parserType).path("/{foo}").query("bar={baz}").build();
UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); UriComponents uric2 = UriComponentsBuilder.fromUriString(url, parserType).path("/{foo}").query("bar={baz}").build();
UriComponents uric3 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bin={baz}").build(); UriComponents uric3 = UriComponentsBuilder.fromUriString(url, parserType).path("/{foo}").query("bin={baz}").build();
assertThat(uric1).isInstanceOf(HierarchicalUriComponents.class); assertThat(uric1).isInstanceOf(HierarchicalUriComponents.class);
assertThat(uric1).isEqualTo(uric1); assertThat(uric1).isEqualTo(uric1);
@ -235,12 +253,13 @@ class UriComponentsTests {
assertThat(uric1).isNotEqualTo(uric3); assertThat(uric1).isNotEqualTo(uric3);
} }
@Test @ParameterizedTest
void equalsOpaqueUriComponents() { @EnumSource(value = ParserType.class)
void equalsOpaqueUriComponents(ParserType parserType) {
String baseUrl = "http:example.com"; String baseUrl = "http:example.com";
UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build(); UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar", parserType).build();
UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build(); UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar", parserType).build();
UriComponents uric3 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bin").build(); UriComponents uric3 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bin", parserType).build();
assertThat(uric1).isEqualTo(uric1); assertThat(uric1).isEqualTo(uric1);
assertThat(uric1).isEqualTo(uric2); assertThat(uric1).isEqualTo(uric2);