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

Side by Side Diff: tests/lib/uri/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: Add more tests, refactor, rename to DataUriHelper. Need better name! Created 5 years, 1 month 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
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 "dart:uri";
6 import "package:expect/expect.dart";
7 import "dart:convert";
8 import "dart:typed_data";
9
10 main() {
11 testMediaType();
12
13 testRoundTrip("");
14 testRoundTrip("a");
15 testRoundTrip("ab");
16 testRoundTrip("abc");
17 testRoundTrip("abcd");
18 testRoundTrip("Content with special%25 characters: # ? = % # ? = %");
19 testRoundTrip("blåbærgrød", UTF8);
20 testRoundTrip("blåbærgrød", LATIN1);
21
22 testUtf8Encoding("\u1000\uffff");
23 testBytes();
24 testInvalidCharacters();
25 testErrors();
26 }
27
28 void testMediaType() {
29 for (var mimeType in ["", "text/plain", "text/javascript"]) {
30 for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) {
31 for (var base64 in ["", ";base64"]) {
32 bool isBase64 = base64.isNotEmpty;
33 var text = "data:$mimeType$charset$base64,";
34 var uri = DataUriHelper.parse(text);
35
36 String expectedCharset =
37 charset.isEmpty ? "US-ASCII" : charset.substring(9);
38 String expectedMimeType =
39 mimeType.isEmpty ? "text/plain" : mimeType;
40
41 Expect.equals(text, "$uri");
42 Expect.equals(expectedMimeType, uri.mimeType);
43 Expect.equals(expectedCharset, uri.charset);
44 Expect.equals(isBase64, uri.isBase64);
45 }
46 }
47 }
48 }
49
50 void testRoundTrip(String content, [Encoding encoding]) {
51 DataUriHelper dataUri =
52 new DataUriHelper.fromString(content, encoding: encoding);
53 Expect.isFalse(dataUri.isBase64);
54
55 if (encoding != null) {
56 DataUriHelper dataUriParams = new DataUriHelper.fromString(
57 content, parameters: {"charset" : encoding.name});
58 Expect.equals("$dataUri", "$dataUriParams");
59 }
60
61 Expect.equals(encoding ?? ASCII, Encoding.getByName(dataUri.charset));
62
63 Expect.equals(content, dataUri.contentAsString(encoding: encoding));
64 Expect.equals(content, dataUri.contentAsString());
65 Expect.equals(content, (encoding ?? ASCII).decode(dataUri.contentAsBytes()));
66
67 Uri uri = dataUri.toUri();
68 Expect.equals(uri.toString(), dataUri.toString());
69 Expect.equals(dataUri.toString(), new DataUriHelper.fromUri(uri).toString());
70
71 dataUri = new DataUriHelper.fromBytes(content.codeUnits);
72 Expect.listEquals(content.codeUnits, dataUri.contentAsBytes());
73 Expect.equals(content, dataUri.contentAsString(encoding: LATIN1));
74
75 uri = dataUri.toUri();
76 Expect.equals(uri.toString(), dataUri.toString());
77 Expect.equals(dataUri.toString(), new DataUriHelper.fromUri(uri).toString());
78 // Check that the URI is properly normalized.
79 expectUriEquals(uri, Uri.parse("$uri"));
80 }
81
82 void testUtf8Encoding(String content) {
83 DataUriHelper uri = new DataUriHelper.fromString(content, encoding: UTF8);
84 Expect.equals(content, uri.contentAsString(encoding: UTF8));
85 Expect.listEquals(UTF8.encode(content), uri.contentAsBytes());
86 }
87
88 void testInvalidCharacters() {
89 // SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim)
90 // This contains all ASCII character that are not valid in attribute/value
91 // parts.
92 var invalid =
93 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f'
94 ' ()<>@,;:"/[]?=%#\x80\u{1000}\u{10000}';
95 var invalidNoSlash = invalid.replaceAll('/', '');
96 var dataUri = new DataUriHelper.fromString(
97 invalid,
98 encoding: UTF8,
99 mimeType: "$invalidNoSlash/$invalidNoSlash",
100 parameters: {invalid: invalid});
101
102 Expect.equals(invalid, dataUri.contentAsString());
103 Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType);
104 Expect.equals(invalid, dataUri.parameters[invalid]);
105
106 var uri = dataUri.toUri();
107 Expect.equals("$uri", "$dataUri");
108 expectUriEquals(uri, Uri.parse("$uri")); // Check that it's canonicalized.
109 Expect.equals("$dataUri", new DataUriHelper.fromUri(uri).toString());
110 }
111
112 void testBytes() {
113 void testList(List<int> list) {
114 var dataUri = new DataUriHelper.fromBytes(list);
115 Expect.equals("application/octet-stream", dataUri.mimeType);
116 Expect.isTrue(dataUri.isBase64);
117 Expect.listEquals(list, dataUri.contentAsBytes());
118
119 dataUri = new DataUriHelper.fromBytes(list, percentEncoded: true);
120 Expect.equals("application/octet-stream", dataUri.mimeType);
121 Expect.isFalse(dataUri.isBase64);
122 Expect.listEquals(list, dataUri.contentAsBytes());
123
124 var string = new String.fromCharCodes(list);
125
126 dataUri = new DataUriHelper.fromString(string, encoding: LATIN1);
127 Expect.equals("text/plain", dataUri.mimeType);
128 Expect.isFalse(dataUri.isBase64);
129 Expect.listEquals(list, dataUri.contentAsBytes());
130
131 dataUri =
132 new DataUriHelper.fromString(string, encoding: LATIN1, base64: true);
133 Expect.equals("text/plain", dataUri.mimeType);
134 Expect.isTrue(dataUri.isBase64);
135 Expect.listEquals(list, dataUri.contentAsBytes());
136 }
137
138 void testLists(List<int> list) {
139 testList(list);
140 for (int i = 0; i < 27; i++) {
141 testList(list.sublist(i, i + i)); // All lengths from 0 to 27.
142 }
143 }
144
145 var bytes = new Uint8List(512);
146 for (int i = 0; i < bytes.length; i++) {
147 bytes[i] = i;
148 }
149 testLists(bytes);
150 testLists(new List.from(bytes));
151 testLists(new List.unmodifiable(bytes));
152 }
153
154 bool badArgument(e) => e is ArgumentError;
155 bool badFormat(e) => e is FormatException;
156
157 void testErrors() {
158 // Invalid constructor parameters.
159 Expect.throws(() { new DataUriHelper.fromBytes([], mimeType: "noslash"); },
160 badArgument);
161 Expect.throws(() { new DataUriHelper.fromBytes([257]); },
162 badArgument);
163 Expect.throws(() { new DataUriHelper.fromBytes([-1]); },
164 badArgument);
165 Expect.throws(() { new DataUriHelper.fromBytes([0x100000000]); },
166 badArgument);
167 Expect.throws(() { new DataUriHelper.fromString("", mimeType: "noslash"); },
168 badArgument);
169
170 // Empty parameters allowed, not an error.
171 var uri = new DataUriHelper.fromString("", mimeType: "", parameters: {});
172 Expect.equals("data:,", "$uri");
173 // Empty parameter key or value is an error.
174 Expect.throws(() => new DataUriHelper.fromString("", parameters: {"": "X"}),
175 badArgument);
176 Expect.throws(() => new DataUriHelper.fromString("", parameters: {"X": ""}),
177 badArgument);
178
179 // Not recognizing charset is an error.
180 uri = DataUriHelper.parse("data:;charset=arglebargle,X");
181 Expect.throws(() {
182 uri.contentAsString();
183 });
184 // Doesn't throw if we specify the encoding.
185 Expect.equals("X", uri.contentAsString(encoding: ASCII));
186
187 // Parse format.
188 Expect.throws(() { DataUriHelper.parse("notdata:,");}, badFormat);
189 Expect.throws(() { DataUriHelper.parse("text/plain,noscheme");}, badFormat);
190 Expect.throws(() { DataUriHelper.parse("data:noseparator");}, badFormat);
191 Expect.throws(() { DataUriHelper.parse("data:noslash,text");}, badFormat);
192 Expect.throws(() { DataUriHelper.parse("data:type/sub;noequals,text");},
193 badFormat);
194 Expect.throws(() { DataUriHelper.parse("data:type/sub;knocomma=");},
195 badFormat);
196 Expect.throws(() { DataUriHelper.parse("data:type/sub;k=v;nocomma");},
197 badFormat);
198 Expect.throws(() { DataUriHelper.parse("data:type/sub;k=nocomma");},
199 badFormat);
200 Expect.throws(() { DataUriHelper.parse("data:type/sub;k=v;base64");},
201 badFormat);
202
203 // Invalid base64 format (only detected when decodeing).
204 for (var a = 0; a <= 4; a++) {
205 for (var p = 0; p <= 4; p++) {
206 // Base-64 encoding must have length divisible by four and no more
207 // than two padding characters at the end.
208 if (p < 3 && (a + p) % 4 == 0) continue;
209 uri = DataUriHelper.parse("data:;base64," + "A" * a + "=" * p);
210 Expect.throws(uri.contentAsBytes, badFormat);
211 }
212 }
213 // Invalid base64 encoding: padding not at end.
214 uri = DataUriHelper.parse("data:;base64,AA=A");
215 Expect.throws(uri.contentAsBytes, badFormat);
216 uri = DataUriHelper.parse("data:;base64,A=AA");
217 Expect.throws(uri.contentAsBytes, badFormat);
218 uri = DataUriHelper.parse("data:;base64,=AAA");
219 Expect.throws(uri.contentAsBytes, badFormat);
220 uri = DataUriHelper.parse("data:;base64,A==A");
221 Expect.throws(uri.contentAsBytes, badFormat);
222 uri = DataUriHelper.parse("data:;base64,==AA");
223 Expect.throws(uri.contentAsBytes, badFormat);
224 uri = DataUriHelper.parse("data:;base64,===A");
225 Expect.throws(uri.contentAsBytes, badFormat);
226 }
227
228 /// Checks that two [Uri]s are exactly the same.
229 expectUriEquals(Uri expect, Uri actual) {
230 Expect.equals(expect.scheme, actual.scheme, "scheme");
231 Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority");
232 Expect.equals(expect.userInfo, actual.userInfo, "userInfo");
233 Expect.equals(expect.host, actual.host, "host");
234 Expect.equals(expect.hasPort, actual.hasPort, "hasPort");
235 Expect.equals(expect.port, actual.port, "port");
236 Expect.equals(expect.port, actual.port, "port");
237 Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery");
238 Expect.equals(expect.query, actual.query, "query");
239 Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment");
240 Expect.equals(expect.fragment, actual.fragment, "fragment");
241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698