OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import "dart:convert"; | 6 import "dart:convert"; |
7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
8 | 8 |
9 main() { | 9 main() { |
10 testMediaType(); | 10 testMediaType(); |
11 | 11 |
12 testRoundTrip(""); | 12 testRoundTrip(""); |
13 testRoundTrip("a"); | 13 testRoundTrip("a"); |
14 testRoundTrip("ab"); | 14 testRoundTrip("ab"); |
15 testRoundTrip("abc"); | 15 testRoundTrip("abc"); |
16 testRoundTrip("abcd"); | 16 testRoundTrip("abcd"); |
17 testRoundTrip("Content with special%25 characters: # ? = % # ? = %"); | 17 testRoundTrip("Content with special%25 characters: # ? = % # ? = %"); |
18 testRoundTrip("blåbærgrød", UTF8); | 18 testRoundTrip("blåbærgrød", UTF8); |
19 testRoundTrip("blåbærgrød", LATIN1); | 19 testRoundTrip("blåbærgrød", LATIN1); |
20 | 20 |
21 testUriEquals("data:,abc?d"); | 21 testUriEquals("data:,abc?d"); |
22 testUriEquals("DATA:,ABC?D"); | 22 testUriEquals("DATA:,ABC?D"); |
23 testUriEquals("data:,a%20bc?d"); | 23 testUriEquals("data:,a%20bc?d"); |
24 testUriEquals("DATA:,A%20BC?D"); | 24 testUriEquals("DATA:,A%20BC?D"); |
25 testUriEquals("data:,abc?d%23e"); // # must and will be is escaped. | 25 testUriEquals("data:,abc?d%23e"); // # must and will be is escaped. |
26 | 26 |
27 // Test that UriData.uri normalizes path and query. | 27 // Test that UriData.uri normalizes path and query. |
28 | 28 |
29 testUtf8Encoding("\u1000\uffff"); | 29 testUtf8Encoding("\u1000\uffff"); |
30 testBytes(); | 30 testBytes(); |
31 testInvalidCharacters(); | 31 testInvalidCharacters(); |
32 testNormalization(); | 32 testNormalization(); |
33 testErrors(); | 33 testErrors(); |
34 } | 34 } |
35 | 35 |
36 void testMediaType() { | 36 void testMediaType() { |
37 for (var mimeType in ["", "text/plain", "text/javascript"]) { | 37 for (var mimeType in ["", "text/plain", "text/javascript"]) { |
38 for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) { | 38 for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) { |
39 for (var base64 in ["", ";base64"]) { | 39 for (var base64 in ["", ";base64"]) { |
40 bool isBase64 = base64.isNotEmpty; | 40 bool isBase64 = base64.isNotEmpty; |
41 var text = "data:$mimeType$charset$base64,"; | 41 var text = "data:$mimeType$charset$base64,"; |
42 var uri = UriData.parse(text); | 42 var uri = UriData.parse(text); |
43 | 43 |
44 String expectedCharset = | 44 String expectedCharset = |
45 charset.isEmpty ? "US-ASCII" : charset.substring(9); | 45 charset.isEmpty ? "US-ASCII" : charset.substring(9); |
46 String expectedMimeType = | 46 String expectedMimeType = mimeType.isEmpty ? "text/plain" : mimeType; |
47 mimeType.isEmpty ? "text/plain" : mimeType; | |
48 | 47 |
49 Expect.equals(text, "$uri"); | 48 Expect.equals(text, "$uri"); |
50 Expect.equals(expectedMimeType, uri.mimeType); | 49 Expect.equals(expectedMimeType, uri.mimeType); |
51 Expect.equals(expectedCharset, uri.charset); | 50 Expect.equals(expectedCharset, uri.charset); |
52 Expect.equals(isBase64, uri.isBase64); | 51 Expect.equals(isBase64, uri.isBase64); |
53 } | 52 } |
54 } | 53 } |
55 } | 54 } |
56 } | 55 } |
57 | 56 |
58 void testRoundTrip(String content, [Encoding encoding]) { | 57 void testRoundTrip(String content, [Encoding encoding]) { |
59 UriData dataUri = | 58 UriData dataUri = new UriData.fromString(content, encoding: encoding); |
60 new UriData.fromString(content, encoding: encoding); | |
61 Expect.isFalse(dataUri.isBase64); | 59 Expect.isFalse(dataUri.isBase64); |
62 Uri uri = dataUri.uri; | 60 Uri uri = dataUri.uri; |
63 expectUriEquals(new Uri.dataFromString(content, encoding: encoding), uri); | 61 expectUriEquals(new Uri.dataFromString(content, encoding: encoding), uri); |
64 | 62 |
65 if (encoding != null) { | 63 if (encoding != null) { |
66 UriData dataUriParams = new UriData.fromString( | 64 UriData dataUriParams = |
67 content, parameters: {"charset" : encoding.name}); | 65 new UriData.fromString(content, parameters: {"charset": encoding.name}); |
68 Expect.equals("$dataUri", "$dataUriParams"); | 66 Expect.equals("$dataUri", "$dataUriParams"); |
69 } | 67 } |
70 | 68 |
71 Expect.equals(encoding ?? ASCII, Encoding.getByName(dataUri.charset)); | 69 Expect.equals(encoding ?? ASCII, Encoding.getByName(dataUri.charset)); |
72 Expect.equals(content, dataUri.contentAsString(encoding: encoding)); | 70 Expect.equals(content, dataUri.contentAsString(encoding: encoding)); |
73 Expect.equals(content, dataUri.contentAsString()); | 71 Expect.equals(content, dataUri.contentAsString()); |
74 Expect.equals(content, (encoding ?? ASCII).decode(dataUri.contentAsBytes())); | 72 Expect.equals(content, (encoding ?? ASCII).decode(dataUri.contentAsBytes())); |
75 | 73 |
76 uri = dataUri.uri; | 74 uri = dataUri.uri; |
77 Expect.equals(uri.toString(), dataUri.toString()); | 75 Expect.equals(uri.toString(), dataUri.toString()); |
(...skipping 14 matching lines...) Expand all Loading... |
92 UriData uri = new UriData.fromString(content, encoding: UTF8); | 90 UriData uri = new UriData.fromString(content, encoding: UTF8); |
93 Expect.equals(content, uri.contentAsString(encoding: UTF8)); | 91 Expect.equals(content, uri.contentAsString(encoding: UTF8)); |
94 Expect.listEquals(UTF8.encode(content), uri.contentAsBytes()); | 92 Expect.listEquals(UTF8.encode(content), uri.contentAsBytes()); |
95 } | 93 } |
96 | 94 |
97 void testInvalidCharacters() { | 95 void testInvalidCharacters() { |
98 // SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim) | 96 // SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim) |
99 // This contains all ASCII character that are not valid in attribute/value | 97 // This contains all ASCII character that are not valid in attribute/value |
100 // parts. | 98 // parts. |
101 var invalid = | 99 var invalid = |
102 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f' | 100 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f' |
103 ' ()<>@,;:"/[]?=%#\x80\u{1000}\u{10000}'; | 101 ' ()<>@,;:"/[]?=%#\x80\u{1000}\u{10000}'; |
104 var invalidNoSlash = invalid.replaceAll('/', ''); | 102 var invalidNoSlash = invalid.replaceAll('/', ''); |
105 var dataUri = new UriData.fromString( | 103 var dataUri = new UriData.fromString(invalid, |
106 invalid, | |
107 encoding: UTF8, | 104 encoding: UTF8, |
108 mimeType: "$invalidNoSlash/$invalidNoSlash", | 105 mimeType: "$invalidNoSlash/$invalidNoSlash", |
109 parameters: {invalid: invalid}); | 106 parameters: {invalid: invalid}); |
110 | 107 |
111 Expect.equals(invalid, dataUri.contentAsString()); | 108 Expect.equals(invalid, dataUri.contentAsString()); |
112 Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType); | 109 Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType); |
113 Expect.equals(invalid, dataUri.parameters[invalid]); | 110 Expect.equals(invalid, dataUri.parameters[invalid]); |
114 | 111 |
115 var uri = dataUri.uri; | 112 var uri = dataUri.uri; |
116 Expect.equals("$uri", "$dataUri"); | 113 Expect.equals("$uri", "$dataUri"); |
117 expectUriEquals(uri, Uri.parse("$uri")); // Check that it's canonicalized. | 114 expectUriEquals(uri, Uri.parse("$uri")); // Check that it's canonicalized. |
118 Expect.equals("$dataUri", new UriData.fromUri(uri).toString()); | 115 Expect.equals("$dataUri", new UriData.fromUri(uri).toString()); |
119 } | 116 } |
120 | 117 |
121 void testBytes() { | 118 void testBytes() { |
122 void testList(List<int> list) { | 119 void testList(List<int> list) { |
123 var dataUri = new UriData.fromBytes(list); | 120 var dataUri = new UriData.fromBytes(list); |
124 Expect.equals("application/octet-stream", dataUri.mimeType); | 121 Expect.equals("application/octet-stream", dataUri.mimeType); |
125 Expect.isTrue(dataUri.isBase64); | 122 Expect.isTrue(dataUri.isBase64); |
126 Expect.listEquals(list, dataUri.contentAsBytes()); | 123 Expect.listEquals(list, dataUri.contentAsBytes()); |
127 | 124 |
128 dataUri = new UriData.fromBytes(list, percentEncoded: true); | 125 dataUri = new UriData.fromBytes(list, percentEncoded: true); |
129 Expect.equals("application/octet-stream", dataUri.mimeType); | 126 Expect.equals("application/octet-stream", dataUri.mimeType); |
130 Expect.isFalse(dataUri.isBase64); | 127 Expect.isFalse(dataUri.isBase64); |
131 Expect.listEquals(list, dataUri.contentAsBytes()); | 128 Expect.listEquals(list, dataUri.contentAsBytes()); |
132 | 129 |
133 var string = new String.fromCharCodes(list); | 130 var string = new String.fromCharCodes(list); |
134 | 131 |
135 dataUri = new UriData.fromString(string, encoding: LATIN1); | 132 dataUri = new UriData.fromString(string, encoding: LATIN1); |
136 Expect.equals("text/plain", dataUri.mimeType); | 133 Expect.equals("text/plain", dataUri.mimeType); |
137 Expect.isFalse(dataUri.isBase64); | 134 Expect.isFalse(dataUri.isBase64); |
138 Expect.listEquals(list, dataUri.contentAsBytes()); | 135 Expect.listEquals(list, dataUri.contentAsBytes()); |
139 | 136 |
140 dataUri = | 137 dataUri = new UriData.fromString(string, encoding: LATIN1, base64: true); |
141 new UriData.fromString(string, encoding: LATIN1, base64: true); | |
142 Expect.equals("text/plain", dataUri.mimeType); | 138 Expect.equals("text/plain", dataUri.mimeType); |
143 Expect.isTrue(dataUri.isBase64); | 139 Expect.isTrue(dataUri.isBase64); |
144 Expect.listEquals(list, dataUri.contentAsBytes()); | 140 Expect.listEquals(list, dataUri.contentAsBytes()); |
145 } | 141 } |
146 | 142 |
147 void testLists(List<int> list) { | 143 void testLists(List<int> list) { |
148 testList(list); | 144 testList(list); |
149 for (int i = 0; i < 27; i++) { | 145 for (int i = 0; i < 27; i++) { |
150 testList(list.sublist(i, i + i)); // All lengths from 0 to 27. | 146 testList(list.sublist(i, i + i)); // All lengths from 0 to 27. |
151 } | 147 } |
152 } | 148 } |
153 | 149 |
154 var bytes = new Uint8List(512); | 150 var bytes = new Uint8List(512); |
155 for (int i = 0; i < bytes.length; i++) { | 151 for (int i = 0; i < bytes.length; i++) { |
156 bytes[i] = i; | 152 bytes[i] = i; |
157 } | 153 } |
158 testLists(bytes); | 154 testLists(bytes); |
159 testLists(new List.from(bytes)); | 155 testLists(new List.from(bytes)); |
160 testLists(new List.unmodifiable(bytes)); | 156 testLists(new List.unmodifiable(bytes)); |
161 } | 157 } |
162 | 158 |
163 void testNormalization() { | 159 void testNormalization() { |
164 // Base-64 normalization. | 160 // Base-64 normalization. |
165 | 161 |
166 // Normalized URI-alphabet characters. | 162 // Normalized URI-alphabet characters. |
167 Expect.equals("data:;base64,AA/+", | 163 Expect.equals( |
168 UriData.parse("data:;base64,AA_-").toString()); | 164 "data:;base64,AA/+", UriData.parse("data:;base64,AA_-").toString()); |
169 // Normalized escapes. | 165 // Normalized escapes. |
170 Expect.equals("data:;base64,AB==", | 166 Expect.equals( |
171 UriData.parse("data:;base64,A%42=%3D").toString()); | 167 "data:;base64,AB==", UriData.parse("data:;base64,A%42=%3D").toString()); |
172 Expect.equals("data:;base64,/+/+", | 168 Expect.equals("data:;base64,/+/+", |
173 UriData.parse("data:;base64,%5F%2D%2F%2B").toString()); | 169 UriData.parse("data:;base64,%5F%2D%2F%2B").toString()); |
174 // Normalized padded data. | 170 // Normalized padded data. |
175 Expect.equals("data:;base64,AA==", | 171 Expect.equals( |
176 UriData.parse("data:;base64,AA%3D%3D").toString()); | 172 "data:;base64,AA==", UriData.parse("data:;base64,AA%3D%3D").toString()); |
177 Expect.equals("data:;base64,AAA=", | 173 Expect.equals( |
178 UriData.parse("data:;base64,AAA%3D").toString()); | 174 "data:;base64,AAA=", UriData.parse("data:;base64,AAA%3D").toString()); |
179 // Normalized unpadded data. | 175 // Normalized unpadded data. |
180 Expect.equals("data:;base64,AA==", | 176 Expect.equals( |
181 UriData.parse("data:;base64,AA").toString()); | 177 "data:;base64,AA==", UriData.parse("data:;base64,AA").toString()); |
182 Expect.equals("data:;base64,AAA=", | 178 Expect.equals( |
183 UriData.parse("data:;base64,AAA").toString()); | 179 "data:;base64,AAA=", UriData.parse("data:;base64,AAA").toString()); |
184 | 180 |
185 // "URI normalization" of non-base64 content. | 181 // "URI normalization" of non-base64 content. |
186 var uri = UriData.parse("data:,\x20\xa0"); | 182 var uri = UriData.parse("data:,\x20\xa0"); |
187 Expect.equals("data:,%20%C2%A0", uri.toString()); | 183 Expect.equals("data:,%20%C2%A0", uri.toString()); |
188 uri = UriData.parse("data:,x://x@y:[z]:42/p/./?q=x&y=z#?#\u1234\u{12345}"); | 184 uri = UriData.parse("data:,x://x@y:[z]:42/p/./?q=x&y=z#?#\u1234\u{12345}"); |
189 Expect.equals( | 185 Expect.equals( |
190 "data:,x://x@y:%5Bz%5D:42/p/./?q=x&y=z%23?%23%E1%88%B4%F0%92%8D%85", | 186 "data:,x://x@y:%5Bz%5D:42/p/./?q=x&y=z%23?%23%E1%88%B4%F0%92%8D%85", |
191 uri.toString()); | 187 uri.toString()); |
192 } | 188 } |
193 | 189 |
194 bool badArgument(e) => e is ArgumentError; | 190 bool badArgument(e) => e is ArgumentError; |
195 bool badFormat(e) => e is FormatException; | 191 bool badFormat(e) => e is FormatException; |
196 | 192 |
197 void testErrors() { | 193 void testErrors() { |
198 // Invalid constructor parameters. | 194 // Invalid constructor parameters. |
199 Expect.throws(() { new UriData.fromBytes([], mimeType: "noslash"); }, | 195 Expect.throws(() { |
200 badArgument); | 196 new UriData.fromBytes([], mimeType: "noslash"); |
201 Expect.throws(() { new UriData.fromBytes([257]); }, | 197 }, badArgument); |
202 badArgument); | 198 Expect.throws(() { |
203 Expect.throws(() { new UriData.fromBytes([-1]); }, | 199 new UriData.fromBytes([257]); |
204 badArgument); | 200 }, badArgument); |
205 Expect.throws(() { new UriData.fromBytes([0x10000000]); }, | 201 Expect.throws(() { |
206 badArgument); | 202 new UriData.fromBytes([-1]); |
207 Expect.throws(() { new UriData.fromString("", mimeType: "noslash"); }, | 203 }, badArgument); |
208 badArgument); | 204 Expect.throws(() { |
| 205 new UriData.fromBytes([0x10000000]); |
| 206 }, badArgument); |
| 207 Expect.throws(() { |
| 208 new UriData.fromString("", mimeType: "noslash"); |
| 209 }, badArgument); |
209 | 210 |
210 Expect.throws(() { new Uri.dataFromBytes([], mimeType: "noslash"); }, | 211 Expect.throws(() { |
211 badArgument); | 212 new Uri.dataFromBytes([], mimeType: "noslash"); |
212 Expect.throws(() { new Uri.dataFromBytes([257]); }, | 213 }, badArgument); |
213 badArgument); | 214 Expect.throws(() { |
214 Expect.throws(() { new Uri.dataFromBytes([-1]); }, | 215 new Uri.dataFromBytes([257]); |
215 badArgument); | 216 }, badArgument); |
216 Expect.throws(() { new Uri.dataFromBytes([0x10000000]); }, | 217 Expect.throws(() { |
217 badArgument); | 218 new Uri.dataFromBytes([-1]); |
218 Expect.throws(() { new Uri.dataFromString("", mimeType: "noslash"); }, | 219 }, badArgument); |
219 badArgument); | 220 Expect.throws(() { |
| 221 new Uri.dataFromBytes([0x10000000]); |
| 222 }, badArgument); |
| 223 Expect.throws(() { |
| 224 new Uri.dataFromString("", mimeType: "noslash"); |
| 225 }, badArgument); |
220 | 226 |
221 // Empty parameters allowed, not an error. | 227 // Empty parameters allowed, not an error. |
222 var uri = new UriData.fromString("", mimeType: "", parameters: {}); | 228 var uri = new UriData.fromString("", mimeType: "", parameters: {}); |
223 Expect.equals("data:,", "$uri"); | 229 Expect.equals("data:,", "$uri"); |
224 // Empty parameter key or value is an error. | 230 // Empty parameter key or value is an error. |
225 Expect.throws(() => new UriData.fromString("", parameters: {"": "X"}), | 231 Expect.throws( |
226 badArgument); | 232 () => new UriData.fromString("", parameters: {"": "X"}), badArgument); |
227 Expect.throws(() => new UriData.fromString("", parameters: {"X": ""}), | 233 Expect.throws( |
228 badArgument); | 234 () => new UriData.fromString("", parameters: {"X": ""}), badArgument); |
229 | 235 |
230 // Not recognizing charset is an error. | 236 // Not recognizing charset is an error. |
231 uri = UriData.parse("data:;charset=arglebargle,X"); | 237 uri = UriData.parse("data:;charset=arglebargle,X"); |
232 Expect.throws(() { | 238 Expect.throws(() { |
233 uri.contentAsString(); | 239 uri.contentAsString(); |
234 }); | 240 }); |
235 // Doesn't throw if we specify the encoding. | 241 // Doesn't throw if we specify the encoding. |
236 Expect.equals("X", uri.contentAsString(encoding: ASCII)); | 242 Expect.equals("X", uri.contentAsString(encoding: ASCII)); |
237 | 243 |
238 // Parse format. | 244 // Parse format. |
239 Expect.throws(() { UriData.parse("notdata:,");}, badFormat); | 245 Expect.throws(() { |
240 Expect.throws(() { UriData.parse("text/plain,noscheme");}, badFormat); | 246 UriData.parse("notdata:,"); |
241 Expect.throws(() { UriData.parse("data:noseparator");}, badFormat); | 247 }, badFormat); |
242 Expect.throws(() { UriData.parse("data:noslash,text");}, badFormat); | 248 Expect.throws(() { |
243 Expect.throws(() { UriData.parse("data:type/sub;noequals,text");}, | 249 UriData.parse("text/plain,noscheme"); |
244 badFormat); | 250 }, badFormat); |
245 Expect.throws(() { UriData.parse("data:type/sub;knocomma=");}, | 251 Expect.throws(() { |
246 badFormat); | 252 UriData.parse("data:noseparator"); |
247 Expect.throws(() { UriData.parse("data:type/sub;k=v;nocomma");}, | 253 }, badFormat); |
248 badFormat); | 254 Expect.throws(() { |
249 Expect.throws(() { UriData.parse("data:type/sub;k=nocomma");}, | 255 UriData.parse("data:noslash,text"); |
250 badFormat); | 256 }, badFormat); |
251 Expect.throws(() { UriData.parse("data:type/sub;k=v;base64");}, | 257 Expect.throws(() { |
252 badFormat); | 258 UriData.parse("data:type/sub;noequals,text"); |
| 259 }, badFormat); |
| 260 Expect.throws(() { |
| 261 UriData.parse("data:type/sub;knocomma="); |
| 262 }, badFormat); |
| 263 Expect.throws(() { |
| 264 UriData.parse("data:type/sub;k=v;nocomma"); |
| 265 }, badFormat); |
| 266 Expect.throws(() { |
| 267 UriData.parse("data:type/sub;k=nocomma"); |
| 268 }, badFormat); |
| 269 Expect.throws(() { |
| 270 UriData.parse("data:type/sub;k=v;base64"); |
| 271 }, badFormat); |
253 | 272 |
254 void formatError(String input) { | 273 void formatError(String input) { |
255 Expect.throws(() => UriData.parse("data:;base64,$input"), badFormat, input); | 274 Expect.throws(() => UriData.parse("data:;base64,$input"), badFormat, input); |
256 } | 275 } |
257 | 276 |
258 // Invalid base64 format (detected when parsed). | 277 // Invalid base64 format (detected when parsed). |
259 for (var a = 0; a <= 4; a++) { | 278 for (var a = 0; a <= 4; a++) { |
260 for (var p = 0; p <= 4; p++) { | 279 for (var p = 0; p <= 4; p++) { |
261 // Base-64 encoding must have length divisible by four and no more | 280 // Base-64 encoding must have length divisible by four and no more |
262 // than two padding characters at the end. | 281 // than two padding characters at the end. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 Expect.equals(expect.fragment, actual.fragment, "fragment"); | 326 Expect.equals(expect.fragment, actual.fragment, "fragment"); |
308 } | 327 } |
309 | 328 |
310 void testUriEquals(String uriText) { | 329 void testUriEquals(String uriText) { |
311 var data = UriData.parse(uriText); | 330 var data = UriData.parse(uriText); |
312 var uri = Uri.parse(uriText); | 331 var uri = Uri.parse(uriText); |
313 Expect.equals(data.uri, uri); | 332 Expect.equals(data.uri, uri); |
314 Expect.equals(data.toString(), uri.data.toString()); | 333 Expect.equals(data.toString(), uri.data.toString()); |
315 Expect.equals(data.toString(), uri.toString()); | 334 Expect.equals(data.toString(), uri.toString()); |
316 } | 335 } |
OLD | NEW |