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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 | 7 |
8 void testUriCombi() { | 8 void testUriCombi() { |
9 var schemes = ["", "file", "ws", "ftp"]; | 9 var schemes = ["", "file", "ws", "ftp"]; |
10 var fragments = ["", "#", "#f", "#fragment", "#l:?/"]; | 10 var fragments = ["", "#", "#f", "#fragment", "#l:?/"]; |
11 var queries = ["", "?", "?q", "?query", "?q:/"]; | 11 var queries = ["", "?", "?q", "?query", "?q:/"]; |
12 var paths = ["/", "/x", "/x/y", "/x/y/", "/x:y"]; | 12 var paths = ["/", "/x", "/x/y", "/x/y/", "/x:y", "x", "x/y", "x/y/"]; |
13 var userInfos = ["", "x", "xxx", "x:4", "xxx:444", "x:4:x"]; | 13 var userInfos = ["", "x", "xxx", "x:4", "xxx:444", "x:4:x"]; |
14 var hosts = ["", "h", "hhh", "h:4", "hhh:444", "[::1.2.3.4]"]; | 14 var hosts = ["", "h", "hhh", "h:4", "hhh:444", "[::1.2.3.4]"]; |
15 | 15 |
16 void check(uriString, scheme, fragment, query, path, user, host) { | 16 void check(uriString, scheme, fragment, query, path, user, host) { |
17 var uri = Uri.parse(uriString); | 17 for (var uri in [Uri.parse(uriString), |
18 Expect.equals(scheme, uri.scheme); | 18 Uri.parse(">\u{10000}>$uriString<\u{10000}<", |
19 var uriFragment = uri.fragment; | 19 4, uriString.length + 4), |
20 if (fragment.startsWith('#')) uriFragment = "#$uriFragment"; | 20 Uri.parse("http://example.com/$uriString#?:/[]\"", |
21 Expect.equals(fragment, uriFragment); | 21 19, uriString.length + 19), |
22 var uriQuery = uri.query; | 22 Uri.parse(uriString * 3, |
23 if (query.startsWith('?')) uriQuery = "?$uriQuery"; | 23 uriString.length, uriString.length * 2)]) { |
24 Expect.equals(query, uriQuery); | 24 String name = "$uriString -> $uri"; |
25 Expect.equals(path, uri.path); | 25 Expect.equals(scheme, uri.scheme, name); |
26 Expect.equals(user, uri.userInfo); | 26 var uriFragment = uri.fragment; |
27 var uriHost = uri.host; | 27 if (fragment.startsWith('#')) uriFragment = "#$uriFragment"; |
28 if (host.startsWith("[")) uriHost = "[$uriHost]"; | 28 Expect.equals(fragment, uriFragment, name); |
29 if (uri.port != 0) uriHost += ":${uri.port}"; | 29 var uriQuery = uri.query; |
30 Expect.equals(host, uriHost); | 30 if (query.startsWith('?')) uriQuery = "?$uriQuery"; |
| 31 Expect.equals(query, uriQuery, name); |
| 32 Expect.equals(path, uri.path, name); |
| 33 Expect.equals(user, uri.userInfo, name); |
| 34 var uriHost = uri.host; |
| 35 if (host.startsWith("[")) uriHost = "[$uriHost]"; |
| 36 if (uri.port != 0) uriHost += ":${uri.port}"; |
| 37 Expect.equals(host, uriHost, name); |
| 38 } |
31 } | 39 } |
32 | 40 |
33 for (var scheme in schemes) { | 41 for (var scheme in schemes) { |
34 for (var fragment in fragments) { | 42 for (var fragment in fragments) { |
35 for (var query in queries) { | 43 for (var query in queries) { |
36 for (var path in paths) { | 44 for (var path in paths) { |
| 45 // File scheme URIs always get a leading slash. |
| 46 if (scheme == "file" && !path.startsWith('/')) continue; |
37 for (var user in userInfos) { | 47 for (var user in userInfos) { |
38 for (var host in hosts) { | 48 for (var host in hosts) { |
39 var auth = host; | 49 var auth = host; |
40 var s = scheme; | 50 var s = scheme; |
41 if (user.isNotEmpty) auth = "$user@$auth"; | 51 if (user.isNotEmpty) auth = "$user@$auth"; |
42 if (auth.isNotEmpty) auth = "//$auth"; | 52 if (auth.isNotEmpty) auth = "//$auth"; |
| 53 if (auth.isNotEmpty && !path.startsWith('/')) continue; |
43 check("$scheme${scheme.isEmpty ? "" : ":"}" | 54 check("$scheme${scheme.isEmpty ? "" : ":"}" |
44 "$auth$path$query$fragment", | 55 "$auth$path$query$fragment", |
45 scheme, | 56 scheme, |
46 fragment, | 57 fragment, |
47 query, | 58 query, |
48 path, | 59 path, |
49 user, | 60 user, |
50 host); | 61 host); |
51 } | 62 } |
52 } | 63 } |
53 } | 64 } |
54 } | 65 } |
55 } | 66 } |
56 } | 67 } |
57 } | 68 } |
58 | 69 |
59 void main() { | 70 void main() { |
60 testUriCombi(); | 71 testUriCombi(); |
61 } | 72 } |
OLD | NEW |