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

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

Issue 335373003: New Uri.parse and validation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Completed merge, updated tests. Created 6 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
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: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
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 }
265
266 void testNormalization() {
267 // The Uri constructor and the Uri.parse function performs RFC-3986
268 // syntax based normalization.
269
270 var uri;
271
272 // Scheme: Only case normalization. Schemes cannot contain escapes.
273 uri = Uri.parse("A:");
274 Expect.equals("a", uri.scheme);
275 uri = Uri.parse("Z:");
276 Expect.equals("z", uri.scheme);
277 uri = Uri.parse("$SCHEMECHAR:");
278 Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme);
279
280 // Percent escape normalization.
281 // Escapes of unreserved characters are converted to the character,
282 // subject to case normalization in reg-name.
283 for (var i = 0; i < UNRESERVED.length; i++) {
284 var char = UNRESERVED[i];
285 var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf.
286
287 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ"
288 "?vV${escape}vV#wW${escape}wW");
289 Expect.equals("xX${char}xX", uri.userInfo);
290 Expect.equals("yY${char}yY".toLowerCase(), uri.host);
291 Expect.equals("/zZ${char}zZ", uri.path);
292 Expect.equals("vV${char}vV", uri.query);
293 Expect.equals("wW${char}wW", uri.fragment);
294 }
295
296 // Escapes of reserved characters are kept, but upper-cased.
297 for (var escape in ["%00", "%1f", "%7F", "%fF"]) {
298 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ"
299 "?vV${escape}vV#wW${escape}wW");
300 var normalizedEscape = escape.toUpperCase();
301 Expect.equals("xX${normalizedEscape}xX", uri.userInfo);
302 Expect.equals("yy${normalizedEscape}yy", uri.host);
303 Expect.equals("/zZ${normalizedEscape}zZ", uri.path);
304 Expect.equals("vV${normalizedEscape}vV", uri.query);
305 Expect.equals("wW${normalizedEscape}wW", uri.fragment);
306 }
307
308 // Some host normalization edge cases.
309 uri = Uri.parse("x://x%61X%41x%41X%61x/");
310 Expect.equals("xaxaxaxax", uri.host);
311
312 uri = Uri.parse("x://Xxxxxxxx/");
313 Expect.equals("xxxxxxxx", uri.host);
314
315 uri = Uri.parse("x://xxxxxxxX/");
316 Expect.equals("xxxxxxxx", uri.host);
317
318 uri = Uri.parse("x://xxxxxxxx%61/");
319 Expect.equals("xxxxxxxxa", uri.host);
320
321 uri = Uri.parse("x://%61xxxxxxxx/");
322 Expect.equals("axxxxxxxx", uri.host);
323
324 uri = Uri.parse("x://X/");
325 Expect.equals("x", uri.host);
326
327 uri = Uri.parse("x://%61/");
328 Expect.equals("a", uri.host);
329
330 // TODO(lrn): Also do path normalization: /./ -> / and /x/../ -> /
Anders Johnsen 2014/06/17 13:07:04 Is this TODO a test or Uri todo?
Lasse Reichstein Nielsen 2014/06/17 14:31:45 It's a Uri-TODO, so I'll remove it from here.
331 }
332
127 main() { 333 main() {
128 testUri("http:", true); 334 testUri("http:", true);
129 testUri("file://", true); 335 testUri("file://", true);
130 testUri("file", false); 336 testUri("file", false);
131 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); 337 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", 338 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment",
133 false); 339 false);
134 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", 340 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment",
135 new Uri( 341 new Uri(
136 scheme: "http", 342 scheme: "http",
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 testEncodeDecodeComponent(s, "%F0%90%80%80"); 461 testEncodeDecodeComponent(s, "%F0%90%80%80");
256 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); 462 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B");
257 testEncodeDecodeQueryComponent( 463 testEncodeDecodeQueryComponent(
258 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); 464 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null);
259 465
260 // Invalid URI - : and @ is swapped, port ("host") should be numeric. 466 // Invalid URI - : and @ is swapped, port ("host") should be numeric.
261 Expect.throws( 467 Expect.throws(
262 () => Uri.parse("file://user@password:host/path"), 468 () => Uri.parse("file://user@password:host/path"),
263 (e) => e is FormatException); 469 (e) => e is FormatException);
264 } 470 }
471
472 String dump(Uri uri) {
Anders Johnsen 2014/06/17 13:07:04 Debug stuff?
Lasse Reichstein Nielsen 2014/06/17 14:31:45 Actually used in the error messages of some of the
473 return "URI: $uri\n"
474 " Scheme: ${uri.scheme} #${uri.scheme.length}\n"
475 " User-info: ${uri.userInfo} #${uri.userInfo.length}\n"
476 " Host: ${uri.host} #${uri.host.length}\n"
477 " Port: ${uri.port}\n"
478 " Path: ${uri.path} #${uri.path.length}\n"
479 " Query: ${uri.query} #${uri.query.length}\n"
480 " Fragment: ${uri.fragment} #${uri.fragment.length}\n";
481 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698