Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: tests/corelib/uri_test.dart

Issue 16019002: Merge the dart:uri library into dart:core and update the Uri class (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Final cleanup Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/corelib/uri_scheme_test.dart ('k') | tests/html/form_data_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:utf'; 8 import 'dart:utf';
9 import 'dart:uri';
10 9
11 testUri(String uri, bool isAbsolute) { 10 testUri(String uri, bool isAbsolute) {
12 Expect.equals(isAbsolute, Uri.parse(uri).isAbsolute); 11 Expect.equals(isAbsolute, Uri.parse(uri).isAbsolute);
13 Expect.equals(isAbsolute, new Uri(uri).isAbsolute);
14 Expect.stringEquals(uri, Uri.parse(uri).toString()); 12 Expect.stringEquals(uri, Uri.parse(uri).toString());
15 Expect.stringEquals(uri, new Uri(uri).toString());
16 13
17 // Test equals and hashCode members. 14 // Test equals and hashCode members.
18 Expect.equals(new Uri(uri), new Uri(uri)); 15 Expect.equals(Uri.parse(uri), Uri.parse(uri));
19 Expect.equals(new Uri(uri).hashCode, new Uri(uri).hashCode); 16 Expect.equals(Uri.parse(uri).hashCode, Uri.parse(uri).hashCode);
20 } 17 }
21 18
22 testEncodeDecode(String orig, String encoded) { 19 testEncodeDecode(String orig, String encoded) {
23 var e = encodeUri(orig); 20 var e = Uri.encodeFull(orig);
24 Expect.stringEquals(encoded, e); 21 Expect.stringEquals(encoded, e);
25 var d = decodeUri(encoded); 22 var d = Uri.decodeFull(encoded);
26 Expect.stringEquals(orig, d); 23 Expect.stringEquals(orig, d);
27 } 24 }
28 25
29 testEncodeDecodeComponent(String orig, String encoded) { 26 testEncodeDecodeComponent(String orig, String encoded) {
30 var e = encodeUriComponent(orig); 27 var e = Uri.encodeComponent(orig);
31 Expect.stringEquals(encoded, e); 28 Expect.stringEquals(encoded, e);
32 var d = decodeUriComponent(encoded); 29 var d = Uri.decodeComponent(encoded);
33 Expect.stringEquals(orig, d); 30 Expect.stringEquals(orig, d);
34 } 31 }
35 32
33 testEncodeDecodeQueryComponent(String orig, String encoded) {
34 var e = Uri.encodeQueryComponent(orig);
35 Expect.stringEquals(encoded, e);
36 var d = Uri.decodeQueryComponent(encoded);
37 Expect.stringEquals(orig, d);
38 }
39
36 testUriPerRFCs(Uri base) { 40 testUriPerRFCs(Uri base) {
37 // From RFC 3986. 41 // From RFC 3986.
38 Expect.stringEquals("g:h", base.resolve("g:h").toString()); 42 Expect.stringEquals("g:h", base.resolve("g:h").toString());
39 Expect.stringEquals("http://a/b/c/g", base.resolve("g").toString()); 43 Expect.stringEquals("http://a/b/c/g", base.resolve("g").toString());
40 Expect.stringEquals("http://a/b/c/g", base.resolve("./g").toString()); 44 Expect.stringEquals("http://a/b/c/g", base.resolve("./g").toString());
41 Expect.stringEquals("http://a/b/c/g/", base.resolve("g/").toString()); 45 Expect.stringEquals("http://a/b/c/g/", base.resolve("g/").toString());
42 Expect.stringEquals("http://a/g", base.resolve("/g").toString()); 46 Expect.stringEquals("http://a/g", base.resolve("/g").toString());
43 Expect.stringEquals("http://g", base.resolve("//g").toString()); 47 Expect.stringEquals("http://g", base.resolve("//g").toString());
44 Expect.stringEquals("http://a/b/c/d;p?y", base.resolve("?y").toString()); 48 Expect.stringEquals("http://a/b/c/d;p?y", base.resolve("?y").toString());
45 Expect.stringEquals("http://a/b/c/g?y", base.resolve("g?y").toString()); 49 Expect.stringEquals("http://a/b/c/g?y", base.resolve("g?y").toString());
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 base.resolve("g#s/./x").toString()); 86 base.resolve("g#s/./x").toString());
83 Expect.stringEquals("http://a/b/c/g#s/../x", 87 Expect.stringEquals("http://a/b/c/g#s/../x",
84 base.resolve("g#s/../x").toString()); 88 base.resolve("g#s/../x").toString());
85 Expect.stringEquals("http:g", base.resolve("http:g").toString()); 89 Expect.stringEquals("http:g", base.resolve("http:g").toString());
86 90
87 // Additional tests (not from RFC 3986). 91 // Additional tests (not from RFC 3986).
88 Expect.stringEquals("http://a/b/g;p/h;s", 92 Expect.stringEquals("http://a/b/g;p/h;s",
89 base.resolve("../g;p/h;s").toString()); 93 base.resolve("../g;p/h;s").toString());
90 } 94 }
91 95
96 void testResolvePath(String expected, String path) {
97 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path);
98 Expect.equals(
99 "http://localhost$expected",
100 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString());
101 }
102
92 main() { 103 main() {
93 testUri("http:", true); 104 testUri("http:", true);
94 testUri("file://", true); 105 testUri("file://", true);
95 testUri("file", false); 106 testUri("file", false);
96 testUri("http://user@example.com:80/fisk?query=89&hest=silas", true); 107 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true);
97 testUri("http://user@example.com:80/fisk?query=89&hest=silas#fragment", 108 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment",
98 false); 109 false);
99 Expect.stringEquals("http://user@example.com:80/a/b/c?query#fragment", 110 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment",
100 const Uri.fromComponents( 111 new Uri(
101 scheme: "http", 112 scheme: "http",
102 userInfo: "user", 113 userInfo: "user",
103 domain: "example.com", 114 host: "example.com",
104 port: 80, 115 port: 80,
105 path: "/a/b/c", 116 path: "/a/b/c",
106 query: "query", 117 query: "query",
107 fragment: "fragment").toString()); 118 fragment: "fragment").toString());
108 Expect.stringEquals("null://null@null/a/b/c/?null#null", 119 Expect.stringEquals("//null@null/a/b/c/",
109 const Uri.fromComponents( 120 new Uri(
110 scheme: null, 121 scheme: null,
111 userInfo: null, 122 userInfo: null,
112 domain: null, 123 host: null,
113 port: 0, 124 port: 0,
114 path: "/a/b/c/", 125 path: "/a/b/c/",
115 query: null, 126 query: null,
116 fragment: null).toString()); 127 fragment: null).toString());
117 Expect.stringEquals("file://", Uri.parse("file:").toString()); 128 Expect.stringEquals("file://", Uri.parse("file:").toString());
118 Expect.stringEquals("file://", new Uri("file:").toString()); 129
119 Expect.stringEquals("/a/g", removeDotSegments("/a/b/c/./../../g")); 130 testResolvePath("/a/g", "/a/b/c/./../../g");
120 Expect.stringEquals("mid/6", removeDotSegments("mid/content=5/../6")); 131 testResolvePath("/a/g", "/a/b/c/./../../g");
121 Expect.stringEquals("a/b/e", removeDotSegments("a/b/c/d/../../e")); 132 testResolvePath("/mid/6", "mid/content=5/../6");
122 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/c/d/../../e")); 133 testResolvePath("/a/b/e", "a/b/c/d/../../e");
123 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/c/d/../../e")); 134 testResolvePath("/a/b/e", "../a/b/c/d/../../e");
124 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/./c/d/../../e")); 135 testResolvePath("/a/b/e", "./a/b/c/d/../../e");
125 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/./c/d/../../e")); 136 testResolvePath("/a/b/e", "../a/b/./c/d/../../e");
126 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/.")); 137 testResolvePath("/a/b/e", "./a/b/./c/d/../../e");
127 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/./.")); 138 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/.");
128 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/././.")); 139 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./.");
140 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/././.");
129 141
130 final urisSample = "http://a/b/c/d;p?q"; 142 final urisSample = "http://a/b/c/d;p?q";
131 Uri baseFromString = Uri.parse(urisSample); 143 Uri baseFromString = Uri.parse(urisSample);
132 testUriPerRFCs(baseFromString); 144 testUriPerRFCs(baseFromString);
133 Uri base = new Uri(urisSample); 145 Uri base = Uri.parse(urisSample);
134 testUriPerRFCs(base); 146 testUriPerRFCs(base);
135 147
136 Expect.stringEquals( 148 Expect.stringEquals(
137 "http://example.com", 149 "http://example.com",
138 Uri.parse("http://example.com/a/b/c").origin); 150 Uri.parse("http://example.com/a/b/c").origin);
139 Expect.stringEquals( 151 Expect.stringEquals(
140 "https://example.com", 152 "https://example.com",
141 Uri.parse("https://example.com/a/b/c").origin); 153 Uri.parse("https://example.com/a/b/c").origin);
142 Expect.stringEquals( 154 Expect.stringEquals(
143 "http://example.com:1234", 155 "http://example.com:1234",
144 Uri.parse("http://example.com:1234/a/b/c").origin); 156 Uri.parse("http://example.com:1234/a/b/c").origin);
145 Expect.stringEquals( 157 Expect.stringEquals(
146 "https://example.com:1234", 158 "https://example.com:1234",
147 Uri.parse("https://example.com:1234/a/b/c").origin); 159 Uri.parse("https://example.com:1234/a/b/c").origin);
148 Expect.throws( 160 Expect.throws(
149 () => Uri.parse("http:").origin, 161 () => Uri.parse("http:").origin,
150 (e) { return e is ArgumentError; }, 162 (e) { return e is StateError; },
151 "origin for uri with empty domain should fail"); 163 "origin for uri with empty host should fail");
152 Expect.throws( 164 Expect.throws(
153 () => const Uri.fromComponents( 165 () => new Uri(
154 scheme: "http", 166 scheme: "http",
155 userInfo: null, 167 userInfo: null,
156 domain: "", 168 host: "",
157 port: 80, 169 port: 80,
158 path: "/a/b/c", 170 path: "/a/b/c",
159 query: "query", 171 query: "query",
160 fragment: "fragment").origin, 172 fragment: "fragment").origin,
161 (e) { return e is ArgumentError; }, 173 (e) { return e is StateError; },
162 "origin for uri with empty domain should fail"); 174 "origin for uri with empty host should fail");
163 Expect.throws( 175 Expect.throws(
164 () => const Uri.fromComponents( 176 () => new Uri(
165 scheme: null, 177 scheme: null,
166 userInfo: null, 178 userInfo: null,
167 domain: "", 179 host: "",
168 port: 80, 180 port: 80,
169 path: "/a/b/c", 181 path: "/a/b/c",
170 query: "query", 182 query: "query",
171 fragment: "fragment").origin, 183 fragment: "fragment").origin,
172 (e) { return e is ArgumentError; }, 184 (e) { return e is StateError; },
173 "origin for uri with empty scheme should fail"); 185 "origin for uri with empty scheme should fail");
174 Expect.throws( 186 Expect.throws(
175 () => const Uri.fromComponents( 187 () => new Uri(
176 scheme: "http", 188 scheme: "http",
177 userInfo: null, 189 userInfo: null,
178 domain: null, 190 host: null,
179 port: 80, 191 port: 80,
180 path: "/a/b/c", 192 path: "/a/b/c",
181 query: "query", 193 query: "query",
182 fragment: "fragment").origin, 194 fragment: "fragment").origin,
183 (e) { return e is ArgumentError; }, 195 (e) { return e is StateError; },
184 "origin for uri with empty domain should fail"); 196 "origin for uri with empty host should fail");
185 Expect.throws( 197 Expect.throws(
186 () => Uri.parse("http://:80").origin, 198 () => Uri.parse("http://:80").origin,
187 (e) { return e is ArgumentError; }, 199 (e) { return e is StateError; },
188 "origin for uri with empty domain should fail"); 200 "origin for uri with empty host should fail");
189 Expect.throws( 201 Expect.throws(
190 () => Uri.parse("file://localhost/test.txt").origin, 202 () => Uri.parse("file://localhost/test.txt").origin,
191 (e) { return e is ArgumentError; }, 203 (e) { return e is StateError; },
192 "origin for non-http/https uri should fail"); 204 "origin for non-http/https uri should fail");
193 205
194 // URI encode tests 206 // URI encode tests
195 // Create a string with code point 0x10000 encoded as a surrogate pair. 207 // Create a string with code point 0x10000 encoded as a surrogate pair.
196 var s = decodeUtf8([0xf0, 0x90, 0x80, 0x80]); 208 var s = decodeUtf8([0xf0, 0x90, 0x80, 0x80]);
197 209
198 Expect.stringEquals("\u{10000}", s); 210 Expect.stringEquals("\u{10000}", s);
199 211
200 testEncodeDecode("A + B", "A+%2B+B"); 212 testEncodeDecode("A + B", "A%20%2B%20B");
201 testEncodeDecode("\uFFFE", "%EF%BF%BE"); 213 testEncodeDecode("\uFFFE", "%EF%BF%BE");
202 testEncodeDecode("\uFFFF", "%EF%BF%BF"); 214 testEncodeDecode("\uFFFF", "%EF%BF%BF");
203 testEncodeDecode("\uFFFE", "%EF%BF%BE"); 215 testEncodeDecode("\uFFFE", "%EF%BF%BE");
204 testEncodeDecode("\uFFFF", "%EF%BF%BF"); 216 testEncodeDecode("\uFFFF", "%EF%BF%BF");
205 testEncodeDecode("\x7f", "%7F"); 217 testEncodeDecode("\x7f", "%7F");
206 testEncodeDecode("\x80", "%C2%80"); 218 testEncodeDecode("\x80", "%C2%80");
207 testEncodeDecode("\u0800", "%E0%A0%80"); 219 testEncodeDecode("\u0800", "%E0%A0%80");
208 testEncodeDecode(":/@',;?&=+\$", ":/@',;?&=%2B\$"); 220 testEncodeDecode(":/@',;?&=+\$", ":/@',;?&=%2B\$");
209 testEncodeDecode(s, "%F0%90%80%80"); 221 testEncodeDecode(s, "%F0%90%80%80");
222 testEncodeDecodeComponent("A + B", "A%20%2B%20B");
210 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); 223 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE");
211 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); 224 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF");
212 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); 225 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE");
213 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); 226 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF");
214 testEncodeDecodeComponent("\x7f", "%7F"); 227 testEncodeDecodeComponent("\x7f", "%7F");
215 testEncodeDecodeComponent("\x80", "%C2%80"); 228 testEncodeDecodeComponent("\x80", "%C2%80");
216 testEncodeDecodeComponent("\u0800", "%E0%A0%80"); 229 testEncodeDecodeComponent("\u0800", "%E0%A0%80");
217 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); 230 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24");
218 testEncodeDecodeComponent(s, "%F0%90%80%80"); 231 testEncodeDecodeComponent(s, "%F0%90%80%80");
232 testEncodeDecodeQueryComponent("A + B", "A+%2B+B");
219 } 233 }
OLDNEW
« no previous file with comments | « tests/corelib/uri_scheme_test.dart ('k') | tests/html/form_data_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698