• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  /*
2   * Copyright (C) 2010 The Android Open Source Project
3   *
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package libcore.java.net;
18  
19  import junit.framework.TestCase;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import libcore.libcore.util.SerializationTester;
23  
24  public final class URITest extends TestCase {
25  
testUriParts()26      public void testUriParts() throws Exception {
27          URI uri = new URI("http://username:password@host:8080/directory/file?query#ref");
28          assertEquals("http", uri.getScheme());
29          assertEquals("username:password@host:8080", uri.getAuthority());
30          assertEquals("username:password@host:8080", uri.getRawAuthority());
31          assertEquals("username:password", uri.getUserInfo());
32          assertEquals("username:password", uri.getRawUserInfo());
33          assertEquals("host", uri.getHost());
34          assertEquals(8080, uri.getPort());
35          assertEquals("/directory/file", uri.getPath());
36          assertEquals("/directory/file", uri.getRawPath());
37          assertEquals("query", uri.getQuery());
38          assertEquals("query", uri.getRawQuery());
39          assertEquals("ref", uri.getFragment());
40          assertEquals("ref", uri.getRawFragment());
41          assertEquals("//username:password@host:8080/directory/file?query",
42                  uri.getSchemeSpecificPart());
43          assertEquals("//username:password@host:8080/directory/file?query",
44                  uri.getRawSchemeSpecificPart());
45      }
46  
testEqualsCaseMapping()47      public void testEqualsCaseMapping() throws Exception {
48          assertEquals(new URI("HTTP://localhost/foo?bar=baz#quux"),
49                  new URI("HTTP://localhost/foo?bar=baz#quux"));
50          assertEquals(new URI("http://localhost/foo?bar=baz#quux"),
51                  new URI("http://LOCALHOST/foo?bar=baz#quux"));
52          assertFalse(new URI("http://localhost/foo?bar=baz#quux")
53                  .equals(new URI("http://localhost/FOO?bar=baz#quux")));
54          assertFalse(new URI("http://localhost/foo?bar=baz#quux")
55                  .equals(new URI("http://localhost/foo?BAR=BAZ#quux")));
56          assertFalse(new URI("http://localhost/foo?bar=baz#quux")
57                  .equals(new URI("http://localhost/foo?bar=baz#QUUX")));
58      }
59  
testEqualsEscaping()60      public void testEqualsEscaping() throws Exception {
61          // Case insensitive when comparing escaped values, but not when
62          // comparing unescaped values.
63          assertEquals(new URI("http://localhost/foo?bar=fooobar%E0%AE%A8%E0bar"),
64                  new URI("http://localhost/foo?bar=fooobar%E0%AE%a8%e0bar"));
65          assertFalse(new URI("http://localhost/foo?bar=fooobar%E0%AE%A8%E0bar").equals(
66                  new URI("http://localhost/foo?bar=FoooBar%E0%AE%a8%e0bar")));
67          assertFalse(new URI("http://localhost/foo?bar=fooobar%E0%AE%A8%E0bar").equals(
68                  new URI("http://localhost/foo?bar=fooobar%E0%AE%a8%e0BaR")));
69  
70          // Last byte replaced by an unescaped value.
71          assertFalse(new URI("http://localhost/foo?bar=%E0%AE%A8%E0").equals(
72                  new URI("http://localhost/foo?bar=%E0%AE%a8xxx")));
73          // Missing byte.
74          assertFalse(new URI("http://localhost/foo?bar=%E0%AE%A8%E0").equals(
75                  new URI("http://localhost/foo?bar=%E0%AE%a8")));
76      }
77  
testFileEqualsWithEmptyHost()78      public void testFileEqualsWithEmptyHost() throws Exception {
79          assertEquals(new URI("file", "", "/a/", null), new URI("file:/a/"));
80          assertEquals(new URI("file", null, "/a/", null), new URI("file:/a/"));
81      }
82  
testUriSerialization()83      public void testUriSerialization() throws Exception {
84          String s = "aced00057372000c6a6176612e6e65742e555249ac01782e439e49ab0300014c0006737472696e6"
85                  + "77400124c6a6176612f6c616e672f537472696e673b787074002a687474703a2f2f757365723a706"
86                  + "1737340686f73742f706174682f66696c653f7175657279236861736878";
87          URI uri = new URI("http://user:pass@host/path/file?query#hash");
88          new SerializationTester<URI>(uri, s).test();
89      }
90  
testEmptyHost()91      public void testEmptyHost() throws Exception {
92          URI uri = new URI("http:///path");
93          assertEquals(null, uri.getHost());
94          assertEquals("/path", uri.getPath());
95      }
96  
testNoHost()97      public void testNoHost() throws Exception {
98          URI uri = new URI("http:/path");
99          assertEquals(null, uri.getHost());
100          assertEquals("/path", uri.getPath());
101      }
102  
testNoPath()103      public void testNoPath() throws Exception {
104          URI uri = new URI("http://host");
105          assertEquals("host", uri.getHost());
106          assertEquals("", uri.getPath());
107      }
108  
testEmptyHostAndNoPath()109      public void testEmptyHostAndNoPath() throws Exception {
110          try {
111              new URI("http://");
112              fail();
113          } catch (URISyntaxException expected) {
114          }
115      }
116  
117      // http://b/26632332
testSingleLetterHost()118      public void testSingleLetterHost() throws Exception {
119          URI uri = new URI("http://a");
120          assertEquals("a", uri.getHost());
121          assertEquals("", uri.getPath());
122      }
123  
testNoHostAndNoPath()124      public void testNoHostAndNoPath() throws Exception {
125          try {
126              new URI("http:");
127              fail();
128          } catch (URISyntaxException expected) {
129          }
130      }
131  
testAtSignInUserInfo()132      public void testAtSignInUserInfo() throws Exception {
133          URI uri = new URI("http://user@userhost.com:password@host");
134          assertEquals("user@userhost.com:password@host", uri.getAuthority());
135          assertEquals(null, uri.getUserInfo());
136          assertEquals(null, uri.getHost());
137      }
138  
testUserNoPassword()139      public void testUserNoPassword() throws Exception {
140          URI uri = new URI("http://user@host");
141          assertEquals("user@host", uri.getAuthority());
142          assertEquals("user", uri.getUserInfo());
143          assertEquals("host", uri.getHost());
144      }
145  
146      // http://b/26632332
testUserNoHost()147      public void testUserNoHost() throws Exception {
148          URI uri = new URI("http://user@");
149          assertEquals("user@", uri.getAuthority());
150          // from RI. this is curious
151          assertEquals(null, uri.getUserInfo());
152          assertEquals(null, uri.getHost());
153      }
154  
testUserNoPasswordExplicitPort()155      public void testUserNoPasswordExplicitPort() throws Exception {
156          URI uri = new URI("http://user@host:8080");
157          assertEquals("user@host:8080", uri.getAuthority());
158          assertEquals("user", uri.getUserInfo());
159          assertEquals("host", uri.getHost());
160          assertEquals(8080, uri.getPort());
161      }
162  
testUserPasswordHostPort()163      public void testUserPasswordHostPort() throws Exception {
164          URI uri = new URI("http://user:password@host:8080");
165          assertEquals("user:password@host:8080", uri.getAuthority());
166          assertEquals("user:password", uri.getUserInfo());
167          assertEquals("host", uri.getHost());
168          assertEquals(8080, uri.getPort());
169      }
170  
testUserPasswordEmptyHostPort()171      public void testUserPasswordEmptyHostPort() throws Exception {
172          URI uri = new URI("http://user:password@:8080");
173          assertEquals("user:password@:8080", uri.getAuthority());
174          // from RI. this is curious
175          assertEquals(null, uri.getUserInfo());
176          assertEquals(null, uri.getHost());
177          assertEquals(-1, uri.getPort());
178      }
179  
testUserPasswordEmptyHostEmptyPort()180      public void testUserPasswordEmptyHostEmptyPort() throws Exception {
181          URI uri = new URI("http://user:password@:");
182          assertEquals("user:password@:", uri.getAuthority());
183          // from RI. this is curious
184          assertEquals(null, uri.getUserInfo());
185          assertEquals(null, uri.getHost());
186          assertEquals(-1, uri.getPort());
187      }
188  
testPathOnly()189      public void testPathOnly() throws Exception {
190          URI uri = new URI("http://host/path");
191          assertEquals("host", uri.getHost());
192          assertEquals("/path", uri.getPath());
193      }
194  
testQueryOnly()195      public void testQueryOnly() throws Exception {
196          URI uri = new URI("http://host?query");
197          assertEquals("host", uri.getHost());
198          assertEquals("", uri.getPath());
199          assertEquals("query", uri.getQuery());
200      }
201  
testFragmentOnly()202      public void testFragmentOnly() throws Exception {
203          URI uri = new URI("http://host#fragment");
204          assertEquals("host", uri.getHost());
205          assertEquals("", uri.getPath());
206          assertEquals(null, uri.getQuery());
207          assertEquals("fragment", uri.getFragment());
208      }
209  
testAtSignInPath()210      public void testAtSignInPath() throws Exception {
211          URI uri = new URI("http://host/file@foo");
212          assertEquals("/file@foo", uri.getPath());
213          assertEquals(null, uri.getUserInfo());
214      }
215  
216  
testColonInPath()217      public void testColonInPath() throws Exception {
218          URI uri = new URI("http://host/file:colon");
219          assertEquals("/file:colon", uri.getPath());
220      }
221  
testSlashInQuery()222      public void testSlashInQuery() throws Exception {
223          URI uri = new URI("http://host/file?query/path");
224          assertEquals("/file", uri.getPath());
225          assertEquals("query/path", uri.getQuery());
226      }
227  
testQuestionMarkInQuery()228      public void testQuestionMarkInQuery() throws Exception {
229          URI uri = new URI("http://host/file?query?another");
230          assertEquals("/file", uri.getPath());
231          assertEquals("query?another", uri.getQuery());
232      }
233  
testAtSignInQuery()234      public void testAtSignInQuery() throws Exception {
235          URI uri = new URI("http://host/file?query@at");
236          assertEquals("/file", uri.getPath());
237          assertEquals("query@at", uri.getQuery());
238      }
239  
testColonInQuery()240      public void testColonInQuery() throws Exception {
241          URI uri = new URI("http://host/file?query:colon");
242          assertEquals("/file", uri.getPath());
243          assertEquals("query:colon", uri.getQuery());
244      }
245  
testQuestionMarkInFragment()246      public void testQuestionMarkInFragment() throws Exception {
247          URI uri = new URI("http://host/file#fragment?query");
248          assertEquals("/file", uri.getPath());
249          assertEquals(null, uri.getQuery());
250          assertEquals("fragment?query", uri.getFragment());
251      }
252  
testColonInFragment()253      public void testColonInFragment() throws Exception {
254          URI uri = new URI("http://host/file#fragment:80");
255          assertEquals("/file", uri.getPath());
256          assertEquals(-1, uri.getPort());
257          assertEquals("fragment:80", uri.getFragment());
258      }
259  
testSlashInFragment()260      public void testSlashInFragment() throws Exception {
261          URI uri = new URI("http://host/file#fragment/path");
262          assertEquals("/file", uri.getPath());
263          assertEquals("fragment/path", uri.getFragment());
264      }
265  
testHashInFragment()266      public void testHashInFragment() throws Exception {
267          try {
268              // This is not consistent with java.net.URL
269              new URI("http://host/file#fragment#another");
270              fail();
271          } catch (URISyntaxException expected) {
272          }
273      }
274  
testEmptyPort()275      public void testEmptyPort() throws Exception {
276          URI uri = new URI("http://host:/");
277          assertEquals(-1, uri.getPort());
278      }
279  
testNonNumericPort()280      public void testNonNumericPort() throws Exception {
281          URI uri = new URI("http://host:x/");
282          // From the RI. This is curious
283          assertEquals(null, uri.getHost());
284          assertEquals(-1, uri.getPort());
285      }
286  
testNegativePort()287      public void testNegativePort() throws Exception {
288          URI uri = new URI("http://host:-2/");
289          // From the RI. This is curious
290          assertEquals(null, uri.getHost());
291          assertEquals(-1, uri.getPort());
292      }
293  
testNegativePortEqualsPlaceholder()294      public void testNegativePortEqualsPlaceholder() throws Exception {
295          URI uri = new URI("http://host:-1/");
296          // From the RI. This is curious
297          assertEquals(null, uri.getHost());
298          assertEquals(-1, uri.getPort());
299      }
300  
testRelativePathOnQuery()301      public void testRelativePathOnQuery() throws Exception {
302          URI base = new URI("http://host/file?query/x");
303          URI uri = base.resolve("another");
304          assertEquals("http://host/another", uri.toString());
305          assertEquals("/another", uri.getPath());
306          assertEquals(null, uri.getQuery());
307          assertEquals(null, uri.getFragment());
308      }
309  
testRelativeFragmentOnQuery()310      public void testRelativeFragmentOnQuery() throws Exception {
311          URI base = new URI("http://host/file?query/x#fragment");
312          URI uri = base.resolve("#another");
313          assertEquals("http://host/file?query/x#another", uri.toString());
314          assertEquals("/file", uri.getPath());
315          assertEquals("query/x", uri.getQuery());
316          assertEquals("another", uri.getFragment());
317      }
318  
testPathContainsRelativeParts()319      public void testPathContainsRelativeParts() throws Exception {
320          URI uri = new URI("http://host/a/b/../c");
321  //        assertEquals("http://host/a/c", uri.toString()); // RI doesn't canonicalize
322      }
323  
testRelativePathAndFragment()324      public void testRelativePathAndFragment() throws Exception {
325          URI base = new URI("http://host/file");
326          assertEquals("http://host/another#fragment", base.resolve("another#fragment").toString());
327      }
328  
testRelativeParentDirectory()329      public void testRelativeParentDirectory() throws Exception {
330          URI base = new URI("http://host/a/b/c");
331          assertEquals("http://host/a/d", base.resolve("../d").toString());
332      }
333  
testRelativeChildDirectory()334      public void testRelativeChildDirectory() throws Exception {
335          URI base = new URI("http://host/a/b/c");
336          assertEquals("http://host/a/b/d/e", base.resolve("d/e").toString());
337      }
338  
testRelativeRootDirectory()339      public void testRelativeRootDirectory() throws Exception {
340          URI base = new URI("http://host/a/b/c");
341          assertEquals("http://host/d", base.resolve("/d").toString());
342      }
343  
testRelativeFullUrl()344      public void testRelativeFullUrl() throws Exception {
345          URI base = new URI("http://host/a/b/c");
346          assertEquals("http://host2/d/e", base.resolve("http://host2/d/e").toString());
347          assertEquals("https://host2/d/e", base.resolve("https://host2/d/e").toString());
348      }
349  
testRelativeDifferentScheme()350      public void testRelativeDifferentScheme() throws Exception {
351          URI base = new URI("http://host/a/b/c");
352          assertEquals("https://host2/d/e", base.resolve("https://host2/d/e").toString());
353      }
354  
testRelativeDifferentAuthority()355      public void testRelativeDifferentAuthority() throws Exception {
356          URI base = new URI("http://host/a/b/c");
357          assertEquals("http://another/d/e", base.resolve("//another/d/e").toString());
358      }
359  
testRelativeWithScheme()360      public void testRelativeWithScheme() throws Exception {
361          URI base = new URI("http://host/a/b/c");
362          try {
363              base.resolve("http:");
364              fail();
365          } catch (IllegalArgumentException expected) {
366          }
367          assertEquals("http:/", base.resolve("http:/").toString());
368      }
369  
testMalformedUrlsRefusedByFirefoxAndChrome()370      public void testMalformedUrlsRefusedByFirefoxAndChrome() throws Exception {
371          URI base = new URI("http://host/a/b/c");
372          try {
373              base.resolve("http://");
374              fail();
375          } catch (IllegalArgumentException expected) {
376          }
377          try {
378              base.resolve("//");
379              fail();
380          } catch (IllegalArgumentException expected) {
381          }
382          try {
383              base.resolve("https:");
384              fail();
385          } catch (IllegalArgumentException expected) {
386          }
387          assertEquals("https:/", base.resolve("https:/").toString());
388          try {
389              base.resolve("https://");
390              fail();
391          } catch (IllegalArgumentException expected) {
392          }
393      }
394  
testRfc1808NormalExamples()395      public void testRfc1808NormalExamples() throws Exception {
396          URI base = new URI("http://a/b/c/d;p?q");
397          assertEquals("https:h", base.resolve("https:h").toString());
398          assertEquals("http://a/b/c/g", base.resolve("g").toString());
399          assertEquals("http://a/b/c/g", base.resolve("./g").toString());
400          assertEquals("http://a/b/c/g/", base.resolve("g/").toString());
401          assertEquals("http://a/g", base.resolve("/g").toString());
402          assertEquals("http://g", base.resolve("//g").toString());
403          assertEquals("http://a/b/c/d;p?y", base.resolve("?y").toString()); // RI fails; loses file
404          assertEquals("http://a/b/c/g?y", base.resolve("g?y").toString());
405          assertEquals("http://a/b/c/d;p?q#s", base.resolve("#s").toString());
406          assertEquals("http://a/b/c/g#s", base.resolve("g#s").toString());
407          assertEquals("http://a/b/c/g?y#s", base.resolve("g?y#s").toString());
408          assertEquals("http://a/b/c/;x", base.resolve(";x").toString());
409          assertEquals("http://a/b/c/g;x", base.resolve("g;x").toString());
410          assertEquals("http://a/b/c/g;x?y#s", base.resolve("g;x?y#s").toString());
411          assertEquals("http://a/b/c/d;p?q", base.resolve("").toString()); // RI returns http://a/b/c/
412          assertEquals("http://a/b/c/", base.resolve(".").toString());
413          assertEquals("http://a/b/c/", base.resolve("./").toString());
414          assertEquals("http://a/b/", base.resolve("..").toString());
415          assertEquals("http://a/b/", base.resolve("../").toString());
416          assertEquals("http://a/b/g", base.resolve("../g").toString());
417          assertEquals("http://a/", base.resolve("../..").toString());
418          assertEquals("http://a/", base.resolve("../../").toString());
419          assertEquals("http://a/g", base.resolve("../../g").toString());
420      }
421  
testRfc1808AbnormalExampleTooManyDotDotSequences()422      public void testRfc1808AbnormalExampleTooManyDotDotSequences() throws Exception {
423          URI base = new URI("http://a/b/c/d;p?q");
424          assertEquals("http://a/g", base.resolve("../../../g").toString()); // RI doesn't normalize
425          assertEquals("http://a/g", base.resolve("../../../../g").toString()); // fails on RI
426      }
427  
testRfc1808AbnormalExampleRemoveDotSegments()428      public void testRfc1808AbnormalExampleRemoveDotSegments() throws Exception {
429          URI base = new URI("http://a/b/c/d;p?q");
430          assertEquals("http://a/g", base.resolve("/./g").toString()); // RI doesn't normalize
431          assertEquals("http://a/g", base.resolve("/../g").toString()); // fails on RI
432          assertEquals("http://a/b/c/g.", base.resolve("g.").toString());
433          assertEquals("http://a/b/c/.g", base.resolve(".g").toString());
434          assertEquals("http://a/b/c/g..", base.resolve("g..").toString());
435          assertEquals("http://a/b/c/..g", base.resolve("..g").toString());
436      }
437  
testRfc1808AbnormalExampleNonsensicalDots()438      public void testRfc1808AbnormalExampleNonsensicalDots() throws Exception {
439          URI base = new URI("http://a/b/c/d;p?q");
440          assertEquals("http://a/b/g", base.resolve("./../g").toString());
441          assertEquals("http://a/b/c/g/", base.resolve("./g/.").toString());
442          assertEquals("http://a/b/c/g/h", base.resolve("g/./h").toString());
443          assertEquals("http://a/b/c/h", base.resolve("g/../h").toString());
444          assertEquals("http://a/b/c/g;x=1/y", base.resolve("g;x=1/./y").toString());
445          assertEquals("http://a/b/c/y", base.resolve("g;x=1/../y").toString());
446      }
447  
testRfc1808AbnormalExampleRelativeScheme()448      public void testRfc1808AbnormalExampleRelativeScheme() throws Exception {
449          URI base = new URI("http://a/b/c/d;p?q");
450          URI uri = base.resolve("http:g");
451          assertEquals("http:g", uri.toString()); // this is an opaque URI
452          assertEquals(true, uri.isOpaque());
453          assertEquals(true, uri.isAbsolute());
454      }
455  
testRfc1808AbnormalExampleQueryOrFragmentDots()456      public void testRfc1808AbnormalExampleQueryOrFragmentDots() throws Exception {
457          URI base = new URI("http://a/b/c/d;p?q");
458          assertEquals("http://a/b/c/g?y/./x", base.resolve("g?y/./x").toString());
459          assertEquals("http://a/b/c/g?y/../x", base.resolve("g?y/../x").toString());
460          assertEquals("http://a/b/c/g#s/./x", base.resolve("g#s/./x").toString());
461          assertEquals("http://a/b/c/g#s/../x", base.resolve("g#s/../x").toString());
462      }
463  
testSquareBracketsInUserInfo()464      public void testSquareBracketsInUserInfo() throws Exception {
465          try {
466              new URI("http://user:[::1]@host");
467              fail();
468          } catch (URISyntaxException expected) {
469          }
470      }
471  
testFileUriExtraLeadingSlashes()472      public void testFileUriExtraLeadingSlashes() throws Exception {
473          URI uri = new URI("file:////foo");
474          assertEquals(null, uri.getAuthority());
475          assertEquals("//foo", uri.getPath());
476          assertEquals("file:////foo", uri.toString());
477      }
478  
testFileUrlWithAuthority()479      public void testFileUrlWithAuthority() throws Exception {
480          URI uri = new URI("file://x/foo");
481          assertEquals("x", uri.getAuthority());
482          assertEquals("/foo", uri.getPath());
483          assertEquals("file://x/foo", uri.toString());
484      }
485  
testEmptyAuthority()486      public void testEmptyAuthority() throws Exception {
487          URI uri = new URI("http:///foo");
488          assertEquals(null, uri.getAuthority());
489          assertEquals("/foo", uri.getPath());
490          assertEquals("http:///foo", uri.toString());
491      }
492  
testHttpUrlExtraLeadingSlashes()493      public void testHttpUrlExtraLeadingSlashes() throws Exception {
494          URI uri = new URI("http:////foo");
495          assertEquals(null, uri.getAuthority());
496          assertEquals("//foo", uri.getPath());
497          assertEquals("http:////foo", uri.toString());
498      }
499  
testFileUrlRelativePath()500      public void testFileUrlRelativePath() throws Exception {
501          URI base = new URI("file:/a/b/c");
502          assertEquals("file:/a/b/d", base.resolve("d").toString());
503      }
504  
testFileUrlDottedPath()505      public void testFileUrlDottedPath() throws Exception {
506          URI url = new URI("file:../a/b");
507          assertTrue(url.isOpaque());
508          assertNull(url.getPath());
509      }
510  
511      /**
512       * Regression test for http://b/issue?id=2604061
513       */
testParsingDotAsHostname()514      public void testParsingDotAsHostname() throws Exception {
515          assertEquals(null, new URI("http://./").getHost());
516      }
517  
testSquareBracketsWithIPv4()518      public void testSquareBracketsWithIPv4() throws Exception {
519          try {
520              new URI("http://[192.168.0.1]/");
521              fail();
522          } catch (URISyntaxException e) {
523          }
524      }
525  
testSquareBracketsWithHostname()526      public void testSquareBracketsWithHostname() throws Exception {
527          try {
528              new URI("http://[google.com]/");
529              fail();
530          } catch (URISyntaxException e) {
531          }
532      }
533  
testIPv6WithoutSquareBrackets()534      public void testIPv6WithoutSquareBrackets() throws Exception {
535          assertEquals(null, new URI("http://fe80::1234/").getHost());
536      }
537  
testEqualityWithNoPath()538      public void testEqualityWithNoPath() throws Exception {
539          assertFalse(new URI("http://android.com").equals(new URI("http://android.com/")));
540      }
541  
testRelativize()542      public void testRelativize() throws Exception {
543          URI a = new URI("http://host/a/b");
544          URI b = new URI("http://host/a/b/c");
545          assertEquals("b/c", a.relativize(b).toString()); // RI assumes a directory
546      }
547  
testParseServerAuthorityInvalidPortMinus()548      public void testParseServerAuthorityInvalidPortMinus() throws Exception {
549          URI uri = new URI("http://host:-2/");
550          assertEquals("host:-2", uri.getAuthority());
551          assertNull(uri.getHost());
552          assertEquals(-1, uri.getPort());
553          try {
554              uri.parseServerAuthority();
555              fail();
556          } catch (URISyntaxException expected) {
557          }
558      }
559  
testParseServerAuthorityInvalidPortPlus()560      public void testParseServerAuthorityInvalidPortPlus() throws Exception {
561          URI uri = new URI("http://host:+2/");
562          assertEquals("host:+2", uri.getAuthority());
563          assertNull(uri.getHost());
564          assertEquals(-1, uri.getPort());
565          try {
566              uri.parseServerAuthority();
567              fail();
568          } catch (URISyntaxException expected) {
569          }
570      }
571  
testParseServerAuthorityInvalidPortNonASCII()572      public void testParseServerAuthorityInvalidPortNonASCII() throws Exception {
573          URI uri = new URI("http://host:١٢٣/"); // 123 in arabic
574          assertEquals("host:١٢٣", uri.getAuthority());
575          assertNull(uri.getHost());
576          assertEquals(-1, uri.getPort());
577          try {
578              uri.parseServerAuthority();
579              fail();
580          } catch (URISyntaxException expected) {
581          }
582      }
583  
testParseServerAuthorityOmittedAuthority()584      public void testParseServerAuthorityOmittedAuthority() throws Exception {
585          URI uri = new URI("http:file");
586          uri.parseServerAuthority(); // does nothing!
587          assertNull(uri.getAuthority());
588          assertNull(uri.getHost());
589          assertEquals(-1, uri.getPort());
590      }
591  
testEncodingParts()592      public void testEncodingParts() throws Exception {
593          URI uri = new URI("http", "user:pa55w?rd", "host", 80, "/doc|search",
594                  "q=green robots", "over 6\"");
595          assertEquals("http", uri.getScheme());
596          assertEquals("user:pa55w?rd@host:80", uri.getAuthority());
597          assertEquals("user:pa55w%3Frd@host:80", uri.getRawAuthority());
598          assertEquals("user:pa55w?rd", uri.getUserInfo());
599          assertEquals("user:pa55w%3Frd", uri.getRawUserInfo());
600          assertEquals("/doc|search", uri.getPath());
601          assertEquals("/doc%7Csearch", uri.getRawPath());
602          assertEquals("q=green robots", uri.getQuery());
603          assertEquals("q=green%20robots", uri.getRawQuery());
604          assertEquals("over 6\"", uri.getFragment());
605          assertEquals("over%206%22", uri.getRawFragment());
606          assertEquals("//user:pa55w?rd@host:80/doc|search?q=green robots",
607                  uri.getSchemeSpecificPart());
608          assertEquals("//user:pa55w%3Frd@host:80/doc%7Csearch?q=green%20robots",
609                  uri.getRawSchemeSpecificPart());
610          assertEquals("http://user:pa55w%3Frd@host:80/doc%7Csearch?q=green%20robots#over%206%22",
611                  uri.toString());
612      }
613  
testSchemeCaseIsNotCanonicalized()614      public void testSchemeCaseIsNotCanonicalized() throws Exception {
615          URI uri = new URI("HTTP://host/path");
616          assertEquals("HTTP", uri.getScheme());
617      }
618  
testEmptyAuthorityWithPath()619      public void testEmptyAuthorityWithPath() throws Exception {
620          URI uri = new URI("http:///path");
621          assertEquals(null, uri.getAuthority());
622          assertEquals("/path", uri.getPath());
623      }
624  
testEmptyAuthorityWithQuery()625      public void testEmptyAuthorityWithQuery() throws Exception {
626          URI uri = new URI("http://?query");
627          assertEquals(null, uri.getAuthority());
628          assertEquals("", uri.getPath());
629          assertEquals("query", uri.getQuery());
630      }
631  
testEmptyAuthorityWithFragment()632      public void testEmptyAuthorityWithFragment() throws Exception {
633          URI uri = new URI("http://#fragment");
634          assertEquals(null, uri.getAuthority());
635          assertEquals("", uri.getPath());
636          assertEquals("fragment", uri.getFragment());
637      }
638  
testEncodingConstructorsRefuseRelativePath()639      public void testEncodingConstructorsRefuseRelativePath() throws Exception {
640          try {
641              new URI("http", "host", "relative", null);
642              fail();
643          } catch (URISyntaxException expected) {
644          }
645          try {
646              new URI("http", "host", "relative", null, null);
647              fail();
648          } catch (URISyntaxException expected) {
649          }
650          try {
651              new URI("http", null, "host", -1, "relative", null, null);
652              fail();
653          } catch (URISyntaxException expected) {
654          }
655      }
656  
testEncodingConstructorsAcceptEmptyPath()657      public void testEncodingConstructorsAcceptEmptyPath() throws Exception {
658          assertEquals("", new URI("http", "host", "", null).getPath());
659          assertEquals("", new URI("http", "host", "", null, null).getPath());
660          assertEquals("", new URI("http", null, "host", -1, "", null, null).getPath());
661      }
662  
testResolveRelativeAndAbsolute()663      public void testResolveRelativeAndAbsolute() throws Exception {
664          URI absolute = new URI("http://android.com/");
665          URI relative = new URI("robots.txt");
666          assertEquals(absolute, absolute.resolve(absolute));
667          assertEquals(new URI("http://android.com/robots.txt"), absolute.resolve(relative));
668          assertEquals(absolute, relative.resolve(absolute));
669          assertEquals(relative, relative.resolve(relative));
670      }
671  
testRelativizeRelativeAndAbsolute()672      public void testRelativizeRelativeAndAbsolute() throws Exception {
673          URI absolute = new URI("http://android.com/");
674          URI relative = new URI("robots.txt");
675          assertEquals(relative, absolute.relativize(new URI("http://android.com/robots.txt")));
676          assertEquals(new URI(""), absolute.relativize(absolute));
677          assertEquals(relative, absolute.relativize(relative));
678          assertEquals(absolute, relative.relativize(absolute));
679          assertEquals(new URI(""), relative.relativize(relative));
680      }
681  
testPartContainsSpace()682      public void testPartContainsSpace() throws Exception {
683          try {
684              new URI("ht tp://host/");
685              fail();
686          } catch (URISyntaxException expected) {
687          }
688          try {
689              new URI("http://user name@host/");
690              fail();
691          } catch (URISyntaxException expected) {
692          }
693          try {
694              new URI("http://ho st/");
695              fail();
696          } catch (URISyntaxException expected) {
697          }
698          try {
699              new URI("http://host:80 80/");
700              fail();
701          } catch (URISyntaxException expected) {
702          }
703          try {
704              new URI("http://host/fi le");
705              fail();
706          } catch (URISyntaxException expected) {
707          }
708          try {
709              new URI("http://host/file?que ry");
710              fail();
711          } catch (URISyntaxException expected) {
712          }
713          try {
714              new URI("http://host/file?query#re f");
715              fail();
716          } catch (URISyntaxException expected) {
717          }
718      }
719  
720      // http://code.google.com/p/android/issues/detail?id=37577
721      // http://b/18023709
722      // http://b/17579865
723      // http://b/18016625
testUnderscore()724      public void testUnderscore() throws Exception {
725          URI uri = new URI("http://a_b.c.d.net/");
726          assertEquals("a_b.c.d.net", uri.getAuthority());
727          // The RFC's don't permit underscores in hostnames, but URI has to because
728          // a certain large website doesn't seem to care about standards and specs.
729          assertEquals("a_b.c.d.net", uri.getHost());
730      }
731  
732      // RFC1034#section-3.5 doesn't permit empty labels in hostnames. This was accepted prior to N,
733      // but returns null in later releases.
734      // http://b/25991669
735      // http://b/29560247
testHostWithEmptyLabel()736      public void testHostWithEmptyLabel() throws Exception {
737          assertNull(new URI("http://.example.com/").getHost());
738          assertNull(new URI("http://example..com/").getHost());
739      }
740  
test_JDK7171415()741      public void test_JDK7171415() {
742          URI lower, mixed;
743          lower = URI.create("http://www.example.com/%2b");
744          mixed = URI.create("http://wWw.ExAmPlE.com/%2B");
745          assertTrue(lower.equals(mixed));
746          assertEquals(lower.hashCode(), mixed.hashCode());
747  
748          lower = URI.create("http://www.example.com/%2bbb");
749          mixed = URI.create("http://wWw.ExAmPlE.com/%2BbB");
750          assertFalse(lower.equals(mixed));
751          assertFalse(lower.hashCode() == mixed.hashCode());
752      }
753  
754      // check that "." and "_" characters are allowed in IPv6 scope_id.
test_JDK6933879()755      public void test_JDK6933879() {
756          final String HOST = "fe80::c00:16fe:cebe:3214%eth1.12_55";
757          URI uri;
758          try {
759              uri = new URI("http", null, HOST, 10, "/", null, null);
760          } catch (URISyntaxException ex) {
761              throw new AssertionError("Should not happen", ex);
762          }
763          assertEquals("[" + HOST + "]", uri.getHost());
764      }
765  
766      // Adding a new test? Consider adding an equivalent test to URLTest.java
767  }
768