|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2002-2019 the original author or authors. |
|
|
|
|
* Copyright 2002-2020 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. |
|
|
|
@ -43,7 +43,6 @@ public class UriComponentsTests {
@@ -43,7 +43,6 @@ public class UriComponentsTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void expandAndEncode() { |
|
|
|
|
|
|
|
|
|
UriComponents uri = UriComponentsBuilder |
|
|
|
|
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").build() |
|
|
|
|
.expand("Z\u00fcrich", "a+b").encode(); |
|
|
|
@ -53,7 +52,6 @@ public class UriComponentsTests {
@@ -53,7 +52,6 @@ public class UriComponentsTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void encodeAndExpand() { |
|
|
|
|
|
|
|
|
|
UriComponents uri = UriComponentsBuilder |
|
|
|
|
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode().build() |
|
|
|
|
.expand("Z\u00fcrich", "a+b"); |
|
|
|
@ -63,16 +61,14 @@ public class UriComponentsTests {
@@ -63,16 +61,14 @@ public class UriComponentsTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void encodeAndExpandPartially() { |
|
|
|
|
|
|
|
|
|
UriComponents uri = UriComponentsBuilder |
|
|
|
|
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode() |
|
|
|
|
.uriVariables(Collections.singletonMap("city", "Z\u00fcrich")) |
|
|
|
|
.build(); |
|
|
|
|
.uriVariables(Collections.singletonMap("city", "Z\u00fcrich")).build(); |
|
|
|
|
|
|
|
|
|
assertThat(uri.expand("a+b").toString()).isEqualTo("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // SPR-17168
|
|
|
|
|
@Test // SPR-17168
|
|
|
|
|
public void encodeAndExpandWithDollarSign() { |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build(); |
|
|
|
|
assertThat(uri.expand("JavaClass$1.class").toString()).isEqualTo("/path?q=JavaClass%241.class"); |
|
|
|
@ -80,71 +76,71 @@ public class UriComponentsTests {
@@ -80,71 +76,71 @@ public class UriComponentsTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void toUriEncoded() throws URISyntaxException { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString( |
|
|
|
|
"https://example.com/hotel list/Z\u00fcrich").build(); |
|
|
|
|
assertThat(uriComponents.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich")); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build(); |
|
|
|
|
assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void toUriNotEncoded() throws URISyntaxException { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString( |
|
|
|
|
"https://example.com/hotel list/Z\u00fcrich").build(); |
|
|
|
|
assertThat(uriComponents.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z\u00fcrich")); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel list/Z\u00fcrich").build(); |
|
|
|
|
assertThat(uri.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z\u00fcrich")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void toUriAlreadyEncoded() throws URISyntaxException { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString( |
|
|
|
|
"https://example.com/hotel%20list/Z%C3%BCrich").build(true); |
|
|
|
|
UriComponents encoded = uriComponents.encode(); |
|
|
|
|
assertThat(encoded.toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich")); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/hotel%20list/Z%C3%BCrich").build(true); |
|
|
|
|
assertThat(uri.encode().toUri()).isEqualTo(new URI("https://example.com/hotel%20list/Z%C3%BCrich")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void toUriWithIpv6HostAlreadyEncoded() throws URISyntaxException { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString( |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString( |
|
|
|
|
"http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich").build(true); |
|
|
|
|
UriComponents encoded = uriComponents.encode(); |
|
|
|
|
assertThat(encoded.toUri()).isEqualTo(new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich")); |
|
|
|
|
|
|
|
|
|
assertThat(uri.encode().toUri()).isEqualTo( |
|
|
|
|
new URI("http://[1abc:2abc:3abc::5ABC:6abc]:8080/hotel%20list/Z%C3%BCrich")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void expand() { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString( |
|
|
|
|
"https://example.com").path("/{foo} {bar}").build(); |
|
|
|
|
uriComponents = uriComponents.expand("1 2", "3 4"); |
|
|
|
|
assertThat(uriComponents.getPath()).isEqualTo("/1 2 3 4"); |
|
|
|
|
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/1 2 3 4"); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com").path("/{foo} {bar}").build(); |
|
|
|
|
uri = uri.expand("1 2", "3 4"); |
|
|
|
|
|
|
|
|
|
assertThat(uri.getPath()).isEqualTo("/1 2 3 4"); |
|
|
|
|
assertThat(uri.toUriString()).isEqualTo("https://example.com/1 2 3 4"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // SPR-13311
|
|
|
|
|
@Test // SPR-13311
|
|
|
|
|
public void expandWithRegexVar() { |
|
|
|
|
String template = "/myurl/{name:[a-z]{1,5}}/show"; |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build(); |
|
|
|
|
uriComponents = uriComponents.expand(Collections.singletonMap("name", "test")); |
|
|
|
|
assertThat(uriComponents.getPath()).isEqualTo("/myurl/test/show"); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString(template).build(); |
|
|
|
|
uri = uri.expand(Collections.singletonMap("name", "test")); |
|
|
|
|
|
|
|
|
|
assertThat(uri.getPath()).isEqualTo("/myurl/test/show"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // SPR-17630
|
|
|
|
|
@Test // SPR-17630
|
|
|
|
|
public void uirTemplateExpandWithMismatchedCurlyBraces() { |
|
|
|
|
assertThat(UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build().toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B"); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString("/myurl/?q={{{{").encode().build(); |
|
|
|
|
assertThat(uri.toUriString()).isEqualTo("/myurl/?q=%7B%7B%7B%7B"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // gh-22447
|
|
|
|
|
@Test // gh-22447
|
|
|
|
|
public void expandWithFragmentOrder() { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder |
|
|
|
|
UriComponents uri = UriComponentsBuilder |
|
|
|
|
.fromUriString("https://{host}/{path}#{fragment}").build() |
|
|
|
|
.expand("example.com", "foo", "bar"); |
|
|
|
|
|
|
|
|
|
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo#bar"); |
|
|
|
|
assertThat(uri.toUriString()).isEqualTo("https://example.com/foo#bar"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test // SPR-12123
|
|
|
|
|
@Test // SPR-12123
|
|
|
|
|
public void port() { |
|
|
|
|
UriComponents uri1 = fromUriString("https://example.com:8080/bar").build(); |
|
|
|
|
UriComponents uri2 = fromUriString("https://example.com/bar").port(8080).build(); |
|
|
|
|
UriComponents uri3 = fromUriString("https://example.com/bar").port("{port}").build().expand(8080); |
|
|
|
|
UriComponents uri4 = fromUriString("https://example.com/bar").port("808{digit}").build().expand(0); |
|
|
|
|
|
|
|
|
|
assertThat(uri1.getPort()).isEqualTo(8080); |
|
|
|
|
assertThat(uri1.toUriString()).isEqualTo("https://example.com:8080/bar"); |
|
|
|
|
assertThat(uri2.getPort()).isEqualTo(8080); |
|
|
|
@ -175,20 +171,22 @@ public class UriComponentsTests {
@@ -175,20 +171,22 @@ public class UriComponentsTests {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void normalize() { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build(); |
|
|
|
|
assertThat(uriComponents.normalize().toString()).isEqualTo("https://example.com/bar"); |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString("https://example.com/foo/../bar").build(); |
|
|
|
|
assertThat(uri.normalize().toString()).isEqualTo("https://example.com/bar"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void serializable() throws Exception { |
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder.fromUriString( |
|
|
|
|
UriComponents uri = UriComponentsBuilder.fromUriString( |
|
|
|
|
"https://example.com").path("/{foo}").query("bar={baz}").build(); |
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
|
|
|
ObjectOutputStream oos = new ObjectOutputStream(bos); |
|
|
|
|
oos.writeObject(uriComponents); |
|
|
|
|
oos.writeObject(uri); |
|
|
|
|
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); |
|
|
|
|
UriComponents readObject = (UriComponents) ois.readObject(); |
|
|
|
|
assertThat(uriComponents.toString()).isEqualTo(readObject.toString()); |
|
|
|
|
|
|
|
|
|
assertThat(uri.toString()).isEqualTo(readObject.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@ -197,6 +195,7 @@ public class UriComponentsTests {
@@ -197,6 +195,7 @@ public class UriComponentsTests {
|
|
|
|
|
UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance(); |
|
|
|
|
source.copyToUriComponentsBuilder(targetBuilder); |
|
|
|
|
UriComponents result = targetBuilder.build().encode(); |
|
|
|
|
|
|
|
|
|
assertThat(result.getPath()).isEqualTo("/foo/bar/ba%2Fz"); |
|
|
|
|
assertThat(result.getPathSegments()).isEqualTo(Arrays.asList("foo", "bar", "ba%2Fz")); |
|
|
|
|
} |
|
|
|
@ -207,6 +206,7 @@ public class UriComponentsTests {
@@ -207,6 +206,7 @@ public class UriComponentsTests {
|
|
|
|
|
UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); |
|
|
|
|
UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build(); |
|
|
|
|
UriComponents uric3 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bin={baz}").build(); |
|
|
|
|
|
|
|
|
|
assertThat(uric1).isInstanceOf(HierarchicalUriComponents.class); |
|
|
|
|
assertThat(uric1).isEqualTo(uric1); |
|
|
|
|
assertThat(uric1).isEqualTo(uric2); |
|
|
|
@ -219,6 +219,7 @@ public class UriComponentsTests {
@@ -219,6 +219,7 @@ public class UriComponentsTests {
|
|
|
|
|
UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build(); |
|
|
|
|
UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build(); |
|
|
|
|
UriComponents uric3 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bin").build(); |
|
|
|
|
|
|
|
|
|
assertThat(uric1).isInstanceOf(OpaqueUriComponents.class); |
|
|
|
|
assertThat(uric1).isEqualTo(uric1); |
|
|
|
|
assertThat(uric1).isEqualTo(uric2); |
|
|
|
|