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 uriText, bool isAbsolute) { | 10 testUri(String uriText, bool isAbsolute) { |
11 var uri = Uri.parse(uriText); | 11 var uri = Uri.parse(uriText); |
12 | 12 |
13 // Test that parsing a substring works the same as parsing the string. | 13 // Test that parsing a substring works the same as parsing the string. |
14 String wrapper = "://@[]:/%?#"; | 14 String wrapper = "://@[]:/%?#"; |
15 var embeddedUri = Uri.parse( | 15 var embeddedUri = Uri.parse( |
16 "$wrapper$uri$wrapper", wrapper.length, uriText.length + wrapper.length); | 16 "$wrapper$uri$wrapper", wrapper.length, uriText.length + wrapper.length); |
17 | 17 |
18 Expect.equals(uri, embeddedUri); | 18 Expect.equals(uri, embeddedUri); |
19 Expect.equals(isAbsolute, uri.isAbsolute); | 19 Expect.equals(isAbsolute, uri.isAbsolute); |
20 Expect.stringEquals(uriText, uri.toString()); | 20 Expect.stringEquals(uriText, uri.toString()); |
21 | 21 |
22 // Test equals and hashCode members. | 22 // Test equals and hashCode members. |
23 var uri2 = Uri.parse(uriText); | 23 var uri2 = Uri.parse(uriText); |
24 Expect.equals(uri, uri2); | 24 Expect.equals(uri, uri2); |
25 Expect.equals(uri.hashCode, uri2.hashCode); | 25 Expect.equals(uri.hashCode, uri2.hashCode); |
26 | 26 |
27 // Test that removeFragment doesn't change anything else. | 27 // Test that removeFragment doesn't change anything else. |
28 if (uri.hasFragment) { | 28 if (uri.hasFragment) { |
29 Expect.equals(Uri.parse(uriText.substring(0, uriText.indexOf('#'))), | 29 Expect.equals(Uri.parse(uriText.substring(0, uriText.indexOf('#'))), |
30 uri.removeFragment()); | 30 uri.removeFragment()); |
31 } else { | 31 } else { |
32 Expect.equals(uri, | 32 Expect.equals(uri, Uri.parse(uriText + "#fragment").removeFragment()); |
33 Uri.parse(uriText + "#fragment").removeFragment()); | |
34 } | 33 } |
35 | 34 |
36 Expect.isTrue(uri.isScheme(uri.scheme)); | 35 Expect.isTrue(uri.isScheme(uri.scheme)); |
37 Expect.isTrue(uri.isScheme(uri.scheme.toLowerCase())); | 36 Expect.isTrue(uri.isScheme(uri.scheme.toLowerCase())); |
38 Expect.isTrue(uri.isScheme(uri.scheme.toUpperCase())); | 37 Expect.isTrue(uri.isScheme(uri.scheme.toUpperCase())); |
39 if (uri.hasScheme) { | 38 if (uri.hasScheme) { |
40 // Capitalize | 39 // Capitalize |
41 Expect.isTrue(uri.isScheme( | 40 Expect.isTrue( |
42 uri.scheme[0].toUpperCase()+uri.scheme.substring(1))); | 41 uri.isScheme(uri.scheme[0].toUpperCase() + uri.scheme.substring(1))); |
43 Expect.isFalse(uri.isScheme( | 42 Expect |
44 uri.scheme.substring(0, uri.scheme.length - 1))); | 43 .isFalse(uri.isScheme(uri.scheme.substring(0, uri.scheme.length - 1))); |
45 Expect.isFalse(uri.isScheme(uri.scheme + ":")); | 44 Expect.isFalse(uri.isScheme(uri.scheme + ":")); |
46 Expect.isFalse(uri.isScheme(uri.scheme + "\x00")); | 45 Expect.isFalse(uri.isScheme(uri.scheme + "\x00")); |
47 } else { | 46 } else { |
48 Expect.isTrue(uri.isScheme(null)); | 47 Expect.isTrue(uri.isScheme(null)); |
49 Expect.isFalse(uri.isScheme(":")); | 48 Expect.isFalse(uri.isScheme(":")); |
50 } | 49 } |
51 } | 50 } |
52 | 51 |
53 testEncodeDecode(String orig, String encoded) { | 52 testEncodeDecode(String orig, String encoded) { |
54 var e = Uri.encodeFull(orig); | 53 var e = Uri.encodeFull(orig); |
55 Expect.stringEquals(encoded, e); | 54 Expect.stringEquals(encoded, e); |
56 var d = Uri.decodeFull(encoded); | 55 var d = Uri.decodeFull(encoded); |
57 Expect.stringEquals(orig, d); | 56 Expect.stringEquals(orig, d); |
58 } | 57 } |
59 | 58 |
60 testEncodeDecodeComponent(String orig, String encoded) { | 59 testEncodeDecodeComponent(String orig, String encoded) { |
61 var e = Uri.encodeComponent(orig); | 60 var e = Uri.encodeComponent(orig); |
62 Expect.stringEquals(encoded, e); | 61 Expect.stringEquals(encoded, e); |
63 var d = Uri.decodeComponent(encoded); | 62 var d = Uri.decodeComponent(encoded); |
64 Expect.stringEquals(orig, d); | 63 Expect.stringEquals(orig, d); |
65 } | 64 } |
66 | 65 |
67 testEncodeDecodeQueryComponent(String orig, | 66 testEncodeDecodeQueryComponent(String orig, String encodedUTF8, |
68 String encodedUTF8, | 67 String encodedLatin1, String encodedAscii) { |
69 String encodedLatin1, | |
70 String encodedAscii) { | |
71 var e, d; | 68 var e, d; |
72 e = Uri.encodeQueryComponent(orig); | 69 e = Uri.encodeQueryComponent(orig); |
73 Expect.stringEquals(encodedUTF8, e); | 70 Expect.stringEquals(encodedUTF8, e); |
74 d = Uri.decodeQueryComponent(encodedUTF8); | 71 d = Uri.decodeQueryComponent(encodedUTF8); |
75 Expect.stringEquals(orig, d); | 72 Expect.stringEquals(orig, d); |
76 | 73 |
77 e = Uri.encodeQueryComponent(orig, encoding: UTF8); | 74 e = Uri.encodeQueryComponent(orig, encoding: UTF8); |
78 Expect.stringEquals(encodedUTF8, e); | 75 Expect.stringEquals(encodedUTF8, e); |
79 d = Uri.decodeQueryComponent(encodedUTF8, encoding: UTF8); | 76 d = Uri.decodeQueryComponent(encodedUTF8, encoding: UTF8); |
80 Expect.stringEquals(orig, d); | 77 Expect.stringEquals(orig, d); |
81 | 78 |
82 e = Uri.encodeQueryComponent(orig, encoding: LATIN1); | 79 e = Uri.encodeQueryComponent(orig, encoding: LATIN1); |
83 Expect.stringEquals(encodedLatin1, e); | 80 Expect.stringEquals(encodedLatin1, e); |
84 d = Uri.decodeQueryComponent(encodedLatin1, encoding: LATIN1); | 81 d = Uri.decodeQueryComponent(encodedLatin1, encoding: LATIN1); |
85 Expect.stringEquals(orig, d); | 82 Expect.stringEquals(orig, d); |
86 | 83 |
87 if (encodedAscii != null) { | 84 if (encodedAscii != null) { |
88 e = Uri.encodeQueryComponent(orig, encoding: ASCII); | 85 e = Uri.encodeQueryComponent(orig, encoding: ASCII); |
89 Expect.stringEquals(encodedAscii, e); | 86 Expect.stringEquals(encodedAscii, e); |
90 d = Uri.decodeQueryComponent(encodedAscii, encoding: ASCII); | 87 d = Uri.decodeQueryComponent(encodedAscii, encoding: ASCII); |
91 Expect.stringEquals(orig, d); | 88 Expect.stringEquals(orig, d); |
92 } else { | 89 } else { |
93 Expect.throws(() => Uri.encodeQueryComponent(orig, encoding: ASCII), | 90 Expect.throws(() => Uri.encodeQueryComponent(orig, encoding: ASCII), |
94 (e) => e is ArgumentError); | 91 (e) => e is ArgumentError); |
95 } | 92 } |
96 } | 93 } |
97 | 94 |
98 testUriPerRFCs() { | 95 testUriPerRFCs() { |
99 // Convert a Uri to a guaranteed "non simple" URI with the same content. | 96 // Convert a Uri to a guaranteed "non simple" URI with the same content. |
100 toComplex(Uri uri) { | 97 toComplex(Uri uri) { |
101 Uri complex = new Uri( | 98 Uri complex = new Uri( |
102 scheme: uri.scheme, | 99 scheme: uri.scheme, |
103 userInfo: uri.hasAuthority ? uri.userInfo : null, | 100 userInfo: uri.hasAuthority ? uri.userInfo : null, |
104 host: uri.hasAuthority ? uri.host : null, | 101 host: uri.hasAuthority ? uri.host : null, |
(...skipping 13 matching lines...) Expand all Loading... |
118 setBase(String uri) { | 115 setBase(String uri) { |
119 base = Uri.parse(uri); | 116 base = Uri.parse(uri); |
120 complexBase = toComplex(base); | 117 complexBase = toComplex(base); |
121 } | 118 } |
122 | 119 |
123 testResolve(expect, relative) { | 120 testResolve(expect, relative) { |
124 String name = "$base << $relative"; | 121 String name = "$base << $relative"; |
125 Expect.stringEquals(expect, base.resolve(relative).toString(), name); | 122 Expect.stringEquals(expect, base.resolve(relative).toString(), name); |
126 | 123 |
127 Expect.stringEquals(expect, complexBase.resolve(relative).toString(), | 124 Expect.stringEquals(expect, complexBase.resolve(relative).toString(), |
128 name + " (complex base)"); | 125 name + " (complex base)"); |
129 } | 126 } |
130 | 127 |
131 // From RFC 3986. | 128 // From RFC 3986. |
132 final urisSample = "http://a/b/c/d;p?q"; | 129 final urisSample = "http://a/b/c/d;p?q"; |
133 setBase(urisSample); | 130 setBase(urisSample); |
134 | 131 |
135 testResolve("g:h", "g:h"); | 132 testResolve("g:h", "g:h"); |
136 testResolve("http://a/b/c/g", "g"); | 133 testResolve("http://a/b/c/g", "g"); |
137 testResolve("http://a/b/c/g", "./g"); | 134 testResolve("http://a/b/c/g", "./g"); |
138 testResolve("http://a/b/c/g/", "g/"); | 135 testResolve("http://a/b/c/g/", "g/"); |
139 testResolve("http://a/g", "/g"); | 136 testResolve("http://a/g", "/g"); |
140 testResolve("http://g", "//g"); | 137 testResolve("http://g", "//g"); |
141 testResolve("http://a/b/c/d;p?y", "?y"); | 138 testResolve("http://a/b/c/d;p?y", "?y"); |
142 testResolve("http://a/b/c/g?y", "g?y"); | 139 testResolve("http://a/b/c/g?y", "g?y"); |
143 testResolve("http://a/b/c/d;p?q#s", "#s"); | 140 testResolve("http://a/b/c/d;p?q#s", "#s"); |
144 testResolve("http://a/b/c/g#s", "g#s"); | 141 testResolve("http://a/b/c/g#s", "g#s"); |
145 testResolve("http://a/b/c/g?y#s", "g?y#s"); | 142 testResolve("http://a/b/c/g?y#s", "g?y#s"); |
146 testResolve("http://a/b/c/;x", ";x"); | 143 testResolve("http://a/b/c/;x", ";x"); |
147 testResolve("http://a/b/c/g;x", "g;x"); | 144 testResolve("http://a/b/c/g;x", "g;x"); |
148 testResolve("http://a/b/c/g;x?y#s", "g;x?y#s"); | 145 testResolve("http://a/b/c/g;x?y#s", "g;x?y#s"); |
149 testResolve("http://a/b/c/d;p?q", ""); | 146 testResolve("http://a/b/c/d;p?q", ""); |
150 testResolve("http://a/b/c/", "."); | 147 testResolve("http://a/b/c/", "."); |
151 testResolve("http://a/b/c/", "./"); | 148 testResolve("http://a/b/c/", "./"); |
152 testResolve("http://a/b/", ".."); | 149 testResolve("http://a/b/", ".."); |
153 testResolve("http://a/b/", "../"); | 150 testResolve("http://a/b/", "../"); |
154 testResolve("http://a/b/g", "../g"); | 151 testResolve("http://a/b/g", "../g"); |
155 testResolve("http://a/", "../.."); | 152 testResolve("http://a/", "../.."); |
156 testResolve("http://a/", "../../"); | 153 testResolve("http://a/", "../../"); |
157 testResolve("http://a/g", "../../g"); | 154 testResolve("http://a/g", "../../g"); |
158 testResolve("http://a/g", "../../../g"); | 155 testResolve("http://a/g", "../../../g"); |
159 testResolve("http://a/g", "../../../../g"); | 156 testResolve("http://a/g", "../../../../g"); |
160 testResolve("http://a/g", "/./g"); | 157 testResolve("http://a/g", "/./g"); |
161 testResolve("http://a/g", "/../g"); | 158 testResolve("http://a/g", "/../g"); |
162 testResolve("http://a/b/c/g.", "g."); | 159 testResolve("http://a/b/c/g.", "g."); |
163 testResolve("http://a/b/c/.g", ".g"); | 160 testResolve("http://a/b/c/.g", ".g"); |
164 testResolve("http://a/b/c/g..", "g.."); | 161 testResolve("http://a/b/c/g..", "g.."); |
165 testResolve("http://a/b/c/..g", "..g"); | 162 testResolve("http://a/b/c/..g", "..g"); |
166 testResolve("http://a/b/g", "./../g"); | 163 testResolve("http://a/b/g", "./../g"); |
167 testResolve("http://a/b/c/g/", "./g/."); | 164 testResolve("http://a/b/c/g/", "./g/."); |
168 testResolve("http://a/b/c/g/h", "g/./h"); | 165 testResolve("http://a/b/c/g/h", "g/./h"); |
169 testResolve("http://a/b/c/h", "g/../h"); | 166 testResolve("http://a/b/c/h", "g/../h"); |
170 testResolve("http://a/b/c/g;x=1/y", "g;x=1/./y"); | 167 testResolve("http://a/b/c/g;x=1/y", "g;x=1/./y"); |
171 testResolve("http://a/b/c/y", "g;x=1/../y"); | 168 testResolve("http://a/b/c/y", "g;x=1/../y"); |
172 testResolve("http://a/b/c/g?y/./x", "g?y/./x"); | 169 testResolve("http://a/b/c/g?y/./x", "g?y/./x"); |
173 testResolve("http://a/b/c/g?y/../x", "g?y/../x"); | 170 testResolve("http://a/b/c/g?y/../x", "g?y/../x"); |
174 testResolve("http://a/b/c/g#s/./x", "g#s/./x"); | 171 testResolve("http://a/b/c/g#s/./x", "g#s/./x"); |
175 testResolve("http://a/b/c/g#s/../x", "g#s/../x"); | 172 testResolve("http://a/b/c/g#s/../x", "g#s/../x"); |
176 testResolve("http:g", "http:g"); | 173 testResolve("http:g", "http:g"); |
177 | 174 |
178 // Additional tests (not from RFC 3986). | 175 // Additional tests (not from RFC 3986). |
179 testResolve("http://a/b/g;p/h;s", "../g;p/h;s"); | 176 testResolve("http://a/b/g;p/h;s", "../g;p/h;s"); |
180 | 177 |
181 setBase("s:a/b"); | 178 setBase("s:a/b"); |
182 testResolve("s:a/c", "c"); | 179 testResolve("s:a/c", "c"); |
183 testResolve("s:/c", "../c"); | 180 testResolve("s:/c", "../c"); |
184 | 181 |
185 setBase("S:a/b"); | 182 setBase("S:a/b"); |
186 testResolve("s:a/c", "c"); | 183 testResolve("s:a/c", "c"); |
187 testResolve("s:/c", "../c"); | 184 testResolve("s:/c", "../c"); |
188 | 185 |
189 setBase("s:foo"); | 186 setBase("s:foo"); |
(...skipping 16 matching lines...) Expand all Loading... |
206 setBase("S:/foo"); | 203 setBase("S:/foo"); |
207 testResolve("s:/bar", "bar"); | 204 testResolve("s:/bar", "bar"); |
208 testResolve("s:/bar", "../bar"); | 205 testResolve("s:/bar", "../bar"); |
209 | 206 |
210 // Test non-URI base (no scheme, no authority, relative path). | 207 // Test non-URI base (no scheme, no authority, relative path). |
211 setBase("a/b/c?_#_"); | 208 setBase("a/b/c?_#_"); |
212 testResolve("a/b/g?q#f", "g?q#f"); | 209 testResolve("a/b/g?q#f", "g?q#f"); |
213 testResolve("./", "../.."); | 210 testResolve("./", "../.."); |
214 testResolve("../", "../../.."); | 211 testResolve("../", "../../.."); |
215 testResolve("a/b/", "."); | 212 testResolve("a/b/", "."); |
216 testResolve("c", "../../c"); // Deliberate non-RFC behavior. | 213 testResolve("c", "../../c"); // Deliberate non-RFC behavior. |
217 setBase("../../a/b/c?_#_"); // Initial ".." in base url. | 214 setBase("../../a/b/c?_#_"); // Initial ".." in base url. |
218 testResolve("../../a/d", "../d"); | 215 testResolve("../../a/d", "../d"); |
219 testResolve("../../d", "../../d"); | 216 testResolve("../../d", "../../d"); |
220 testResolve("../../../d", "../../../d"); | 217 testResolve("../../../d", "../../../d"); |
221 setBase("../../a/b"); | 218 setBase("../../a/b"); |
222 testResolve("../../a/d", "d"); | 219 testResolve("../../a/d", "d"); |
223 testResolve("../../d", "../d"); | 220 testResolve("../../d", "../d"); |
224 testResolve("../../../d", "../../d"); | 221 testResolve("../../../d", "../../d"); |
225 setBase("../../a"); | 222 setBase("../../a"); |
226 testResolve("../../d", "d"); | 223 testResolve("../../d", "d"); |
227 testResolve("../../../d", "../d"); | 224 testResolve("../../../d", "../d"); |
228 testResolve("../../../../d", "../../d"); | 225 testResolve("../../../../d", "../../d"); |
229 | 226 |
230 // Absoluyte path, not scheme or authority. | 227 // Absoluyte path, not scheme or authority. |
231 setBase("/a"); | 228 setBase("/a"); |
232 testResolve("/b", "b"); | 229 testResolve("/b", "b"); |
233 testResolve("/b", "../b"); | 230 testResolve("/b", "../b"); |
234 testResolve("/b", "../../b"); | 231 testResolve("/b", "../../b"); |
235 setBase("/a/b"); | 232 setBase("/a/b"); |
236 testResolve("/a/c", "c"); | 233 testResolve("/a/c", "c"); |
237 testResolve("/c", "../c"); | 234 testResolve("/c", "../c"); |
238 testResolve("/c", "../../c"); | 235 testResolve("/c", "../../c"); |
239 | 236 |
240 setBase("s://h/p?q#f"); // A simple base. | 237 setBase("s://h/p?q#f"); // A simple base. |
241 // Simple references: | 238 // Simple references: |
242 testResolve("s2://h2/P?Q#F", "s2://h2/P?Q#F"); | 239 testResolve("s2://h2/P?Q#F", "s2://h2/P?Q#F"); |
243 testResolve("s://h2/P?Q#F", "//h2/P?Q#F"); | 240 testResolve("s://h2/P?Q#F", "//h2/P?Q#F"); |
244 testResolve("s://h/P?Q#F", "/P?Q#F"); | 241 testResolve("s://h/P?Q#F", "/P?Q#F"); |
245 testResolve("s://h/p?Q#F", "?Q#F"); | 242 testResolve("s://h/p?Q#F", "?Q#F"); |
246 testResolve("s://h/p?q#F", "#F"); | 243 testResolve("s://h/p?q#F", "#F"); |
247 testResolve("s://h/p?q", ""); | 244 testResolve("s://h/p?q", ""); |
248 // Non-simple references: | 245 // Non-simple references: |
249 testResolve("s2://I@h2/P?Q#F%20", "s2://I@h2/P?Q#F%20"); | 246 testResolve("s2://I@h2/P?Q#F%20", "s2://I@h2/P?Q#F%20"); |
250 testResolve("s://I@h2/P?Q#F%20", "//I@h2/P?Q#F%20"); | 247 testResolve("s://I@h2/P?Q#F%20", "//I@h2/P?Q#F%20"); |
251 testResolve("s://h2/P?Q#F%20", "//h2/P?Q#F%20"); | 248 testResolve("s://h2/P?Q#F%20", "//h2/P?Q#F%20"); |
252 testResolve("s://h/P?Q#F%20", "/P?Q#F%20"); | 249 testResolve("s://h/P?Q#F%20", "/P?Q#F%20"); |
253 testResolve("s://h/p?Q#F%20", "?Q#F%20"); | 250 testResolve("s://h/p?Q#F%20", "?Q#F%20"); |
254 testResolve("s://h/p?q#F%20", "#F%20"); | 251 testResolve("s://h/p?q#F%20", "#F%20"); |
255 | 252 |
256 setBase("s://h/p1/p2/p3"); // A simple base with a path. | 253 setBase("s://h/p1/p2/p3"); // A simple base with a path. |
257 testResolve("s://h/p1/p2/", "."); | 254 testResolve("s://h/p1/p2/", "."); |
258 testResolve("s://h/p1/p2/", "./"); | 255 testResolve("s://h/p1/p2/", "./"); |
259 testResolve("s://h/p1/", ".."); | 256 testResolve("s://h/p1/", ".."); |
260 testResolve("s://h/p1/", "../"); | 257 testResolve("s://h/p1/", "../"); |
261 testResolve("s://h/", "../.."); | 258 testResolve("s://h/", "../.."); |
262 testResolve("s://h/", "../../"); | 259 testResolve("s://h/", "../../"); |
263 testResolve("s://h/p1/%20", "../%20"); | 260 testResolve("s://h/p1/%20", "../%20"); |
264 testResolve("s://h/", "../../../.."); | 261 testResolve("s://h/", "../../../.."); |
265 testResolve("s://h/", "../../../../"); | 262 testResolve("s://h/", "../../../../"); |
266 | 263 |
267 setBase("s://h/p?q#f%20"); // A non-simpe base. | 264 setBase("s://h/p?q#f%20"); // A non-simpe base. |
268 // Simple references: | 265 // Simple references: |
269 testResolve("s2://h2/P?Q#F", "s2://h2/P?Q#F"); | 266 testResolve("s2://h2/P?Q#F", "s2://h2/P?Q#F"); |
270 testResolve("s://h2/P?Q#F", "//h2/P?Q#F"); | 267 testResolve("s://h2/P?Q#F", "//h2/P?Q#F"); |
271 testResolve("s://h/P?Q#F", "/P?Q#F"); | 268 testResolve("s://h/P?Q#F", "/P?Q#F"); |
272 testResolve("s://h/p?Q#F", "?Q#F"); | 269 testResolve("s://h/p?Q#F", "?Q#F"); |
273 testResolve("s://h/p?q#F", "#F"); | 270 testResolve("s://h/p?q#F", "#F"); |
274 testResolve("s://h/p?q", ""); | 271 testResolve("s://h/p?q", ""); |
275 // Non-simple references: | 272 // Non-simple references: |
276 testResolve("s2://I@h2/P?Q#F%20", "s2://I@h2/P?Q#F%20"); | 273 testResolve("s2://I@h2/P?Q#F%20", "s2://I@h2/P?Q#F%20"); |
277 testResolve("s://I@h2/P?Q#F%20", "//I@h2/P?Q#F%20"); | 274 testResolve("s://I@h2/P?Q#F%20", "//I@h2/P?Q#F%20"); |
278 testResolve("s://h2/P?Q#F%20", "//h2/P?Q#F%20"); | 275 testResolve("s://h2/P?Q#F%20", "//h2/P?Q#F%20"); |
279 testResolve("s://h/P?Q#F%20", "/P?Q#F%20"); | 276 testResolve("s://h/P?Q#F%20", "/P?Q#F%20"); |
280 testResolve("s://h/p?Q#F%20", "?Q#F%20"); | 277 testResolve("s://h/p?Q#F%20", "?Q#F%20"); |
281 testResolve("s://h/p?q#F%20", "#F%20"); | 278 testResolve("s://h/p?q#F%20", "#F%20"); |
282 | 279 |
283 setBase("S://h/p1/p2/p3"); // A non-simple base with a path. | 280 setBase("S://h/p1/p2/p3"); // A non-simple base with a path. |
284 testResolve("s://h/p1/p2/", "."); | 281 testResolve("s://h/p1/p2/", "."); |
285 testResolve("s://h/p1/p2/", "./"); | 282 testResolve("s://h/p1/p2/", "./"); |
286 testResolve("s://h/p1/", ".."); | 283 testResolve("s://h/p1/", ".."); |
287 testResolve("s://h/p1/", "../"); | 284 testResolve("s://h/p1/", "../"); |
288 testResolve("s://h/", "../.."); | 285 testResolve("s://h/", "../.."); |
289 testResolve("s://h/", "../../"); | 286 testResolve("s://h/", "../../"); |
290 testResolve("s://h/p1/%20", "../%20"); | 287 testResolve("s://h/p1/%20", "../%20"); |
291 testResolve("s://h/", "../../../.."); | 288 testResolve("s://h/", "../../../.."); |
292 testResolve("s://h/", "../../../../"); | 289 testResolve("s://h/", "../../../../"); |
293 | 290 |
294 setBase("../../../"); // A simple relative path. | 291 setBase("../../../"); // A simple relative path. |
295 testResolve("../../../a", "a"); | 292 testResolve("../../../a", "a"); |
296 testResolve("../../../../a", "../a"); | 293 testResolve("../../../../a", "../a"); |
297 testResolve("../../../a%20", "a%20"); | 294 testResolve("../../../a%20", "a%20"); |
298 testResolve("../../../../a%20", "../a%20"); | 295 testResolve("../../../../a%20", "../a%20"); |
299 | 296 |
300 // Tests covering the branches of the merge algorithm in RFC 3986 | 297 // Tests covering the branches of the merge algorithm in RFC 3986 |
301 // with both simple and complex base URIs. | 298 // with both simple and complex base URIs. |
302 for (var b in ["s://a/pa/pb?q#f", "s://a/pa/pb?q#f%20"]) { | 299 for (var b in ["s://a/pa/pb?q#f", "s://a/pa/pb?q#f%20"]) { |
303 setBase(b); | 300 setBase(b); |
304 | 301 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 setBase(noAuthEmptyPathBase); | 414 setBase(noAuthEmptyPathBase); |
418 testResolve("s:", ".."); | 415 testResolve("s:", ".."); |
419 testResolve("s:", "."); | 416 testResolve("s:", "."); |
420 setBase(noAuthRelSinglePathBase); | 417 setBase(noAuthRelSinglePathBase); |
421 testResolve("s:", ".."); | 418 testResolve("s:", ".."); |
422 testResolve("s:", "."); | 419 testResolve("s:", "."); |
423 } | 420 } |
424 } | 421 } |
425 | 422 |
426 void testResolvePath(String expected, String path) { | 423 void testResolvePath(String expected, String path) { |
427 Expect.equals(expected, | |
428 new Uri(path: '/').resolveUri(new Uri(path: path)).path); | |
429 Expect.equals( | 424 Expect.equals( |
430 "http://localhost$expected", | 425 expected, new Uri(path: '/').resolveUri(new Uri(path: path)).path); |
| 426 Expect.equals("http://localhost$expected", |
431 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); | 427 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); |
432 } | 428 } |
433 | 429 |
434 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; | 430 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; |
435 const DIGIT = r"0123456789"; | 431 const DIGIT = r"0123456789"; |
436 const PERCENT_ENCODED = "%00%ff"; | 432 const PERCENT_ENCODED = "%00%ff"; |
437 const SUBDELIM = r"!$&'()*+,;="; | 433 const SUBDELIM = r"!$&'()*+,;="; |
438 | 434 |
439 const SCHEMECHAR = "$ALPHA$DIGIT+-."; | 435 const SCHEMECHAR = "$ALPHA$DIGIT+-."; |
440 const UNRESERVED = "$ALPHA$DIGIT-._~"; | 436 const UNRESERVED = "$ALPHA$DIGIT-._~"; |
441 const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED"; | 437 const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED"; |
442 const USERINFOCHAR = "$REGNAMECHAR:"; | 438 const USERINFOCHAR = "$REGNAMECHAR:"; |
443 | 439 |
444 const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@"; | 440 const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@"; |
445 const PCHAR = "$PCHAR_NC:"; | 441 const PCHAR = "$PCHAR_NC:"; |
446 const QUERYCHAR = "$PCHAR/?"; | 442 const QUERYCHAR = "$PCHAR/?"; |
447 | 443 |
448 void testValidCharacters() { | 444 void testValidCharacters() { |
449 // test that all valid characters are accepted. | 445 // test that all valid characters are accepted. |
450 | 446 |
451 for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) { | 447 for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) { |
452 for (var userinfo in ["", "@", "$USERINFOCHAR$USERINFOCHAR@", | 448 for (var userinfo in [ |
453 "$USERINFOCHAR:$DIGIT@"]) { | 449 "", |
454 for (var host in ["", "$REGNAMECHAR$REGNAMECHAR", | 450 "@", |
455 "255.255.255.256", // valid reg-name. | 451 "$USERINFOCHAR$USERINFOCHAR@", |
456 "[ffff::ffff:ffff]", "[ffff::255.255.255.255]"]) { | 452 "$USERINFOCHAR:$DIGIT@" |
| 453 ]) { |
| 454 for (var host in [ |
| 455 "", "$REGNAMECHAR$REGNAMECHAR", |
| 456 "255.255.255.256", // valid reg-name. |
| 457 "[ffff::ffff:ffff]", "[ffff::255.255.255.255]" |
| 458 ]) { |
457 for (var port in ["", ":", ":$DIGIT$DIGIT"]) { | 459 for (var port in ["", ":", ":$DIGIT$DIGIT"]) { |
458 var auth = "$userinfo$host$port"; | 460 var auth = "$userinfo$host$port"; |
459 if (auth.isNotEmpty) auth = "//$auth"; | 461 if (auth.isNotEmpty) auth = "//$auth"; |
460 var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty. | 462 var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty. |
461 if (auth.isNotEmpty) { | 463 if (auth.isNotEmpty) { |
462 // Initial segment may be empty. | 464 // Initial segment may be empty. |
463 paths..add("//$PCHAR"); | 465 paths..add("//$PCHAR"); |
464 } else { | 466 } else { |
465 // Path may begin with non-slash. | 467 // Path may begin with non-slash. |
466 if (scheme.isEmpty) { | 468 if (scheme.isEmpty) { |
467 // Initial segment must not contain colon. | 469 // Initial segment must not contain colon. |
468 paths..add(PCHAR_NC) | 470 paths |
469 ..add("$PCHAR_NC/$PCHAR") | 471 ..add(PCHAR_NC) |
470 ..add("$PCHAR_NC/$PCHAR/"); | 472 ..add("$PCHAR_NC/$PCHAR") |
| 473 ..add("$PCHAR_NC/$PCHAR/"); |
471 } else { | 474 } else { |
472 paths..add(PCHAR) | 475 paths..add(PCHAR)..add("$PCHAR/$PCHAR")..add("$PCHAR/$PCHAR/"); |
473 ..add("$PCHAR/$PCHAR") | |
474 ..add("$PCHAR/$PCHAR/"); | |
475 } | 476 } |
476 } | 477 } |
477 for (var path in paths) { | 478 for (var path in paths) { |
478 for (var query in ["", "?", "?$QUERYCHAR"]) { | 479 for (var query in ["", "?", "?$QUERYCHAR"]) { |
479 for (var fragment in ["", "#", "#$QUERYCHAR"]) { | 480 for (var fragment in ["", "#", "#$QUERYCHAR"]) { |
480 var uri = "$scheme$auth$path$query$fragment"; | 481 var uri = "$scheme$auth$path$query$fragment"; |
481 // Should not throw. | 482 // Should not throw. |
482 var result = Uri.parse(uri); | 483 var result = Uri.parse(uri); |
483 } | 484 } |
484 } | 485 } |
485 } | 486 } |
486 } | 487 } |
487 } | 488 } |
488 } | 489 } |
489 } | 490 } |
490 } | 491 } |
491 | 492 |
492 void testInvalidUrls() { | 493 void testInvalidUrls() { |
493 void checkInvalid(uri) { | 494 void checkInvalid(uri) { |
494 try { | 495 try { |
495 var result = Uri.parse(uri); | 496 var result = Uri.parse(uri); |
496 Expect.fail("Invalid URI `$uri` parsed to $result\n" + dump(result)); | 497 Expect.fail("Invalid URI `$uri` parsed to $result\n" + dump(result)); |
497 } on FormatException { | 498 } on FormatException { |
498 // Success. | 499 // Success. |
499 } | 500 } |
500 } | 501 } |
| 502 |
501 checkInvalid("s%41://x.x/"); // No escapes in scheme, | 503 checkInvalid("s%41://x.x/"); // No escapes in scheme, |
502 // and no colon before slash in path. | 504 // and no colon before slash in path. |
503 checkInvalid("1a://x.x/"); // Scheme must start with letter, | 505 checkInvalid("1a://x.x/"); // Scheme must start with letter, |
504 // and no colon before slash in path. | 506 // and no colon before slash in path. |
505 checkInvalid(".a://x.x/"); // Scheme must start with letter, | 507 checkInvalid(".a://x.x/"); // Scheme must start with letter, |
506 // and no colon before slash in path. | 508 // and no colon before slash in path. |
507 checkInvalid("_:"); // Character not valid in scheme, | 509 checkInvalid("_:"); // Character not valid in scheme, |
508 // and no colon before slash in path. | 510 // and no colon before slash in path. |
509 checkInvalid(":"); // Scheme must start with letter, | 511 checkInvalid(":"); // Scheme must start with letter, |
510 // and no colon before slash in path. | 512 // and no colon before slash in path. |
511 | 513 |
512 void checkInvalidReplaced(uri, invalid, replacement) { | 514 void checkInvalidReplaced(uri, invalid, replacement) { |
513 var source = uri.replaceAll('{}', invalid); | 515 var source = uri.replaceAll('{}', invalid); |
514 var expected = uri.replaceAll('{}', replacement); | 516 var expected = uri.replaceAll('{}', replacement); |
515 var result = Uri.parse(source); | 517 var result = Uri.parse(source); |
516 Expect.equals(expected, "$result", "Source: $source\n${dump(result)}"); | 518 Expect.equals(expected, "$result", "Source: $source\n${dump(result)}"); |
517 } | 519 } |
518 | 520 |
519 // Regression test for http://dartbug.com/16081 | 521 // Regression test for http://dartbug.com/16081 |
520 checkInvalidReplaced("http://www.example.org/red%09ros{}#red)", | 522 checkInvalidReplaced( |
521 "\u00e9", "%C3%A9"); | 523 "http://www.example.org/red%09ros{}#red)", "\u00e9", "%C3%A9"); |
522 checkInvalidReplaced("http://r{}sum\{}.example.org", "\u00E9", "%C3%A9"); | 524 checkInvalidReplaced("http://r{}sum\{}.example.org", "\u00E9", "%C3%A9"); |
523 | 525 |
524 // Invalid characters. The characters must be rejected, even if normalizing | 526 // Invalid characters. The characters must be rejected, even if normalizing |
525 // the input would cause them to be valid (normalization happens after | 527 // the input would cause them to be valid (normalization happens after |
526 // validation). | 528 // validation). |
527 var invalidCharsAndReplacements = [ | 529 var invalidCharsAndReplacements = [ |
528 "\xe7", "%C3%A7", // Arbitrary non-ASCII letter | 530 "\xe7", "%C3%A7", // Arbitrary non-ASCII letter |
529 " ", "%20", // Space, not allowed anywhere. | 531 " ", "%20", // Space, not allowed anywhere. |
530 '"', "%22", // Quote, not allowed anywhere | 532 '"', "%22", // Quote, not allowed anywhere |
531 "<>", "%3C%3E", // Less/greater-than, not allowed anywhere. | 533 "<>", "%3C%3E", // Less/greater-than, not allowed anywhere. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 uri = Uri.parse("Z:"); | 596 uri = Uri.parse("Z:"); |
595 Expect.equals("z", uri.scheme); | 597 Expect.equals("z", uri.scheme); |
596 uri = Uri.parse("$SCHEMECHAR:"); | 598 uri = Uri.parse("$SCHEMECHAR:"); |
597 Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme); | 599 Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme); |
598 | 600 |
599 // Percent escape normalization. | 601 // Percent escape normalization. |
600 // Escapes of unreserved characters are converted to the character, | 602 // Escapes of unreserved characters are converted to the character, |
601 // subject to case normalization in reg-name. | 603 // subject to case normalization in reg-name. |
602 for (var i = 0; i < UNRESERVED.length; i++) { | 604 for (var i = 0; i < UNRESERVED.length; i++) { |
603 var char = UNRESERVED[i]; | 605 var char = UNRESERVED[i]; |
604 var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf. | 606 var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf. |
605 | 607 |
606 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" | 608 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" |
607 "?vV${escape}vV#wW${escape}wW"); | 609 "?vV${escape}vV#wW${escape}wW"); |
608 Expect.equals("xX${char}xX", uri.userInfo); | 610 Expect.equals("xX${char}xX", uri.userInfo); |
609 Expect.equals("yY${char}yY".toLowerCase(), uri.host); | 611 Expect.equals("yY${char}yY".toLowerCase(), uri.host); |
610 Expect.equals("/zZ${char}zZ", uri.path); | 612 Expect.equals("/zZ${char}zZ", uri.path); |
611 Expect.equals("vV${char}vV", uri.query); | 613 Expect.equals("vV${char}vV", uri.query); |
612 Expect.equals("wW${char}wW", uri.fragment); | 614 Expect.equals("wW${char}wW", uri.fragment); |
613 | 615 |
614 uri = Uri.parse("s://yY${escape}yY/zZ${escape}zZ" | 616 uri = Uri.parse("s://yY${escape}yY/zZ${escape}zZ" |
615 "?vV${escape}vV#wW${escape}wW"); | 617 "?vV${escape}vV#wW${escape}wW"); |
616 Expect.equals("yY${char}yY".toLowerCase(), uri.host); | 618 Expect.equals("yY${char}yY".toLowerCase(), uri.host); |
617 Expect.equals("/zZ${char}zZ", uri.path); | 619 Expect.equals("/zZ${char}zZ", uri.path); |
618 Expect.equals("vV${char}vV", uri.query); | 620 Expect.equals("vV${char}vV", uri.query); |
619 Expect.equals("wW${char}wW", uri.fragment); | 621 Expect.equals("wW${char}wW", uri.fragment); |
620 } | 622 } |
621 | 623 |
622 // Escapes of reserved characters are kept, but upper-cased. | 624 // Escapes of reserved characters are kept, but upper-cased. |
623 for (var escape in ["%00", "%1f", "%7F", "%fF"]) { | 625 for (var escape in ["%00", "%1f", "%7F", "%fF"]) { |
624 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" | 626 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" |
625 "?vV${escape}vV#wW${escape}wW"); | 627 "?vV${escape}vV#wW${escape}wW"); |
626 var normalizedEscape = escape.toUpperCase(); | 628 var normalizedEscape = escape.toUpperCase(); |
627 Expect.equals("xX${normalizedEscape}xX", uri.userInfo); | 629 Expect.equals("xX${normalizedEscape}xX", uri.userInfo); |
628 Expect.equals("yy${normalizedEscape}yy", uri.host); | 630 Expect.equals("yy${normalizedEscape}yy", uri.host); |
629 Expect.equals("/zZ${normalizedEscape}zZ", uri.path); | 631 Expect.equals("/zZ${normalizedEscape}zZ", uri.path); |
630 Expect.equals("vV${normalizedEscape}vV", uri.query); | 632 Expect.equals("vV${normalizedEscape}vV", uri.query); |
631 Expect.equals("wW${normalizedEscape}wW", uri.fragment); | 633 Expect.equals("wW${normalizedEscape}wW", uri.fragment); |
632 } | 634 } |
633 | 635 |
634 // Some host normalization edge cases. | 636 // Some host normalization edge cases. |
635 uri = Uri.parse("x://x%61X%41x%41X%61x/"); | 637 uri = Uri.parse("x://x%61X%41x%41X%61x/"); |
(...skipping 27 matching lines...) Expand all Loading... |
663 | 665 |
664 // File scheme noralizes to always showing authority, even if empty. | 666 // File scheme noralizes to always showing authority, even if empty. |
665 uri = new Uri(scheme: "file", path: "/y"); | 667 uri = new Uri(scheme: "file", path: "/y"); |
666 Expect.equals("file:///y", uri.toString()); | 668 Expect.equals("file:///y", uri.toString()); |
667 uri = new Uri(scheme: "file", path: "y"); | 669 uri = new Uri(scheme: "file", path: "y"); |
668 Expect.equals("file:///y", uri.toString()); | 670 Expect.equals("file:///y", uri.toString()); |
669 | 671 |
670 // Empty host/query/fragment ensures the delimiter is there. | 672 // Empty host/query/fragment ensures the delimiter is there. |
671 // Different from not being there. | 673 // Different from not being there. |
672 Expect.equals("scheme:/", Uri.parse("scheme:/").toString()); | 674 Expect.equals("scheme:/", Uri.parse("scheme:/").toString()); |
673 Expect.equals("scheme:/", | 675 Expect.equals("scheme:/", new Uri(scheme: "scheme", path: "/").toString()); |
674 new Uri(scheme: "scheme", path: "/").toString()); | |
675 | 676 |
676 Expect.equals("scheme:///?#", Uri.parse("scheme:///?#").toString()); | 677 Expect.equals("scheme:///?#", Uri.parse("scheme:///?#").toString()); |
677 Expect.equals("scheme:///#", | 678 Expect.equals( |
678 new Uri(scheme: "scheme", host: "", path: "/", | 679 "scheme:///#", |
679 query: "", fragment: "").toString()); | 680 new Uri(scheme: "scheme", host: "", path: "/", query: "", fragment: "") |
| 681 .toString()); |
680 } | 682 } |
681 | 683 |
682 void testReplace() { | 684 void testReplace() { |
683 var uris = [ | 685 var uris = [ |
684 Uri.parse(""), | 686 Uri.parse(""), |
685 Uri.parse("a://@:/?#"), | 687 Uri.parse("a://@:/?#"), |
686 Uri.parse("a://:/?#"), // Parsed as simple URI. | 688 Uri.parse("a://:/?#"), // Parsed as simple URI. |
687 Uri.parse("a://b@c:4/e/f?g#h"), | 689 Uri.parse("a://b@c:4/e/f?g#h"), |
688 Uri.parse("a://c:4/e/f?g#h"), // Parsed as simple URI. | 690 Uri.parse("a://c:4/e/f?g#h"), // Parsed as simple URI. |
689 Uri.parse("$SCHEMECHAR://$REGNAMECHAR:$DIGIT/$PCHAR/$PCHAR" | 691 Uri.parse("$SCHEMECHAR://$REGNAMECHAR:$DIGIT/$PCHAR/$PCHAR" |
690 "?$QUERYCHAR#$QUERYCHAR"), // Parsed as simple URI. | 692 "?$QUERYCHAR#$QUERYCHAR"), // Parsed as simple URI. |
691 Uri.parse("$SCHEMECHAR://$USERINFOCHAR@$REGNAMECHAR:$DIGIT/$PCHAR/$PCHAR" | 693 Uri.parse("$SCHEMECHAR://$USERINFOCHAR@$REGNAMECHAR:$DIGIT/$PCHAR/$PCHAR" |
692 "?$QUERYCHAR#$QUERYCHAR"), | 694 "?$QUERYCHAR#$QUERYCHAR"), |
693 ]; | 695 ]; |
694 for (var uri1 in uris) { | 696 for (var uri1 in uris) { |
695 for (var uri2 in uris) { | 697 for (var uri2 in uris) { |
696 if (identical(uri1, uri2)) continue; | 698 if (identical(uri1, uri2)) continue; |
697 var scheme = uri1.scheme; | 699 var scheme = uri1.scheme; |
698 var userInfo = uri1.hasAuthority ? uri1.userInfo : ""; | 700 var userInfo = uri1.hasAuthority ? uri1.userInfo : ""; |
699 var host = uri1.hasAuthority ? uri1.host : null; | 701 var host = uri1.hasAuthority ? uri1.host : null; |
700 var port = uri1.hasAuthority ? uri1.port : 0; | 702 var port = uri1.hasAuthority ? uri1.port : 0; |
701 var path = uri1.path; | 703 var path = uri1.path; |
702 var query = uri1.hasQuery ? uri1.query : null; | 704 var query = uri1.hasQuery ? uri1.query : null; |
703 var fragment = uri1.hasFragment ? uri1.fragment : null; | 705 var fragment = uri1.hasFragment ? uri1.fragment : null; |
704 | 706 |
705 var tmp1 = uri1; | 707 var tmp1 = uri1; |
706 | 708 |
707 void test() { | 709 void test() { |
708 var tmp2 = new Uri(scheme: scheme, userInfo: userInfo, host: host, | 710 var tmp2 = new Uri( |
709 port: port, path: path, | 711 scheme: scheme, |
710 query: query == "" ? null : query, | 712 userInfo: userInfo, |
711 queryParameters: query == "" ? {} : null, | 713 host: host, |
712 fragment: fragment); | 714 port: port, |
| 715 path: path, |
| 716 query: query == "" ? null : query, |
| 717 queryParameters: query == "" ? {} : null, |
| 718 fragment: fragment); |
713 Expect.equals(tmp1, tmp2); | 719 Expect.equals(tmp1, tmp2); |
714 } | 720 } |
715 | 721 |
716 test(); | 722 test(); |
717 | 723 |
718 scheme = uri2.scheme; | 724 scheme = uri2.scheme; |
719 tmp1 = tmp1.replace(scheme: scheme); | 725 tmp1 = tmp1.replace(scheme: scheme); |
720 test(); | 726 test(); |
721 | 727 |
722 if (uri2.hasAuthority) { | 728 if (uri2.hasAuthority) { |
(...skipping 21 matching lines...) Expand all Loading... |
744 } | 750 } |
745 } | 751 } |
746 } | 752 } |
747 | 753 |
748 // Regression test, http://dartbug.com/20814 | 754 // Regression test, http://dartbug.com/20814 |
749 var uri = Uri.parse("/no-authorty/"); | 755 var uri = Uri.parse("/no-authorty/"); |
750 uri = uri.replace(fragment: "fragment"); | 756 uri = uri.replace(fragment: "fragment"); |
751 Expect.isFalse(uri.hasAuthority); | 757 Expect.isFalse(uri.hasAuthority); |
752 | 758 |
753 uri = new Uri(scheme: "foo", path: "bar"); | 759 uri = new Uri(scheme: "foo", path: "bar"); |
754 uri = uri.replace( | 760 uri = uri.replace(queryParameters: { |
755 queryParameters: {"x": ["42", "37"], "y": ["43", "38"]}); | 761 "x": ["42", "37"], |
| 762 "y": ["43", "38"] |
| 763 }); |
756 var params = uri.queryParametersAll; | 764 var params = uri.queryParametersAll; |
757 Expect.equals(2, params.length); | 765 Expect.equals(2, params.length); |
758 Expect.listEquals(["42", "37"], params["x"]); | 766 Expect.listEquals(["42", "37"], params["x"]); |
759 Expect.listEquals(["43", "38"], params["y"]); | 767 Expect.listEquals(["43", "38"], params["y"]); |
760 | 768 |
761 // Test replacing with empty strings. | 769 // Test replacing with empty strings. |
762 uri = Uri.parse("s://a:1/b/c?d#e"); | 770 uri = Uri.parse("s://a:1/b/c?d#e"); |
763 Expect.equals("s://a:1/b/c?d#", uri.replace(fragment: "").toString()); | 771 Expect.equals("s://a:1/b/c?d#", uri.replace(fragment: "").toString()); |
764 Expect.equals("s://a:1/b/c?#e", uri.replace(query: "").toString()); | 772 Expect.equals("s://a:1/b/c?#e", uri.replace(query: "").toString()); |
765 Expect.equals("s://a:1?d#e", uri.replace(path: "").toString()); | 773 Expect.equals("s://a:1?d#e", uri.replace(path: "").toString()); |
(...skipping 22 matching lines...) Expand all Loading... |
788 Expect.equals("file%3A///wat", "$uri"); | 796 Expect.equals("file%3A///wat", "$uri"); |
789 Expect.isFalse(uri.hasAuthority); | 797 Expect.isFalse(uri.hasAuthority); |
790 Expect.isFalse(uri.hasScheme); | 798 Expect.isFalse(uri.hasScheme); |
791 } | 799 } |
792 | 800 |
793 main() { | 801 main() { |
794 testUri("http:", true); | 802 testUri("http:", true); |
795 testUri("file:///", true); | 803 testUri("file:///", true); |
796 testUri("file", false); | 804 testUri("file", false); |
797 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); | 805 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); |
798 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment", | 806 testUri( |
799 false); | 807 "http://user@example.com:8080/fisk?query=89&hest=silas#fragment", false); |
800 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", | 808 Expect.stringEquals( |
801 new Uri( | 809 "http://user@example.com/a/b/c?query#fragment", |
802 scheme: "http", | 810 new Uri( |
803 userInfo: "user", | 811 scheme: "http", |
804 host: "example.com", | 812 userInfo: "user", |
805 port: 80, | 813 host: "example.com", |
806 path: "/a/b/c", | 814 port: 80, |
807 query: "query", | 815 path: "/a/b/c", |
808 fragment: "fragment").toString()); | 816 query: "query", |
809 Expect.stringEquals("/a/b/c/", | 817 fragment: "fragment") |
810 new Uri( | 818 .toString()); |
811 scheme: null, | 819 Expect.stringEquals( |
812 userInfo: null, | 820 "/a/b/c/", |
813 host: null, | 821 new Uri( |
814 port: 0, | 822 scheme: null, |
815 path: "/a/b/c/", | 823 userInfo: null, |
816 query: null, | 824 host: null, |
817 fragment: null).toString()); | 825 port: 0, |
| 826 path: "/a/b/c/", |
| 827 query: null, |
| 828 fragment: null) |
| 829 .toString()); |
818 Expect.stringEquals("file:///", Uri.parse("file:").toString()); | 830 Expect.stringEquals("file:///", Uri.parse("file:").toString()); |
819 Expect.stringEquals("file:///", Uri.parse("file:/").toString()); | 831 Expect.stringEquals("file:///", Uri.parse("file:/").toString()); |
820 Expect.stringEquals("file:///", Uri.parse("file:").toString()); | 832 Expect.stringEquals("file:///", Uri.parse("file:").toString()); |
821 Expect.stringEquals("file:///foo", Uri.parse("file:foo").toString()); | 833 Expect.stringEquals("file:///foo", Uri.parse("file:foo").toString()); |
822 Expect.stringEquals("file:///foo", Uri.parse("file:/foo").toString()); | 834 Expect.stringEquals("file:///foo", Uri.parse("file:/foo").toString()); |
823 Expect.stringEquals("file://foo/", Uri.parse("file://foo").toString()); | 835 Expect.stringEquals("file://foo/", Uri.parse("file://foo").toString()); |
824 | 836 |
825 testResolvePath("/a/g", "/a/b/c/./../../g"); | 837 testResolvePath("/a/g", "/a/b/c/./../../g"); |
826 testResolvePath("/a/g", "/a/b/c/./../../g"); | 838 testResolvePath("/a/g", "/a/b/c/./../../g"); |
827 testResolvePath("/mid/6", "mid/content=5/../6"); | 839 testResolvePath("/mid/6", "mid/content=5/../6"); |
828 testResolvePath("/a/b/e", "a/b/c/d/../../e"); | 840 testResolvePath("/a/b/e", "a/b/c/d/../../e"); |
829 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); | 841 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); |
830 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); | 842 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); |
831 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); | 843 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); |
832 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); | 844 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); |
833 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); | 845 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); |
834 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); | 846 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); |
835 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/././."); | 847 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/././."); |
836 | 848 |
837 testUriPerRFCs(); | 849 testUriPerRFCs(); |
838 | 850 |
839 Expect.stringEquals( | 851 Expect.stringEquals( |
840 "http://example.com", | 852 "http://example.com", Uri.parse("http://example.com/a/b/c").origin); |
841 Uri.parse("http://example.com/a/b/c").origin); | |
842 Expect.stringEquals( | 853 Expect.stringEquals( |
843 "https://example.com", | 854 "https://example.com", Uri.parse("https://example.com/a/b/c").origin); |
844 Uri.parse("https://example.com/a/b/c").origin); | 855 Expect.stringEquals("http://example.com:1234", |
845 Expect.stringEquals( | |
846 "http://example.com:1234", | |
847 Uri.parse("http://example.com:1234/a/b/c").origin); | 856 Uri.parse("http://example.com:1234/a/b/c").origin); |
848 Expect.stringEquals( | 857 Expect.stringEquals("https://example.com:1234", |
849 "https://example.com:1234", | |
850 Uri.parse("https://example.com:1234/a/b/c").origin); | 858 Uri.parse("https://example.com:1234/a/b/c").origin); |
851 Expect.throws( | 859 Expect.throws(() => Uri.parse("http:").origin, (e) { |
852 () => Uri.parse("http:").origin, | 860 return e is StateError; |
853 (e) { return e is StateError; }, | 861 }, "origin for URI with empty host should fail"); |
854 "origin for URI with empty host should fail"); | |
855 Expect.throws( | 862 Expect.throws( |
856 () => new Uri( | 863 () => new Uri( |
857 scheme: "http", | 864 scheme: "http", |
858 userInfo: null, | 865 userInfo: null, |
859 host: "", | 866 host: "", |
860 port: 80, | 867 port: 80, |
861 path: "/a/b/c", | 868 path: "/a/b/c", |
862 query: "query", | 869 query: "query", |
863 fragment: "fragment").origin, | 870 fragment: "fragment") |
864 (e) { return e is StateError; }, | 871 .origin, (e) { |
865 "origin for URI with empty host should fail"); | 872 return e is StateError; |
| 873 }, "origin for URI with empty host should fail"); |
866 Expect.throws( | 874 Expect.throws( |
867 () => new Uri( | 875 () => new Uri( |
868 scheme: null, | 876 scheme: null, |
869 userInfo: null, | 877 userInfo: null, |
870 host: "", | 878 host: "", |
871 port: 80, | 879 port: 80, |
872 path: "/a/b/c", | 880 path: "/a/b/c", |
873 query: "query", | 881 query: "query", |
874 fragment: "fragment").origin, | 882 fragment: "fragment") |
875 (e) { return e is StateError; }, | 883 .origin, (e) { |
876 "origin for URI with empty scheme should fail"); | 884 return e is StateError; |
| 885 }, "origin for URI with empty scheme should fail"); |
877 Expect.throws( | 886 Expect.throws( |
878 () => new Uri( | 887 () => new Uri( |
879 scheme: "http", | 888 scheme: "http", |
880 userInfo: null, | 889 userInfo: null, |
881 host: null, | 890 host: null, |
882 port: 80, | 891 port: 80, |
883 path: "/a/b/c", | 892 path: "/a/b/c", |
884 query: "query", | 893 query: "query", |
885 fragment: "fragment").origin, | 894 fragment: "fragment") |
886 (e) { return e is StateError; }, | 895 .origin, (e) { |
887 "origin for URI with empty host should fail"); | 896 return e is StateError; |
888 Expect.throws( | 897 }, "origin for URI with empty host should fail"); |
889 () => Uri.parse("http://:80").origin, | 898 Expect.throws(() => Uri.parse("http://:80").origin, (e) { |
890 (e) { return e is StateError; }, | 899 return e is StateError; |
891 "origin for URI with empty host should fail"); | 900 }, "origin for URI with empty host should fail"); |
892 Expect.throws( | 901 Expect.throws(() => Uri.parse("file://localhost/test.txt").origin, (e) { |
893 () => Uri.parse("file://localhost/test.txt").origin, | 902 return e is StateError; |
894 (e) { return e is StateError; }, | 903 }, "origin for non-http/https uri should fail"); |
895 "origin for non-http/https uri should fail"); | |
896 | 904 |
897 // URI encode tests | 905 // URI encode tests |
898 // Create a string with code point 0x10000 encoded as a surrogate pair. | 906 // Create a string with code point 0x10000 encoded as a surrogate pair. |
899 var s = UTF8.decode([0xf0, 0x90, 0x80, 0x80]); | 907 var s = UTF8.decode([0xf0, 0x90, 0x80, 0x80]); |
900 | 908 |
901 Expect.stringEquals("\u{10000}", s); | 909 Expect.stringEquals("\u{10000}", s); |
902 | 910 |
903 testEncodeDecode("A + B", "A%20+%20B"); | 911 testEncodeDecode("A + B", "A%20+%20B"); |
904 testEncodeDecode("\uFFFE", "%EF%BF%BE"); | 912 testEncodeDecode("\uFFFE", "%EF%BF%BE"); |
905 testEncodeDecode("\uFFFF", "%EF%BF%BF"); | 913 testEncodeDecode("\uFFFF", "%EF%BF%BF"); |
906 testEncodeDecode("\uFFFE", "%EF%BF%BE"); | 914 testEncodeDecode("\uFFFE", "%EF%BF%BE"); |
907 testEncodeDecode("\uFFFF", "%EF%BF%BF"); | 915 testEncodeDecode("\uFFFF", "%EF%BF%BF"); |
908 testEncodeDecode("\x7f", "%7F"); | 916 testEncodeDecode("\x7f", "%7F"); |
909 testEncodeDecode("\x80", "%C2%80"); | 917 testEncodeDecode("\x80", "%C2%80"); |
910 testEncodeDecode("\u0800", "%E0%A0%80"); | 918 testEncodeDecode("\u0800", "%E0%A0%80"); |
911 // All characters not escaped by encodeFull. | 919 // All characters not escaped by encodeFull. |
912 var unescapedFull = | 920 var unescapedFull = r"abcdefghijklmnopqrstuvwxyz" |
913 r"abcdefghijklmnopqrstuvwxyz" | |
914 r"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 921 r"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
915 r"0123456789!#$&'()*+,-./:;=?@_~"; | 922 r"0123456789!#$&'()*+,-./:;=?@_~"; |
916 // ASCII characters escaped by encodeFull: | 923 // ASCII characters escaped by encodeFull: |
917 var escapedFull = | 924 var escapedFull = |
918 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" | 925 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
919 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" | 926 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" |
920 r' "%<>[\]^`{|}' | 927 r' "%<>[\]^`{|}' |
921 "\x7f"; | 928 "\x7f"; |
922 var escapedTo = | 929 var escapedTo = "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F" |
923 "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F" | |
924 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F" | 930 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F" |
925 "%20%22%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"; | 931 "%20%22%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"; |
926 testEncodeDecode(unescapedFull, unescapedFull); | 932 testEncodeDecode(unescapedFull, unescapedFull); |
927 testEncodeDecode(escapedFull, escapedTo); | 933 testEncodeDecode(escapedFull, escapedTo); |
928 var nonAscii = | 934 var nonAscii = |
929 "\x80-\xff-\u{100}-\u{7ff}-\u{800}-\u{ffff}-\u{10000}-\u{10ffff}"; | 935 "\x80-\xff-\u{100}-\u{7ff}-\u{800}-\u{ffff}-\u{10000}-\u{10ffff}"; |
930 var nonAsciiEncoding = | 936 var nonAsciiEncoding = "%C2%80-%C3%BF-%C4%80-%DF%BF-%E0%A0%80-%EF%BF%BF-" |
931 "%C2%80-%C3%BF-%C4%80-%DF%BF-%E0%A0%80-%EF%BF%BF-" | |
932 "%F0%90%80%80-%F4%8F%BF%BF"; | 937 "%F0%90%80%80-%F4%8F%BF%BF"; |
933 testEncodeDecode(nonAscii, nonAsciiEncoding); | 938 testEncodeDecode(nonAscii, nonAsciiEncoding); |
934 testEncodeDecode(s, "%F0%90%80%80"); | 939 testEncodeDecode(s, "%F0%90%80%80"); |
935 testEncodeDecodeComponent("A + B", "A%20%2B%20B"); | 940 testEncodeDecodeComponent("A + B", "A%20%2B%20B"); |
936 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); | 941 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); |
937 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); | 942 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); |
938 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); | 943 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); |
939 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); | 944 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); |
940 testEncodeDecodeComponent("\x7f", "%7F"); | 945 testEncodeDecodeComponent("\x7f", "%7F"); |
941 testEncodeDecodeComponent("\x80", "%C2%80"); | 946 testEncodeDecodeComponent("\x80", "%C2%80"); |
942 testEncodeDecodeComponent("\u0800", "%E0%A0%80"); | 947 testEncodeDecodeComponent("\u0800", "%E0%A0%80"); |
943 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); | 948 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); |
944 testEncodeDecodeComponent(s, "%F0%90%80%80"); | 949 testEncodeDecodeComponent(s, "%F0%90%80%80"); |
945 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); | 950 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); |
946 testEncodeDecodeQueryComponent( | 951 testEncodeDecodeQueryComponent( |
947 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); | 952 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); |
948 testEncodeDecodeComponent(nonAscii, nonAsciiEncoding); | 953 testEncodeDecodeComponent(nonAscii, nonAsciiEncoding); |
949 | 954 |
950 // Invalid URI - : and @ is swapped, port ("host") should be numeric. | 955 // Invalid URI - : and @ is swapped, port ("host") should be numeric. |
951 Expect.throws( | 956 Expect.throws(() => Uri.parse("file://user@password:host/path"), |
952 () => Uri.parse("file://user@password:host/path"), | |
953 (e) => e is FormatException); | 957 (e) => e is FormatException); |
954 | 958 |
955 testValidCharacters(); | 959 testValidCharacters(); |
956 testInvalidUrls(); | 960 testInvalidUrls(); |
957 testNormalization(); | 961 testNormalization(); |
958 testReplace(); | 962 testReplace(); |
959 testRegression28359(); | 963 testRegression28359(); |
960 } | 964 } |
961 | 965 |
962 String dump(Uri uri) { | 966 String dump(Uri uri) { |
963 return "URI: $uri\n" | 967 return "URI: $uri\n" |
964 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" | 968 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" |
965 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" | 969 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" |
966 " Host: ${uri.host} #${uri.host.length}\n" | 970 " Host: ${uri.host} #${uri.host.length}\n" |
967 " Port: ${uri.port}\n" | 971 " Port: ${uri.port}\n" |
968 " Path: ${uri.path} #${uri.path.length}\n" | 972 " Path: ${uri.path} #${uri.path.length}\n" |
969 " Query: ${uri.query} #${uri.query.length}\n" | 973 " Query: ${uri.query} #${uri.query.length}\n" |
970 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; | 974 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; |
971 } | 975 } |
OLD | NEW |