|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
20 | 20 | import static org.junit.Assert.assertThrows; |
| 21 | +import static org.junit.Assume.assumeNoException; |
21 | 22 |
|
22 | 23 | import com.google.common.net.InetAddresses; |
23 | 24 | import com.google.common.testing.EqualsTester; |
| 25 | +import java.net.Inet6Address; |
24 | 26 | import java.net.URISyntaxException; |
| 27 | +import java.net.UnknownHostException; |
25 | 28 | import java.util.BitSet; |
26 | 29 | import org.junit.Test; |
27 | 30 | import org.junit.runner.RunWith; |
@@ -77,6 +80,20 @@ public void parse_ipv6Literal_noPort() throws URISyntaxException { |
77 | 80 | assertThat(uri.getPort()).isLessThan(0); |
78 | 81 | } |
79 | 82 |
|
| 83 | + @Test |
| 84 | + public void parse_ipv6ScopedLiteral() throws URISyntaxException { |
| 85 | + Uri uri = Uri.parse("http://[fe80::1%25eth0]"); |
| 86 | + assertThat(uri.getRawHost()).isEqualTo("[fe80::1%25eth0]"); |
| 87 | + assertThat(uri.getHost()).isEqualTo("[fe80::1%eth0]"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void parse_ipv6ScopedPercentEncodedLiteral() throws URISyntaxException { |
| 92 | + Uri uri = Uri.parse("http://[fe80::1%25foo-bar%2Fblah]"); |
| 93 | + assertThat(uri.getRawHost()).isEqualTo("[fe80::1%25foo-bar%2Fblah]"); |
| 94 | + assertThat(uri.getHost()).isEqualTo("[fe80::1%foo-bar/blah]"); |
| 95 | + } |
| 96 | + |
80 | 97 | @Test |
81 | 98 | public void parse_noQuery() throws URISyntaxException { |
82 | 99 | Uri uri = Uri.parse("scheme://authority/path#fragment"); |
@@ -203,6 +220,13 @@ public void parse_invalidBackslashInHost_throws() { |
203 | 220 | assertThat(e).hasMessageThat().contains("Invalid character in host"); |
204 | 221 | } |
205 | 222 |
|
| 223 | + @Test |
| 224 | + public void parse_invalidBackslashScope_throws() { |
| 225 | + URISyntaxException e = |
| 226 | + assertThrows(URISyntaxException.class, () -> Uri.parse("http://[::1%25foo\\bar]")); |
| 227 | + assertThat(e).hasMessageThat().contains("Invalid character in scope"); |
| 228 | + } |
| 229 | + |
206 | 230 | @Test |
207 | 231 | public void parse_emptyPort_throws() { |
208 | 232 | URISyntaxException e = |
@@ -397,6 +421,47 @@ public void builder_ipv6Literal() throws URISyntaxException { |
397 | 421 | assertThat(uri.toString()).isEqualTo("scheme://[2001:4860:4860::8844]"); |
398 | 422 | } |
399 | 423 |
|
| 424 | + @Test |
| 425 | + public void builder_ipv6ScopedLiteral_numeric() throws UnknownHostException { |
| 426 | + Uri uri = |
| 427 | + Uri.newBuilder() |
| 428 | + .setScheme("http") |
| 429 | + // Create an address with a numeric scope_id, which should always be valid. |
| 430 | + .setHost( |
| 431 | + Inet6Address.getByAddress(null, InetAddresses.forString("fe80::1").getAddress(), 1)) |
| 432 | + .build(); |
| 433 | + |
| 434 | + // We expect the scope ID to be percent encoded. |
| 435 | + assertThat(uri.getRawHost()).isEqualTo("[fe80::1%251]"); |
| 436 | + assertThat(uri.getHost()).isEqualTo("[fe80::1%1]"); |
| 437 | + } |
| 438 | + |
| 439 | + @Test |
| 440 | + public void builder_ipv6ScopedLiteral_named() throws UnknownHostException { |
| 441 | + // Unfortunately, there's no Java API to create an Inet6Address with an arbitrary interface- |
| 442 | + // scoped name. There's actually no way to hermetically create an Inet6Address with a scope name |
| 443 | + // at all! The following address/interface is likely to be present on Linux test runners. |
| 444 | + Inet6Address address; |
| 445 | + try { |
| 446 | + address = (Inet6Address) InetAddresses.forString("::1%lo"); |
| 447 | + } catch (IllegalArgumentException e) { |
| 448 | + assumeNoException(e); |
| 449 | + return; // Not reached. |
| 450 | + } |
| 451 | + Uri uri = Uri.newBuilder().setScheme("http").setHost(address).build(); |
| 452 | + |
| 453 | + // We expect the scope ID to be percent encoded. |
| 454 | + assertThat(uri.getRawHost()).isEqualTo("[::1%25lo]"); |
| 455 | + assertThat(uri.getHost()).isEqualTo("[::1%lo]"); |
| 456 | + } |
| 457 | + |
| 458 | + @Test |
| 459 | + public void builder_ipv6PercentEncodedScopedLiteral() { |
| 460 | + Uri uri = Uri.newBuilder().setScheme("http").setRawHost("[fe80::1%25foo%2Dbar%2Fblah]").build(); |
| 461 | + assertThat(uri.getRawHost()).isEqualTo("[fe80::1%25foo%2Dbar%2Fblah]"); |
| 462 | + assertThat(uri.getHost()).isEqualTo("[fe80::1%foo-bar/blah]"); |
| 463 | + } |
| 464 | + |
400 | 465 | @Test |
401 | 466 | public void builder_encodingWithAllowedReservedChars() throws URISyntaxException { |
402 | 467 | Uri uri = |
|
0 commit comments