OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library uriTest; | 5 library uriTest; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 testUri(String uri, bool isAbsolute) { | 10 testUri(String uri, bool isAbsolute) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 e = Uri.encodeQueryComponent(orig, encoding: ASCII); | 54 e = Uri.encodeQueryComponent(orig, encoding: ASCII); |
55 Expect.stringEquals(encodedAscii, e); | 55 Expect.stringEquals(encodedAscii, e); |
56 d = Uri.decodeQueryComponent(encodedAscii, encoding: ASCII); | 56 d = Uri.decodeQueryComponent(encodedAscii, encoding: ASCII); |
57 Expect.stringEquals(orig, d); | 57 Expect.stringEquals(orig, d); |
58 } else { | 58 } else { |
59 Expect.throws(() => Uri.encodeQueryComponent(orig, encoding: ASCII), | 59 Expect.throws(() => Uri.encodeQueryComponent(orig, encoding: ASCII), |
60 (e) => e is ArgumentError); | 60 (e) => e is ArgumentError); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 testUriPerRFCs(Uri base) { | 64 testUriPerRFCs() { |
| 65 final urisSample = "http://a/b/c/d;p?q"; |
| 66 Uri base = Uri.parse(urisSample); |
| 67 testResolve(expect, relative) { |
| 68 Expect.stringEquals(expect, base.resolve(relative).toString()); |
| 69 } |
| 70 |
65 // From RFC 3986. | 71 // From RFC 3986. |
66 Expect.stringEquals("g:h", base.resolve("g:h").toString()); | 72 testResolve("g:h", "g:h"); |
67 Expect.stringEquals("http://a/b/c/g", base.resolve("g").toString()); | 73 testResolve("http://a/b/c/g", "g"); |
68 Expect.stringEquals("http://a/b/c/g", base.resolve("./g").toString()); | 74 testResolve("http://a/b/c/g", "./g"); |
69 Expect.stringEquals("http://a/b/c/g/", base.resolve("g/").toString()); | 75 testResolve("http://a/b/c/g/", "g/"); |
70 Expect.stringEquals("http://a/g", base.resolve("/g").toString()); | 76 testResolve("http://a/g", "/g"); |
71 Expect.stringEquals("http://g", base.resolve("//g").toString()); | 77 testResolve("http://g", "//g"); |
72 Expect.stringEquals("http://a/b/c/d;p?y", base.resolve("?y").toString()); | 78 testResolve("http://a/b/c/d;p?y", "?y"); |
73 Expect.stringEquals("http://a/b/c/g?y", base.resolve("g?y").toString()); | 79 testResolve("http://a/b/c/g?y", "g?y"); |
74 Expect.stringEquals("http://a/b/c/d;p?q#s", base.resolve("#s").toString()); | 80 testResolve("http://a/b/c/d;p?q#s", "#s"); |
75 Expect.stringEquals("http://a/b/c/g#s", base.resolve("g#s").toString()); | 81 testResolve("http://a/b/c/g#s", "g#s"); |
76 Expect.stringEquals("http://a/b/c/g?y#s", base.resolve("g?y#s").toString()); | 82 testResolve("http://a/b/c/g?y#s", "g?y#s"); |
77 Expect.stringEquals("http://a/b/c/;x", base.resolve(";x").toString()); | 83 testResolve("http://a/b/c/;x", ";x"); |
78 Expect.stringEquals("http://a/b/c/g;x", base.resolve("g;x").toString()); | 84 testResolve("http://a/b/c/g;x", "g;x"); |
79 Expect.stringEquals("http://a/b/c/g;x?y#s", | 85 testResolve("http://a/b/c/g;x?y#s", "g;x?y#s"); |
80 base.resolve("g;x?y#s").toString()); | 86 testResolve("http://a/b/c/d;p?q", ""); |
81 Expect.stringEquals("http://a/b/c/d;p?q", base.resolve("").toString()); | 87 testResolve("http://a/b/c/", "."); |
82 Expect.stringEquals("http://a/b/c/", base.resolve(".").toString()); | 88 testResolve("http://a/b/c/", "./"); |
83 Expect.stringEquals("http://a/b/c/", base.resolve("./").toString()); | 89 testResolve("http://a/b/", ".."); |
84 Expect.stringEquals("http://a/b/", base.resolve("..").toString()); | 90 testResolve("http://a/b/", "../"); |
85 Expect.stringEquals("http://a/b/", base.resolve("../").toString()); | 91 testResolve("http://a/b/g", "../g"); |
86 Expect.stringEquals("http://a/b/g", base.resolve("../g").toString()); | 92 testResolve("http://a/", "../.."); |
87 Expect.stringEquals("http://a/", base.resolve("../..").toString()); | 93 testResolve("http://a/", "../../"); |
88 Expect.stringEquals("http://a/", base.resolve("../../").toString()); | 94 testResolve("http://a/g", "../../g"); |
89 Expect.stringEquals("http://a/g", base.resolve("../../g").toString()); | 95 testResolve("http://a/g", "../../../g"); |
90 Expect.stringEquals("http://a/g", base.resolve("../../../g").toString()); | 96 testResolve("http://a/g", "../../../../g"); |
91 Expect.stringEquals("http://a/g", base.resolve("../../../../g").toString()); | 97 testResolve("http://a/g", "/./g"); |
92 Expect.stringEquals("http://a/g", base.resolve("/./g").toString()); | 98 testResolve("http://a/g", "/../g"); |
93 Expect.stringEquals("http://a/g", base.resolve("/../g").toString()); | 99 testResolve("http://a/b/c/g.", "g."); |
94 Expect.stringEquals("http://a/b/c/g.", base.resolve("g.").toString()); | 100 testResolve("http://a/b/c/.g", ".g"); |
95 Expect.stringEquals("http://a/b/c/.g", base.resolve(".g").toString()); | 101 testResolve("http://a/b/c/g..", "g.."); |
96 Expect.stringEquals("http://a/b/c/g..", base.resolve("g..").toString()); | 102 testResolve("http://a/b/c/..g", "..g"); |
97 Expect.stringEquals("http://a/b/c/..g", base.resolve("..g").toString()); | 103 testResolve("http://a/b/g", "./../g"); |
98 Expect.stringEquals("http://a/b/g", base.resolve("./../g").toString()); | 104 testResolve("http://a/b/c/g/", "./g/."); |
99 Expect.stringEquals("http://a/b/c/g/", base.resolve("./g/.").toString()); | 105 testResolve("http://a/b/c/g/h", "g/./h"); |
100 Expect.stringEquals("http://a/b/c/g/h", base.resolve("g/./h").toString()); | 106 testResolve("http://a/b/c/h", "g/../h"); |
101 Expect.stringEquals("http://a/b/c/h", base.resolve("g/../h").toString()); | 107 testResolve("http://a/b/c/g;x=1/y", "g;x=1/./y"); |
102 Expect.stringEquals("http://a/b/c/g;x=1/y", | 108 testResolve("http://a/b/c/y", "g;x=1/../y"); |
103 base.resolve("g;x=1/./y").toString()); | 109 testResolve("http://a/b/c/g?y/./x", "g?y/./x"); |
104 Expect.stringEquals("http://a/b/c/y", base.resolve("g;x=1/../y").toString()); | 110 testResolve("http://a/b/c/g?y/../x", "g?y/../x"); |
105 Expect.stringEquals("http://a/b/c/g?y/./x", | 111 testResolve("http://a/b/c/g#s/./x", "g#s/./x"); |
106 base.resolve("g?y/./x").toString()); | 112 testResolve("http://a/b/c/g#s/../x", "g#s/../x"); |
107 Expect.stringEquals("http://a/b/c/g?y/../x", | 113 testResolve("http:g", "http:g"); |
108 base.resolve("g?y/../x").toString()); | |
109 Expect.stringEquals("http://a/b/c/g#s/./x", | |
110 base.resolve("g#s/./x").toString()); | |
111 Expect.stringEquals("http://a/b/c/g#s/../x", | |
112 base.resolve("g#s/../x").toString()); | |
113 Expect.stringEquals("http:g", base.resolve("http:g").toString()); | |
114 | 114 |
115 // Additional tests (not from RFC 3986). | 115 // Additional tests (not from RFC 3986). |
116 Expect.stringEquals("http://a/b/g;p/h;s", | 116 testResolve("http://a/b/g;p/h;s", "../g;p/h;s"); |
117 base.resolve("../g;p/h;s").toString()); | 117 |
| 118 // Test non-URI base (no scheme, no authority, relative path). |
| 119 base = Uri.parse("a/b/c?_#_"); |
| 120 testResolve("a/b/g?q#f", "g?q#f"); |
| 121 testResolve("../", "../../.."); |
| 122 testResolve("a/b/", "."); |
| 123 testResolve("c", "../../c"); |
| 124 |
| 125 base = Uri.parse("s:a/b"); |
| 126 testResolve("s:/c", "../c"); |
118 } | 127 } |
119 | 128 |
120 void testResolvePath(String expected, String path) { | 129 void testResolvePath(String expected, String path) { |
121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path); | 130 Expect.equals(expected, |
| 131 new Uri(path: '/').resolveUri(new Uri(path: path)).path); |
122 Expect.equals( | 132 Expect.equals( |
123 "http://localhost$expected", | 133 "http://localhost$expected", |
124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); | 134 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); |
125 } | 135 } |
126 | 136 |
127 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; | 137 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; |
128 const DIGIT = r"0123456789"; | 138 const DIGIT = r"0123456789"; |
129 const PERCENT_ENCODED = "%00%ff"; | 139 const PERCENT_ENCODED = "%00%ff"; |
130 const SUBDELIM = r"!$&'()*+,;="; | 140 const SUBDELIM = r"!$&'()*+,;="; |
131 | 141 |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 testResolvePath("/mid/6", "mid/content=5/../6"); | 474 testResolvePath("/mid/6", "mid/content=5/../6"); |
465 testResolvePath("/a/b/e", "a/b/c/d/../../e"); | 475 testResolvePath("/a/b/e", "a/b/c/d/../../e"); |
466 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); | 476 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); |
467 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); | 477 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); |
468 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); | 478 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); |
469 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); | 479 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); |
470 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); | 480 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); |
471 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); | 481 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); |
472 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/././."); | 482 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/././."); |
473 | 483 |
474 final urisSample = "http://a/b/c/d;p?q"; | 484 testUriPerRFCs(); |
475 Uri baseFromString = Uri.parse(urisSample); | |
476 testUriPerRFCs(baseFromString); | |
477 Uri base = Uri.parse(urisSample); | |
478 testUriPerRFCs(base); | |
479 | 485 |
480 Expect.stringEquals( | 486 Expect.stringEquals( |
481 "http://example.com", | 487 "http://example.com", |
482 Uri.parse("http://example.com/a/b/c").origin); | 488 Uri.parse("http://example.com/a/b/c").origin); |
483 Expect.stringEquals( | 489 Expect.stringEquals( |
484 "https://example.com", | 490 "https://example.com", |
485 Uri.parse("https://example.com/a/b/c").origin); | 491 Uri.parse("https://example.com/a/b/c").origin); |
486 Expect.stringEquals( | 492 Expect.stringEquals( |
487 "http://example.com:1234", | 493 "http://example.com:1234", |
488 Uri.parse("http://example.com:1234/a/b/c").origin); | 494 Uri.parse("http://example.com:1234/a/b/c").origin); |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 String dump(Uri uri) { | 608 String dump(Uri uri) { |
603 return "URI: $uri\n" | 609 return "URI: $uri\n" |
604 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" | 610 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" |
605 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" | 611 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" |
606 " Host: ${uri.host} #${uri.host.length}\n" | 612 " Host: ${uri.host} #${uri.host.length}\n" |
607 " Port: ${uri.port}\n" | 613 " Port: ${uri.port}\n" |
608 " Path: ${uri.path} #${uri.path.length}\n" | 614 " Path: ${uri.path} #${uri.path.length}\n" |
609 " Query: ${uri.query} #${uri.query.length}\n" | 615 " Query: ${uri.query} #${uri.query.length}\n" |
610 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; | 616 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; |
611 } | 617 } |
OLD | NEW |