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

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

Issue 1381033002: Add data-URI support class to dart:core (next to Uri). (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Update CHANGELOG.md Created 5 years, 2 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
« sdk/lib/core/uri.dart ('K') | « sdk/lib/core/uri.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4
5 import "package:expect/expect.dart";
6 import "dart:convert";
7 import "dart:typed_data";
8
9 main() {
10 testRoundTrip("");
11 testRoundTrip("a");
12 testRoundTrip("ab");
13 testRoundTrip("abc");
14 testRoundTrip("abcd");
15
16 testUtf8Encoding("\u1000\uffff");
17 testBytes();
18 testInvalidCharacters();
19 testErrors();
20 }
nweiz 2015/10/15 21:09:04 Also test: * Percent-encoding non-token character
Lasse Reichstein Nielsen 2015/11/03 18:02:52 Good points. Also, I should test the "base64" and
21
22 void testRoundTrip(String content) {
23 DataUri dataUri = new DataUri.fromString(content);
24 Expect.equals(content, dataUri.contentAsString());
25 Expect.listEquals(content.codeUnits, dataUri.contentAsBytes());
26
27 Uri uri = dataUri.toUri();
28 Expect.equals(uri.toString(), dataUri.toString());
29 Expect.equals(dataUri.toString(), new DataUri.fromUri(uri).toString());
30
31 dataUri = new DataUri.fromBytes(content.codeUnits);
32 Expect.listEquals(content.codeUnits, dataUri.contentAsBytes());
33 Expect.equals(content, dataUri.contentAsString(encoding: ASCII));
34
35 uri = dataUri.toUri();
36 Expect.equals(uri.toString(), dataUri.toString());
37 Expect.equals(dataUri.toString(), new DataUri.fromUri(uri).toString());
38 }
39
40 void testUtf8Encoding(String content) {
41 DataUri uri = new DataUri.fromString(content);
42 Expect.equals(content, uri.contentAsString(encoding: UTF8));
43 Expect.listEquals(UTF8.encode(content), uri.contentAsBytes());
44 }
45
46 void testInvalidCharacters() {
47 // SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim)
48 var invalid =
49 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f'
50 ' ()<>@,;:"/[]?=%#';
51 var invalidNoSlash = invalid.replaceAll('/', '');
52 var dataUri = new DataUri.fromString(
53 invalid,
54 mimeType: "$invalidNoSlash/$invalidNoSlash",
55 parameters: DataUriParameter.fromMap({invalid: invalid}));
56
57 Expect.equals(invalid, dataUri.contentAsString());
58 Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType);
59 Expect.equals(invalid, dataUri.parameters.first.key);
60 Expect.equals(invalid, dataUri.parameters.first.value);
61
62 var uri = dataUri.toUri();
63 Expect.equals("$uri", "$dataUri");
64 Expect.equals("$dataUri", new DataUri.fromUri(uri).toString());
65 }
66
67 void testBytes() {
68 void testList(List<int> list) {
69 var dataUri = new DataUri.fromBytes(list);
70 Expect.equals("application/octet-stream", dataUri.mimeType);
71 Expect.listEquals(list, dataUri.contentAsBytes());
72 }
73
74 void testLists(List<int> list) {
75 testList(list);
76 for (int i = 0; i < 27; i++) {
77 testList(list.sublist(i, i + i)); // All lengths from 0 to 27.
78 }
79 }
80
81 var bytes = new Uint8List(512);
82 for (int i = 0; i < bytes.length; i++) {
83 bytes[i] = i;
nweiz 2015/10/15 21:09:04 Won't the latter half of this list be outside the
Lasse Reichstein Nielsen 2015/11/03 18:02:52 Storing into an Uint8List will truncate, so I get
84 }
85 testLists(bytes);
86 testLists(new List.from(bytes));
87 testLists(new List.unmodifiable(bytes));
88 }
89
90 bool badArgument(e) => e is ArgumentError;
91 bool badFormat(e) => e is FormatException;
92
93 void testErrors() {
94 // Invalid constructor parameters.
95 Expect.throws(() { new DataUri.fromBytes([], mimeType: "noslash"); },
96 badArgument);
97 Expect.throws(() { new DataUri.fromBytes([257]); },
98 badArgument);
99 Expect.throws(() { new DataUri.fromBytes([-1]); },
100 badArgument);
101 Expect.throws(() { new DataUri.fromBytes([0x100000000]); },
102 badArgument);
103 Expect.throws(() { new DataUri.fromString("", mimeType: "noslash"); },
104 badArgument);
105
106 // Empty parameters allowed, not an error.
107 var uri = new DataUri.fromString("", mimeType: "", parameters: []);
108 Expect.equals("data:,", uri.text);
109
110 // Parse format.
111 Expect.throws(() { DataUri.parse("notdata:,");}, badFormat);
112 Expect.throws(() { DataUri.parse("text/plain,noscheme");}, badFormat);
113 Expect.throws(() { DataUri.parse("data:noseparator");}, badFormat);
114 Expect.throws(() { DataUri.parse("data:noslash,text");}, badFormat);
115 Expect.throws(() { DataUri.parse("data:type/sub;noequals,text");}, badFormat);
116 Expect.throws(() { DataUri.parse("data:type/sub;knocomma=");}, badFormat);
117 Expect.throws(() { DataUri.parse("data:type/sub;k=v;nocomma");}, badFormat);
118 Expect.throws(() { DataUri.parse("data:type/sub;k=nocomma");}, badFormat);
119 Expect.throws(() { DataUri.parse("data:type/sub;k=v;base64");}, badFormat);
120
121 uri = DataUri.parse("data:,"); // Minimal valid.
122 Expect.equals("data:,", uri.text);
123 uri = DataUri.parse("data:;base64,"); // Minimal valid base64.
124 Expect.equals("data:;base64,", uri.text);
nweiz 2015/10/15 21:09:04 Why are these in testErrors?
Lasse Reichstein Nielsen 2015/11/03 18:02:52 Mostly as sanity checks. I'll move them to main a
125
126 // Invalid base64 format (only detected when decodeing).
127 uri = DataUri.parse("data:;base64,AAA");
128 Expect.throws(uri.contentAsBytes, badFormat);
129 uri = DataUri.parse("data:;base64,AA=");
130 Expect.throws(uri.contentAsBytes, badFormat);
131
132 // Accepts encoded '=' in base64.
133 uri = DataUri.parse("data:;base64,AA==");
134 Expect.listEquals([0], uri.contentAsBytes());
135 uri = DataUri.parse("data:;base64,AA%3D%3D");
136 Expect.listEquals([0], uri.contentAsBytes());
nweiz 2015/10/15 21:09:04 Also these?
Lasse Reichstein Nielsen 2015/11/03 18:02:52 That's really base-64 decoding now, so I'll just r
137 }
OLDNEW
« sdk/lib/core/uri.dart ('K') | « sdk/lib/core/uri.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698