| 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('../../lib/utf/utf.dart'); | 7 #import('../../lib/utf/utf.dart'); |
| 8 #import('../../lib/uri/uri.dart'); | 8 #import('../../lib/uri/uri.dart'); |
| 9 | 9 |
| 10 testUri(String uri, bool isAbsolute) { | 10 testUri(String uri, bool isAbsolute) { |
| 11 Expect.equals(isAbsolute, new Uri.fromString(uri).isAbsolute()); | 11 Expect.equals(isAbsolute, new Uri.fromString(uri).isAbsolute()); |
| 12 Expect.equals(isAbsolute, new Uri(uri).isAbsolute()); |
| 12 Expect.stringEquals(uri, new Uri.fromString(uri).toString()); | 13 Expect.stringEquals(uri, new Uri.fromString(uri).toString()); |
| 14 Expect.stringEquals(uri, new Uri(uri).toString()); |
| 13 } | 15 } |
| 14 | 16 |
| 15 testEncodeDecode(String orig, String encoded) { | 17 testEncodeDecode(String orig, String encoded) { |
| 16 var e = encodeUri(orig); | 18 var e = encodeUri(orig); |
| 17 Expect.stringEquals(encoded, e); | 19 Expect.stringEquals(encoded, e); |
| 18 var d = decodeUri(encoded); | 20 var d = decodeUri(encoded); |
| 19 Expect.stringEquals(orig, d); | 21 Expect.stringEquals(orig, d); |
| 20 } | 22 } |
| 21 | 23 |
| 22 testEncodeDecodeComponent(String orig, String encoded) { | 24 testEncodeDecodeComponent(String orig, String encoded) { |
| 23 var e = encodeUriComponent(orig); | 25 var e = encodeUriComponent(orig); |
| 24 Expect.stringEquals(encoded, e); | 26 Expect.stringEquals(encoded, e); |
| 25 var d = decodeUriComponent(encoded); | 27 var d = decodeUriComponent(encoded); |
| 26 Expect.stringEquals(orig, d); | 28 Expect.stringEquals(orig, d); |
| 27 } | 29 } |
| 28 | 30 |
| 29 main() { | 31 testUriPerRFCs(URI base) { |
| 30 testUri("http:", true); | |
| 31 testUri("file://", true); | |
| 32 testUri("file", false); | |
| 33 testUri("http://user@example.com:80/fisk?query=89&hest=silas", true); | |
| 34 testUri("http://user@example.com:80/fisk?query=89&hest=silas#fragment", | |
| 35 false); | |
| 36 Expect.stringEquals("http://user@example.com:80/a/b/c?query#fragment", | |
| 37 const Uri("http", "user", "example.com", 80, "/a/b/c", | |
| 38 "query", "fragment").toString()); | |
| 39 Expect.stringEquals("null://null@null/a/b/c/?null#null", | |
| 40 const Uri(null, null, null, 0, "/a/b/c/", | |
| 41 null, null).toString()); | |
| 42 Expect.stringEquals("file://", new Uri.fromString("file:").toString()); | |
| 43 Expect.stringEquals("/a/g", removeDotSegments("/a/b/c/./../../g")); | |
| 44 Expect.stringEquals("mid/6", removeDotSegments("mid/content=5/../6")); | |
| 45 Expect.stringEquals("a/b/e", removeDotSegments("a/b/c/d/../../e")); | |
| 46 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/c/d/../../e")); | |
| 47 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/c/d/../../e")); | |
| 48 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/./c/d/../../e")); | |
| 49 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/./c/d/../../e")); | |
| 50 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/.")); | |
| 51 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/./.")); | |
| 52 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/././.")); | |
| 53 | |
| 54 // From RFC 3986. | 32 // From RFC 3986. |
| 55 Uri base = new Uri.fromString("http://a/b/c/d;p?q"); | |
| 56 Expect.stringEquals("g:h", base.resolve("g:h").toString()); | 33 Expect.stringEquals("g:h", base.resolve("g:h").toString()); |
| 57 Expect.stringEquals("http://a/b/c/g", base.resolve("g").toString()); | 34 Expect.stringEquals("http://a/b/c/g", base.resolve("g").toString()); |
| 58 Expect.stringEquals("http://a/b/c/g", base.resolve("./g").toString()); | 35 Expect.stringEquals("http://a/b/c/g", base.resolve("./g").toString()); |
| 59 Expect.stringEquals("http://a/b/c/g/", base.resolve("g/").toString()); | 36 Expect.stringEquals("http://a/b/c/g/", base.resolve("g/").toString()); |
| 60 Expect.stringEquals("http://a/g", base.resolve("/g").toString()); | 37 Expect.stringEquals("http://a/g", base.resolve("/g").toString()); |
| 61 Expect.stringEquals("http://g", base.resolve("//g").toString()); | 38 Expect.stringEquals("http://g", base.resolve("//g").toString()); |
| 62 Expect.stringEquals("http://a/b/c/d;p?y", base.resolve("?y").toString()); | 39 Expect.stringEquals("http://a/b/c/d;p?y", base.resolve("?y").toString()); |
| 63 Expect.stringEquals("http://a/b/c/g?y", base.resolve("g?y").toString()); | 40 Expect.stringEquals("http://a/b/c/g?y", base.resolve("g?y").toString()); |
| 64 Expect.stringEquals("http://a/b/c/d;p?q#s", base.resolve("#s").toString()); | 41 Expect.stringEquals("http://a/b/c/d;p?q#s", base.resolve("#s").toString()); |
| 65 Expect.stringEquals("http://a/b/c/g#s", base.resolve("g#s").toString()); | 42 Expect.stringEquals("http://a/b/c/g#s", base.resolve("g#s").toString()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 base.resolve("g?y/../x").toString()); | 75 base.resolve("g?y/../x").toString()); |
| 99 Expect.stringEquals("http://a/b/c/g#s/./x", | 76 Expect.stringEquals("http://a/b/c/g#s/./x", |
| 100 base.resolve("g#s/./x").toString()); | 77 base.resolve("g#s/./x").toString()); |
| 101 Expect.stringEquals("http://a/b/c/g#s/../x", | 78 Expect.stringEquals("http://a/b/c/g#s/../x", |
| 102 base.resolve("g#s/../x").toString()); | 79 base.resolve("g#s/../x").toString()); |
| 103 Expect.stringEquals("http:g", base.resolve("http:g").toString()); | 80 Expect.stringEquals("http:g", base.resolve("http:g").toString()); |
| 104 | 81 |
| 105 // Additional tests (not from RFC 3986). | 82 // Additional tests (not from RFC 3986). |
| 106 Expect.stringEquals("http://a/b/g;p/h;s", | 83 Expect.stringEquals("http://a/b/g;p/h;s", |
| 107 base.resolve("../g;p/h;s").toString()); | 84 base.resolve("../g;p/h;s").toString()); |
| 85 } |
| 86 |
| 87 main() { |
| 88 testUri("http:", true); |
| 89 testUri("file://", true); |
| 90 testUri("file", false); |
| 91 testUri("http://user@example.com:80/fisk?query=89&hest=silas", true); |
| 92 testUri("http://user@example.com:80/fisk?query=89&hest=silas#fragment", |
| 93 false); |
| 94 Expect.stringEquals("http://user@example.com:80/a/b/c?query#fragment", |
| 95 const Uri.fromComponents( |
| 96 "http", "user", "example.com", 80, "/a/b/c", |
| 97 "query", "fragment").toString()); |
| 98 Expect.stringEquals("null://null@null/a/b/c/?null#null", |
| 99 const Uri.fromComponents( |
| 100 null, null, null, 0, "/a/b/c/", |
| 101 null, null).toString()); |
| 102 Expect.stringEquals("file://", new Uri.fromString("file:").toString()); |
| 103 Expect.stringEquals("file://", new Uri("file:").toString()); |
| 104 Expect.stringEquals("/a/g", removeDotSegments("/a/b/c/./../../g")); |
| 105 Expect.stringEquals("mid/6", removeDotSegments("mid/content=5/../6")); |
| 106 Expect.stringEquals("a/b/e", removeDotSegments("a/b/c/d/../../e")); |
| 107 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/c/d/../../e")); |
| 108 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/c/d/../../e")); |
| 109 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/./c/d/../../e")); |
| 110 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/./c/d/../../e")); |
| 111 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/.")); |
| 112 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/./.")); |
| 113 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/././.")); |
| 114 |
| 115 final urisSample = "http://a/b/c/d;p?q"; |
| 116 Uri baseFromString = new Uri.fromString(urisSample); |
| 117 testUriPerRFCs(baseFromString); |
| 118 Uri base = new Uri(urisSample); |
| 119 testUriPerRFCs(base); |
| 108 | 120 |
| 109 // URI encode tests | 121 // URI encode tests |
| 110 // Note: dart2js won't handle '\ud800\udc00' and frog | 122 // Note: dart2js won't handle '\ud800\udc00' and frog |
| 111 // won't handle '\u{10000}'. So we cons this up synthetically... | 123 // won't handle '\u{10000}'. So we cons this up synthetically... |
| 112 var s = decodeUtf8([0xf0, 0x90, 0x80, 0x80]); | 124 var s = decodeUtf8([0xf0, 0x90, 0x80, 0x80]); |
| 113 | 125 |
| 114 testEncodeDecode("\uFFFE", "%EF%BF%BE"); | 126 testEncodeDecode("\uFFFE", "%EF%BF%BE"); |
| 115 testEncodeDecode("\uFFFF", "%EF%BF%BF"); | 127 testEncodeDecode("\uFFFF", "%EF%BF%BF"); |
| 116 testEncodeDecode("\uFFFE", "%EF%BF%BE"); | 128 testEncodeDecode("\uFFFE", "%EF%BF%BE"); |
| 117 testEncodeDecode("\uFFFF", "%EF%BF%BF"); | 129 testEncodeDecode("\uFFFF", "%EF%BF%BF"); |
| 118 testEncodeDecode("\x7f", "%7F"); | 130 testEncodeDecode("\x7f", "%7F"); |
| 119 testEncodeDecode("\x80", "%C2%80"); | 131 testEncodeDecode("\x80", "%C2%80"); |
| 120 testEncodeDecode("\u0800", "%E0%A0%80"); | 132 testEncodeDecode("\u0800", "%E0%A0%80"); |
| 121 testEncodeDecode(":/@',;?&=+\$", ":/@',;?&=+\$"); | 133 testEncodeDecode(":/@',;?&=+\$", ":/@',;?&=+\$"); |
| 122 testEncodeDecode(s, "%F0%90%80%80"); | 134 testEncodeDecode(s, "%F0%90%80%80"); |
| 123 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); | 135 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); |
| 124 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); | 136 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); |
| 125 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); | 137 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); |
| 126 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); | 138 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); |
| 127 testEncodeDecodeComponent("\x7f", "%7F"); | 139 testEncodeDecodeComponent("\x7f", "%7F"); |
| 128 testEncodeDecodeComponent("\x80", "%C2%80"); | 140 testEncodeDecodeComponent("\x80", "%C2%80"); |
| 129 testEncodeDecodeComponent("\u0800", "%E0%A0%80"); | 141 testEncodeDecodeComponent("\u0800", "%E0%A0%80"); |
| 130 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); | 142 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); |
| 131 testEncodeDecodeComponent(s, "%F0%90%80%80"); | 143 testEncodeDecodeComponent(s, "%F0%90%80%80"); |
| 132 } | 144 } |
| OLD | NEW |