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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 base.resolve("../g;p/h;s").toString()); | 117 base.resolve("../g;p/h;s").toString()); |
118 } | 118 } |
119 | 119 |
120 void testResolvePath(String expected, String path) { | 120 void testResolvePath(String expected, String path) { |
121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path); | 121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path); |
122 Expect.equals( | 122 Expect.equals( |
123 "http://localhost$expected", | 123 "http://localhost$expected", |
124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); | 124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); |
125 } | 125 } |
126 | 126 |
127 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; | |
128 const DIGIT = r"0123456789"; | |
129 const PERCENT_ENCODED = "%00%ff"; | |
130 const SUBDELIM = r"!$&'()*+,;="; | |
131 | |
132 const SCHEMECHAR = "$ALPHA$DIGIT+-."; | |
133 const UNRESERVED = "$ALPHA$DIGIT-._~"; | |
134 const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED"; | |
135 const USERINFOCHAR = "$REGNAMECHAR:"; | |
136 | |
137 const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@"; | |
138 const PCHAR = "$PCHAR_NC:"; | |
139 const QUERYCHAR = "$PCHAR/?"; | |
140 | |
141 void testValidCharacters() { | |
142 // test that all valid characters are accepted. | |
143 | |
144 for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) { | |
145 for (var userinfo in ["", "@", "$USERINFOCHAR$USERINFOCHAR@", | |
146 "$USERINFOCHAR:$DIGIT@"]) { | |
147 for (var host in ["", "$REGNAMECHAR$REGNAMECHAR", | |
148 "255.255.255.256", // valid reg-name. | |
149 "[ffff::ffff:ffff]", "[ffff::255.255.255.255]"]) { | |
150 for (var port in ["", ":$DIGIT$DIGIT"]) { | |
151 var auth = "$userinfo$host$port"; | |
152 if (auth.isNotEmpty) auth = "//$auth"; | |
153 var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty. | |
154 if (auth.isNotEmpty) { | |
155 // Initial segment may be empty. | |
156 paths..add("//$PCHAR"); | |
157 } else { | |
158 // Path may begin with non-slash. | |
159 if (scheme.isEmpty) { | |
160 // Initial segment must not contain colon. | |
161 paths..add(PCHAR_NC) | |
162 ..add("$PCHAR_NC/$PCHAR") | |
163 ..add("$PCHAR_NC/$PCHAR/"); | |
164 } else { | |
165 paths..add(PCHAR) | |
166 ..add("$PCHAR/$PCHAR") | |
167 ..add("$PCHAR/$PCHAR/"); | |
168 } | |
169 } | |
170 for (var path in paths) { | |
171 for (var query in ["", "?", "?$QUERYCHAR"]) { | |
172 for (var fragment in ["", "#", "#$QUERYCHAR"]) { | |
173 var uri = "$scheme$auth$path$query$fragment"; | |
174 // Should not throw. | |
175 var result = Uri.parse(uri); | |
176 } | |
177 } | |
178 } | |
179 } | |
180 } | |
181 } | |
182 } | |
183 } | |
184 | |
185 void testInvalidUrls() { | |
186 void checkInvalid(uri) { | |
187 try { | |
188 var result = Uri.parse(uri); | |
189 Expect.fail("Invalid URI `$uri` parsed to $result\n" + dump(result)); | |
190 } on FormatException { | |
191 // Success. | |
192 } | |
193 } | |
194 checkInvalid("s%41://x.x/"); // No escapes in scheme, | |
195 // and no colon before slash in path. | |
196 checkInvalid("1a://x.x/"); // Scheme must start with letter, | |
197 // and no colon before slash in path. | |
198 checkInvalid(".a://x.x/"); // Scheme must start with letter, | |
199 // and no colon before slash in path. | |
200 checkInvalid("_:"); // Character not valid in scheme, | |
201 // and no colon before slash in path. | |
202 checkInvalid(":"); // Scheme must start with letter, | |
203 // and no colon before slash in path. | |
204 | |
205 void checkInvalidReplaced(uri, invalid, replacement) { | |
206 var source = uri.replaceAll('{}', invalid); | |
207 var expected = uri.replaceAll('{}', replacement); | |
208 var result = Uri.parse(source); | |
209 Expect.equals(expected, "$result", "Source: $source\n${dump(result)}"); | |
210 } | |
211 | |
212 // Regression test for http://dartbug.com/16081 | |
213 checkInvalidReplaced("http://www.example.org/red%09ros{}#red)", | |
214 "\u00e9", "%C3%A9"); | |
215 checkInvalidReplaced("http://r{}sum\{}.example.org", "\u00E9", "%C3%A9"); | |
216 | |
217 // Invalid characters. The characters must be rejected, even if normalizing | |
218 // the input would cause them to be valid (normalization happens after | |
219 // validation). | |
220 var invalidCharsAndReplacements = [ | |
221 "\xe7", "%C3%A7", // Arbitrary non-ASCII letter | |
222 " ", "%20", // Space, not allowed anywhere. | |
223 '"', "%22", // Quote, not allowed anywhere | |
224 "\x7f", "%7F", // DEL, not allowed anywhere | |
225 "\xdf", "%C3%9F", // German lower-case scharf-S. | |
226 // Becomes ASCII when upper-cased. | |
227 "\u0130", "%C4%B0", // Latin capital dotted I, | |
228 // becomes ASCII lower-case in Turkish. | |
229 "%\uFB03", "%25%EF%AC%83", // % + Ligature ffi, | |
230 // becomes ASCII when upper-cased, | |
231 // should not be read as "%FFI". | |
232 "\u212a", "%E2%84%AA", // Kelvin sign. Becomes ASCII when lower-cased. | |
233 "%1g", "%251g", // Invalid escape. | |
234 "\u{10000}", "%F0%90%80%80", // Non-BMP character as surrogate pair. | |
235 ]; | |
236 for (int i = 0; i < invalidCharsAndReplacements.length; i += 2) { | |
237 var invalid = invalidCharsAndReplacements[i]; | |
238 var valid = invalidCharsAndReplacements[i + 1]; | |
239 checkInvalid("A{}b:///".replaceAll('{}', invalid)); | |
240 checkInvalid("{}b:///".replaceAll('{}', invalid)); | |
241 checkInvalidReplaced("s://user{}info@x.x/", invalid, valid); | |
242 checkInvalidReplaced("s://reg{}name/", invalid, valid); | |
243 checkInvalid("s://regname:12{}45/".replaceAll("{}", invalid)); | |
244 checkInvalidReplaced("s://regname/p{}ath/", invalid, valid); | |
245 checkInvalidReplaced("/p{}ath/", invalid, valid); | |
246 checkInvalidReplaced("p{}ath/", invalid, valid); | |
247 checkInvalidReplaced("s://regname/path/?x{}x", invalid, valid); | |
248 checkInvalidReplaced("s://regname/path/#x{}x", invalid, valid); | |
249 checkInvalidReplaced("s://regname/path/??#x{}x", invalid, valid); | |
250 } | |
251 | |
252 // At most one @ in userinfo. | |
253 checkInvalid("s://x@x@x.x/"); | |
254 // No colon in host except before a port. | |
255 checkInvalid("s://x@x:x/"); | |
256 // At most one port. | |
257 checkInvalid("s://x@x:9:9/"); | |
258 // At most one #. | |
259 checkInvalid("s://x/x#foo#bar"); | |
260 // Colon in host implies port and port may not be empty. | |
261 checkInvalid("s://:/"); | |
262 // @ not allowed in scheme. | |
263 checkInvalid("s@://x:9/x?x#x"); | |
264 // IPv6 must be enclosed in [ and ] for Uri.parse. | |
265 // It is allowed un-enclosed as argument to Uri(hist: ) because we don't | |
Søren Gjesse
2014/06/19 10:25:54
hist? (hist og her? hist hvor vejen slog en bugt?)
| |
266 // need to delimit. | |
267 checkInvalid("s://ffff::ffff:1234/"); | |
268 } | |
269 | |
270 void testNormalization() { | |
271 // The Uri constructor and the Uri.parse function performs RFC-3986 | |
272 // syntax based normalization. | |
273 | |
274 var uri; | |
275 | |
276 // Scheme: Only case normalization. Schemes cannot contain escapes. | |
277 uri = Uri.parse("A:"); | |
278 Expect.equals("a", uri.scheme); | |
279 uri = Uri.parse("Z:"); | |
280 Expect.equals("z", uri.scheme); | |
281 uri = Uri.parse("$SCHEMECHAR:"); | |
282 Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme); | |
283 | |
284 // Percent escape normalization. | |
285 // Escapes of unreserved characters are converted to the character, | |
286 // subject to case normalization in reg-name. | |
287 for (var i = 0; i < UNRESERVED.length; i++) { | |
288 var char = UNRESERVED[i]; | |
289 var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf. | |
290 | |
291 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" | |
292 "?vV${escape}vV#wW${escape}wW"); | |
293 Expect.equals("xX${char}xX", uri.userInfo); | |
294 Expect.equals("yY${char}yY".toLowerCase(), uri.host); | |
295 Expect.equals("/zZ${char}zZ", uri.path); | |
296 Expect.equals("vV${char}vV", uri.query); | |
297 Expect.equals("wW${char}wW", uri.fragment); | |
298 } | |
299 | |
300 // Escapes of reserved characters are kept, but upper-cased. | |
301 for (var escape in ["%00", "%1f", "%7F", "%fF"]) { | |
302 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" | |
303 "?vV${escape}vV#wW${escape}wW"); | |
304 var normalizedEscape = escape.toUpperCase(); | |
305 Expect.equals("xX${normalizedEscape}xX", uri.userInfo); | |
306 Expect.equals("yy${normalizedEscape}yy", uri.host); | |
307 Expect.equals("/zZ${normalizedEscape}zZ", uri.path); | |
308 Expect.equals("vV${normalizedEscape}vV", uri.query); | |
309 Expect.equals("wW${normalizedEscape}wW", uri.fragment); | |
310 } | |
311 | |
312 // Some host normalization edge cases. | |
313 uri = Uri.parse("x://x%61X%41x%41X%61x/"); | |
314 Expect.equals("xaxaxaxax", uri.host); | |
315 | |
316 uri = Uri.parse("x://Xxxxxxxx/"); | |
317 Expect.equals("xxxxxxxx", uri.host); | |
318 | |
319 uri = Uri.parse("x://xxxxxxxX/"); | |
320 Expect.equals("xxxxxxxx", uri.host); | |
321 | |
322 uri = Uri.parse("x://xxxxxxxx%61/"); | |
323 Expect.equals("xxxxxxxxa", uri.host); | |
324 | |
325 uri = Uri.parse("x://%61xxxxxxxx/"); | |
326 Expect.equals("axxxxxxxx", uri.host); | |
327 | |
328 uri = Uri.parse("x://X/"); | |
329 Expect.equals("x", uri.host); | |
330 | |
331 uri = Uri.parse("x://%61/"); | |
332 Expect.equals("a", uri.host); | |
333 } | |
334 | |
127 main() { | 335 main() { |
128 testUri("http:", true); | 336 testUri("http:", true); |
129 testUri("file://", true); | 337 testUri("file://", true); |
130 testUri("file", false); | 338 testUri("file", false); |
131 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); | 339 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); |
132 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment", | 340 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment", |
133 false); | 341 false); |
134 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", | 342 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", |
135 new Uri( | 343 new Uri( |
136 scheme: "http", | 344 scheme: "http", |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); | 486 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); |
279 testEncodeDecodeQueryComponent( | 487 testEncodeDecodeQueryComponent( |
280 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); | 488 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); |
281 testEncodeDecodeComponent(nonAscii, nonAsciiEncoding); | 489 testEncodeDecodeComponent(nonAscii, nonAsciiEncoding); |
282 | 490 |
283 // Invalid URI - : and @ is swapped, port ("host") should be numeric. | 491 // Invalid URI - : and @ is swapped, port ("host") should be numeric. |
284 Expect.throws( | 492 Expect.throws( |
285 () => Uri.parse("file://user@password:host/path"), | 493 () => Uri.parse("file://user@password:host/path"), |
286 (e) => e is FormatException); | 494 (e) => e is FormatException); |
287 } | 495 } |
496 | |
497 String dump(Uri uri) { | |
498 return "URI: $uri\n" | |
499 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" | |
500 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" | |
501 " Host: ${uri.host} #${uri.host.length}\n" | |
502 " Port: ${uri.port}\n" | |
503 " Path: ${uri.path} #${uri.path.length}\n" | |
504 " Query: ${uri.query} #${uri.query.length}\n" | |
505 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; | |
506 } | |
OLD | NEW |