OLD | NEW |
| (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:convert'; | |
6 | |
7 import 'package:charcode/charcode.dart'; | |
8 import 'package:http_parser/http_parser.dart'; | |
9 import 'package:test/test.dart'; | |
10 | |
11 void main() { | |
12 group("encode", () { | |
13 test("base64-encodes data by default", () { | |
14 var uri = new DataUri.encode([1, 2, 3, 4]); | |
15 expect(uri.toString(), equals("data:;base64,AQIDBA==")); | |
16 }); | |
17 | |
18 test("doesn't use URL-safe base64 encoding", () { | |
19 var uri = new DataUri.encode([0xFB, 0xFF]); | |
20 expect(uri.toString(), equals("data:;base64,+/8=")); | |
21 }); | |
22 | |
23 test("percent-encodes data if base64 is disabled", () { | |
24 var uri = new DataUri.encode([$a, $B, $plus, $slash, 0xFF], | |
25 base64: false); | |
26 expect(uri.toString(), equals("data:,aB%2B%2F%FF")); | |
27 }); | |
28 | |
29 test("includes a media type and its parameters", () { | |
30 var mediaType = new MediaType('text', 'html', { | |
31 'foo': 'bar', | |
32 'baz': 'bang' | |
33 }); | |
34 var uri = new DataUri.encode([], mediaType: mediaType); | |
35 expect(uri.toString(), equals('data:text/html;foo=bar;baz=bang;base64,')); | |
36 }); | |
37 | |
38 test("percent-encodes the media type and its parameters", () { | |
39 var mediaType = new MediaType('te=xt', 'ht%ml', {'f;oo': 'ba,r'}); | |
40 var uri = new DataUri.encode([], mediaType: mediaType); | |
41 expect(uri.toString(), | |
42 equals('data:te%3Dxt/ht%25ml;f%3Boo=ba%2Cr;base64,')); | |
43 }); | |
44 | |
45 test("UTF-8 encodes non-ASCII characters", () { | |
46 var mediaType = new MediaType('tëxt', 'ћtml', {'føo': 'bår'}); | |
47 var uri = new DataUri.encode([], mediaType: mediaType); | |
48 expect(uri.toString(), | |
49 equals('data:t%C3%ABxt/%D1%9Btml;f%C3%B8o=b%C3%A5r;base64,')); | |
50 }); | |
51 | |
52 test("doesn't include a text/plain media type", () { | |
53 var mediaType = new MediaType('text', 'plain', {'foo': 'bar'}); | |
54 var uri = new DataUri.encode([], mediaType: mediaType); | |
55 expect(uri.toString(), equals('data:;foo=bar;base64,')); | |
56 }); | |
57 | |
58 group("with a string", () { | |
59 test("defaults to ASCII if it's sufficient", () { | |
60 var uri = new DataUri.encodeString('foo'); | |
61 expect(uri.toString(), equals("data:;base64,Zm9v")); | |
62 }); | |
63 | |
64 test("defaults to UTF-8 encoding if it's needed", () { | |
65 var uri = new DataUri.encodeString('føo'); | |
66 expect(uri.toString(), equals("data:;charset=utf-8;base64,ZsO4bw==")); | |
67 }); | |
68 | |
69 test("obeys a passed encoding", () { | |
70 var uri = new DataUri.encodeString('føo', encoding: LATIN1); | |
71 expect(uri.toString(), equals("data:;charset=iso-8859-1;base64,Zvhv")); | |
72 }); | |
73 | |
74 test("obeys a media type encoding", () { | |
75 var mediaType = new MediaType('text', 'plain', | |
76 {'charset': 'iso-8859-1'}); | |
77 var uri = new DataUri.encodeString('føo', mediaType: mediaType); | |
78 expect(uri.toString(), equals("data:;charset=iso-8859-1;base64,Zvhv")); | |
79 }); | |
80 | |
81 test("obeys a passed encoding that matches a media type encoding", () { | |
82 var mediaType = new MediaType('text', 'plain', | |
83 {'charset': 'iso-8859-1'}); | |
84 var uri = new DataUri.encodeString('føo', | |
85 encoding: LATIN1, mediaType: mediaType); | |
86 expect(uri.toString(), equals("data:;charset=iso-8859-1;base64,Zvhv")); | |
87 }); | |
88 | |
89 test("throws if a media type encoding is unsupported", () { | |
90 var mediaType = new MediaType('text', 'plain', {'charset': 'fblthp'}); | |
91 expect(() => new DataUri.encodeString('føo', mediaType: mediaType), | |
92 throwsUnsupportedError); | |
93 }); | |
94 | |
95 test("throws if a passed encoding disagrees with a media type encoding", | |
96 () { | |
97 var mediaType = new MediaType('text', 'plain', {'charset': 'utf-8'}); | |
98 expect(() { | |
99 new DataUri.encodeString('føo', | |
100 encoding: LATIN1, mediaType: mediaType); | |
101 }, throwsArgumentError); | |
102 }); | |
103 }); | |
104 }); | |
105 | |
106 group("decode", () { | |
107 test("decodes a base64 URI", () { | |
108 var uri = new DataUri.decode("data:;base64,AQIDBA=="); | |
109 expect(uri.data, equals([1, 2, 3, 4])); | |
110 }); | |
111 | |
112 test("decodes a percent-encoded URI", () { | |
113 var uri = new DataUri.decode("data:,aB%2B%2F%FF"); | |
114 expect(uri.data, equals([$a, $B, $plus, $slash, 0xFF])); | |
115 }); | |
116 | |
117 test("decodes a media type and its parameters", () { | |
118 var uri = new DataUri.decode("data:text/html;foo=bar;baz=bang;base64,"); | |
119 expect(uri.data, isEmpty); | |
120 expect(uri.mediaType.type, equals('text')); | |
121 expect(uri.mediaType.subtype, equals('html')); | |
122 expect(uri.mediaType.parameters, equals({ | |
123 'foo': 'bar', | |
124 'baz': 'bang' | |
125 })); | |
126 }); | |
127 | |
128 test("defaults to a text/plain media type", () { | |
129 var uri = new DataUri.decode("data:;base64,"); | |
130 expect(uri.mediaType.type, equals('text')); | |
131 expect(uri.mediaType.subtype, equals('plain')); | |
132 expect(uri.mediaType.parameters, equals({'charset': 'US-ASCII'})); | |
133 }); | |
134 | |
135 test("defaults to a text/plain media type with parameters", () { | |
136 var uri = new DataUri.decode("data:;foo=bar;base64,"); | |
137 expect(uri.mediaType.type, equals('text')); | |
138 expect(uri.mediaType.subtype, equals('plain')); | |
139 expect(uri.mediaType.parameters, equals({'foo': 'bar'})); | |
140 }); | |
141 | |
142 test("percent-decodes the media type and its parameters", () { | |
143 var uri = new DataUri.decode( | |
144 'data:te%78t/ht%6Dl;f%6Fo=ba%2Cr;base64,'); | |
145 expect(uri.mediaType.type, equals('text')); | |
146 expect(uri.mediaType.subtype, equals('html')); | |
147 expect(uri.mediaType.parameters, equals({'foo': 'ba,r'})); | |
148 }); | |
149 | |
150 test("assumes the URI is UTF-8", () { | |
151 var uri = new DataUri.decode( | |
152 'data:t%C3%ABxt/%D1%9Btml;f%C3%B8o=b%C3%A5r;base64,'); | |
153 expect(uri.mediaType.type, equals('tëxt')); | |
154 expect(uri.mediaType.subtype, equals('ћtml')); | |
155 expect(uri.mediaType.parameters, equals({'føo': 'bår'})); | |
156 }); | |
157 | |
158 test("allows a parameter named base64", () { | |
159 var uri = new DataUri.decode("data:;base64=no,foo"); | |
160 expect(uri.mediaType.parameters, equals({'base64': 'no'})); | |
161 expect(uri.data, equals([$f, $o, $o])); | |
162 }); | |
163 | |
164 test("includes the query", () { | |
165 var uri = new DataUri.decode("data:,a?b=c"); | |
166 expect(uri.data, equals([$a, $question, $b, $equal, $c])); | |
167 }); | |
168 | |
169 test("doesn't include the fragment", () { | |
170 var uri = new DataUri.decode("data:,a#b=c"); | |
171 expect(uri.data, equals([$a])); | |
172 }); | |
173 | |
174 test("supports the URL-safe base64 alphabet", () { | |
175 var uri = new DataUri.decode("data:;base64,-_8%3D"); | |
176 expect(uri.data, equals([0xFB, 0xFF])); | |
177 }); | |
178 | |
179 group("forbids", () { | |
180 test("a parameter with the wrong type", () { | |
181 expect(() => new DataUri.decode(12), throwsArgumentError); | |
182 }); | |
183 | |
184 test("a parameter with the wrong scheme", () { | |
185 expect(() => new DataUri.decode("http:;base64,"), throwsArgumentError); | |
186 }); | |
187 | |
188 test("non-token characters in invalid positions", () { | |
189 expect(() => new DataUri.decode("data:text//plain;base64,"), | |
190 throwsFormatException); | |
191 expect(() => new DataUri.decode("data:text/plain;;base64,"), | |
192 throwsFormatException); | |
193 expect(() => new DataUri.decode("data:text/plain;/base64,"), | |
194 throwsFormatException); | |
195 expect(() => new DataUri.decode("data:text/plain;,"), | |
196 throwsFormatException); | |
197 expect(() => new DataUri.decode("data:text/plain;base64;"), | |
198 throwsFormatException); | |
199 expect(() => new DataUri.decode("data:text/plain;foo=bar=baz;base64,"), | |
200 throwsFormatException); | |
201 }); | |
202 | |
203 test("encoded non-token characters in invalid positions", () { | |
204 expect(() => new DataUri.decode("data:te%2Cxt/plain;base64,"), | |
205 throwsFormatException); | |
206 expect(() => new DataUri.decode("data:text/pl%2Cain;base64,"), | |
207 throwsFormatException); | |
208 expect(() => new DataUri.decode("data:text/plain;f%2Coo=bar;base64,"), | |
209 throwsFormatException); | |
210 }); | |
211 }); | |
212 }); | |
213 | |
214 group("dataAsString", () { | |
215 test("decodes the data as ASCII by default", () { | |
216 var uri = new DataUri.decode("data:;base64,Zm9v"); | |
217 expect(uri.dataAsString(), equals("foo")); | |
218 | |
219 uri = new DataUri.decode("data:;base64,ZsO4bw=="); | |
220 expect(() => uri.dataAsString(), throwsFormatException); | |
221 }); | |
222 | |
223 test("decodes the data using the declared charset", () { | |
224 var uri = new DataUri.decode("data:;charset=iso-8859-1;base64,ZsO4bw=="); | |
225 expect(uri.dataAsString(), equals("føo")); | |
226 }); | |
227 | |
228 test("throws if the charset isn't supported", () { | |
229 var uri = new DataUri.decode("data:;charset=fblthp;base64,ZsO4bw=="); | |
230 expect(() => uri.dataAsString(), throwsUnsupportedError); | |
231 }); | |
232 | |
233 test("uses the given encoding in preference to the declared charset", () { | |
234 var uri = new DataUri.decode("data:;charset=fblthp;base64,ZsO4bw=="); | |
235 expect(uri.dataAsString(encoding: UTF8), equals("føo")); | |
236 | |
237 uri = new DataUri.decode("data:;charset=utf-8;base64,ZsO4bw=="); | |
238 expect(uri.dataAsString(encoding: LATIN1), equals("føo")); | |
239 }); | |
240 }); | |
241 } | |
OLD | NEW |