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) { |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 | 206 |
207 void testInvalidUrls() { | 207 void testInvalidUrls() { |
208 void checkInvalid(uri) { | 208 void checkInvalid(uri) { |
209 try { | 209 try { |
210 var result = Uri.parse(uri); | 210 var result = Uri.parse(uri); |
211 Expect.fail("Invalid URI `$uri` parsed to $result\n" + dump(result)); | 211 Expect.fail("Invalid URI `$uri` parsed to $result\n" + dump(result)); |
212 } on FormatException { | 212 } on FormatException { |
213 // Success. | 213 // Success. |
214 } | 214 } |
215 } | 215 } |
216 checkInvalid("s%41://x.x/"); // No escapes in scheme, | 216 checkInvalid("s%41://x.x/"); // No escapes in scheme, |
217 // and no colon before slash in path. | 217 // and no colon before slash in path. |
218 checkInvalid("1a://x.x/"); // Scheme must start with letter, | 218 checkInvalid("1a://x.x/"); // Scheme must start with letter, |
219 // and no colon before slash in path. | 219 // and no colon before slash in path. |
220 checkInvalid(".a://x.x/"); // Scheme must start with letter, | 220 checkInvalid(".a://x.x/"); // Scheme must start with letter, |
221 // and no colon before slash in path. | 221 // and no colon before slash in path. |
222 checkInvalid("_:"); // Character not valid in scheme, | 222 checkInvalid("_:"); // Character not valid in scheme, |
223 // and no colon before slash in path. | 223 // and no colon before slash in path. |
224 checkInvalid(":"); // Scheme must start with letter, | 224 checkInvalid(":"); // Scheme must start with letter, |
225 // and no colon before slash in path. | 225 // and no colon before slash in path. |
226 | 226 |
227 void checkInvalidReplaced(uri, invalid, replacement) { | 227 void checkInvalidReplaced(uri, invalid, replacement) { |
228 var source = uri.replaceAll('{}', invalid); | 228 var source = uri.replaceAll('{}', invalid); |
229 var expected = uri.replaceAll('{}', replacement); | 229 var expected = uri.replaceAll('{}', replacement); |
230 var result = Uri.parse(source); | 230 var result = Uri.parse(source); |
231 Expect.equals(expected, "$result", "Source: $source\n${dump(result)}"); | 231 Expect.equals(expected, "$result", "Source: $source\n${dump(result)}"); |
232 } | 232 } |
233 | 233 |
234 // Regression test for http://dartbug.com/16081 | 234 // Regression test for http://dartbug.com/16081 |
235 checkInvalidReplaced("http://www.example.org/red%09ros{}#red)", | 235 checkInvalidReplaced("http://www.example.org/red%09ros{}#red)", |
236 "\u00e9", "%C3%A9"); | 236 "\u00e9", "%C3%A9"); |
237 checkInvalidReplaced("http://r{}sum\{}.example.org", "\u00E9", "%C3%A9"); | 237 checkInvalidReplaced("http://r{}sum\{}.example.org", "\u00E9", "%C3%A9"); |
238 | 238 |
239 // Invalid characters. The characters must be rejected, even if normalizing | 239 // Invalid characters. The characters must be rejected, even if normalizing |
240 // the input would cause them to be valid (normalization happens after | 240 // the input would cause them to be valid (normalization happens after |
241 // validation). | 241 // validation). |
242 var invalidCharsAndReplacements = [ | 242 var invalidCharsAndReplacements = [ |
243 "\xe7", "%C3%A7", // Arbitrary non-ASCII letter | 243 "\xe7", "%C3%A7", // Arbitrary non-ASCII letter |
244 " ", "%20", // Space, not allowed anywhere. | 244 " ", "%20", // Space, not allowed anywhere. |
245 '"', "%22", // Quote, not allowed anywhere | 245 '"', "%22", // Quote, not allowed anywhere |
246 "<>", "%3C%3E", // Less/greater-than, not allowed anywhere. | 246 "<>", "%3C%3E", // Less/greater-than, not allowed anywhere. |
247 "\x7f", "%7F", // DEL, not allowed anywhere | 247 "\x7f", "%7F", // DEL, not allowed anywhere |
248 "\xdf", "%C3%9F", // German lower-case scharf-S. | 248 "\xdf", "%C3%9F", // German lower-case scharf-S. |
249 // Becomes ASCII when upper-cased. | 249 // Becomes ASCII when upper-cased. |
250 "\u0130", "%C4%B0", // Latin capital dotted I, | 250 "\u0130", "%C4%B0", // Latin capital dotted I, |
251 // becomes ASCII lower-case in Turkish. | 251 // becomes ASCII lower-case in Turkish. |
sra1
2017/03/21 03:39:29
fix?
| |
252 "%\uFB03", "%25%EF%AC%83", // % + Ligature ffi, | 252 "%\uFB03", "%25%EF%AC%83", // % + Ligature ffi, |
253 // becomes ASCII when upper-cased, | 253 // becomes ASCII when upper-cased, |
254 // should not be read as "%FFI". | 254 // should not be read as "%FFI". |
255 "\u212a", "%E2%84%AA", // Kelvin sign. Becomes ASCII when lower-cased. | 255 "\u212a", "%E2%84%AA", // Kelvin sign. Becomes ASCII when lower-cased. |
256 "%1g", "%251g", // Invalid escape. | 256 "%1g", "%251g", // Invalid escape. |
257 "\u{10000}", "%F0%90%80%80", // Non-BMP character as surrogate pair. | 257 "\u{10000}", "%F0%90%80%80", // Non-BMP character as surrogate pair. |
258 ]; | 258 ]; |
259 for (int i = 0; i < invalidCharsAndReplacements.length; i += 2) { | 259 for (int i = 0; i < invalidCharsAndReplacements.length; i += 2) { |
260 var invalid = invalidCharsAndReplacements[i]; | 260 var invalid = invalidCharsAndReplacements[i]; |
261 var valid = invalidCharsAndReplacements[i + 1]; | 261 var valid = invalidCharsAndReplacements[i + 1]; |
262 checkInvalid("A{}b:///".replaceAll('{}', invalid)); | 262 checkInvalid("A{}b:///".replaceAll('{}', invalid)); |
263 checkInvalid("{}b:///".replaceAll('{}', invalid)); | 263 checkInvalid("{}b:///".replaceAll('{}', invalid)); |
264 checkInvalidReplaced("s://user{}info@x.x/", invalid, valid); | 264 checkInvalidReplaced("s://user{}info@x.x/", invalid, valid); |
265 checkInvalidReplaced("s://reg{}name/", invalid, valid); | 265 checkInvalidReplaced("s://reg{}name/", invalid, valid); |
266 checkInvalid("s://regname:12{}45/".replaceAll("{}", invalid)); | 266 checkInvalid("s://regname:12{}45/".replaceAll("{}", invalid)); |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 String dump(Uri uri) { | 629 String dump(Uri uri) { |
630 return "URI: $uri\n" | 630 return "URI: $uri\n" |
631 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" | 631 " Scheme: ${uri.scheme} #${uri.scheme.length}\n" |
632 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" | 632 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n" |
633 " Host: ${uri.host} #${uri.host.length}\n" | 633 " Host: ${uri.host} #${uri.host.length}\n" |
634 " Port: ${uri.port}\n" | 634 " Port: ${uri.port}\n" |
635 " Path: ${uri.path} #${uri.path.length}\n" | 635 " Path: ${uri.path} #${uri.path.length}\n" |
636 " Query: ${uri.query} #${uri.query.length}\n" | 636 " Query: ${uri.query} #${uri.query.length}\n" |
637 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; | 637 " Fragment: ${uri.fragment} #${uri.fragment.length}\n"; |
638 } | 638 } |
OLD | NEW |