Skip to content

Commit 7eec2bf

Browse files
authored
data: URL processing and generalized forgiving-base64 decode tests
Tests for whatwg/fetch#579.
1 parent d17d539 commit 7eec2bf

File tree

6 files changed

+359
-167
lines changed

6 files changed

+359
-167
lines changed

fetch/data-urls/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
== data: URLs ==
2+
3+
`resources/data-urls.json` contains `data:` URL tests. The tests are encoded as a JSON array. Each value in the array is an array of two or three values. The first value describes the input, the second value describes the expected MIME type, null if the input is expected to fail somehow, or the empty string if the expected value is `text/plain;charset=US-ASCII`. The third value, if present, describes the expected body as an array of integers representing bytes.
4+
5+
These tests are used for `data:` URLs in this directory (see `processing.any.js`).
6+
7+
== Forgiving-base64 decode ==
8+
9+
`resources/base64.json` contains [forgiving-base64 decode](https://infra.spec.whatwg.org/#forgiving-base64-decode) tests. The tests are encoded as a JSON array. Each value in the array is an array of two values. The first value describes the input, the second value describes the output as an array of integers representing bytes or null if the input cannot be decoded.
10+
11+
These tests are used for `data:` URLs in this directory (see `base64.any.js`) and `window.atob()` in `../../html/webappapis/atob/base64.html`.

fetch/data-urls/base64.any.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
promise_test(() => fetch("resources/base64.json").then(res => res.json()).then(runBase64Tests), "Setup.");
2+
function runBase64Tests(tests) {
3+
for(let i = 0; i < tests.length; i++) {
4+
const input = tests[i][0],
5+
output = tests[i][1],
6+
dataURL = "data:;base64," + input;
7+
promise_test(t => {
8+
if(output === null) {
9+
return promise_rejects(t, new TypeError(), fetch(dataURL));
10+
}
11+
return fetch(dataURL).then(res => res.arrayBuffer()).then(body => {
12+
assert_array_equals(new Uint8Array(body), output);
13+
});
14+
}, "data: URL base64 handling: " + format_value(input));
15+
}
16+
}

fetch/data-urls/processing.any.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
promise_test(() => fetch("resources/data-urls.json").then(res => res.json()).then(runDataURLTests), "Setup.");
2+
function runDataURLTests(tests) {
3+
for(let i = 0; i < tests.length; i++) {
4+
const input = tests[i][0],
5+
expectedMimeType = tests[i][1],
6+
expectedBody = expectedMimeType !== null ? tests[i][2] : null;
7+
promise_test(t => {
8+
if(expectedMimeType === null) {
9+
return promise_rejects(t, new TypeError(), fetch(input));
10+
} else {
11+
return fetch(input).then(res => {
12+
return res.arrayBuffer().then(body => {
13+
assert_array_equals(new Uint8Array(body), expectedBody);
14+
assert_equals(res.headers.get("content-type"), expectedMimeType); // We could assert this earlier, but this fails often
15+
});
16+
});
17+
}
18+
}, format_value(input));
19+
}
20+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[
2+
["", []],
3+
["abcd", [105, 183, 29]],
4+
[" abcd", [105, 183, 29]],
5+
["abcd ", [105, 183, 29]],
6+
[" abcd===", null],
7+
["abcd=== ", null],
8+
["abcd ===", null],
9+
["a", null],
10+
["ab", [105]],
11+
["abc", [105, 183]],
12+
["abcde", null],
13+
["𐀀", null],
14+
["=", null],
15+
["==", null],
16+
["===", null],
17+
["====", null],
18+
["=====", null],
19+
["a=", null],
20+
["a==", null],
21+
["a===", null],
22+
["a====", null],
23+
["a=====", null],
24+
["ab=", null],
25+
["ab==", [105]],
26+
["ab===", null],
27+
["ab====", null],
28+
["ab=====", null],
29+
["abc=", [105, 183]],
30+
["abc==", null],
31+
["abc===", null],
32+
["abc====", null],
33+
["abc=====", null],
34+
["abcd=", null],
35+
["abcd==", null],
36+
["abcd===", null],
37+
["abcd====", null],
38+
["abcd=====", null],
39+
["abcde=", null],
40+
["abcde==", null],
41+
["abcde===", null],
42+
["abcde====", null],
43+
["abcde=====", null],
44+
["=a", null],
45+
["=a=", null],
46+
["a=b", null],
47+
["a=b=", null],
48+
["ab=c", null],
49+
["ab=c=", null],
50+
["abc=d", null],
51+
["abc=d=", null],
52+
["ab\tcd", [105, 183, 29]],
53+
["ab\ncd", [105, 183, 29]],
54+
["ab\fcd", [105, 183, 29]],
55+
["ab\rcd", [105, 183, 29]],
56+
["ab cd", [105, 183, 29]],
57+
["ab\u00a0cd", null],
58+
["ab\t\n\f\r cd", [105, 183, 29]],
59+
[" \t\n\f\r ab\t\n\f\r cd\t\n\f\r ", [105, 183, 29]],
60+
["ab\t\n\f\r =\t\n\f\r =\t\n\f\r ", [105]],
61+
["A", null],
62+
["/A", [252]],
63+
["//A", [255, 240]],
64+
["///A", [255, 255, 192]],
65+
["////A", null],
66+
["/", null],
67+
["A/", [3]],
68+
["AA/", [0, 15]],
69+
["AAAA/", null],
70+
["AAA/", [0, 0, 63]],
71+
["\u0000nonsense", null],
72+
["abcd\u0000nonsense", null],
73+
["YQ", [97]],
74+
["YR", [97]],
75+
["~~", null],
76+
["..", null],
77+
["--", null],
78+
["__", null]
79+
]
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
[
2+
["data://test/,X",
3+
"text/plain;charset=US-ASCII",
4+
[88]],
5+
["data://test:test/,X",
6+
null],
7+
["data:,X",
8+
"text/plain;charset=US-ASCII",
9+
[88]],
10+
["data:",
11+
null],
12+
["data:text/html",
13+
null],
14+
["data:text/html ;charset=x ",
15+
null],
16+
["data:,",
17+
"text/plain;charset=US-ASCII",
18+
[]],
19+
["data:,X#X",
20+
"text/plain;charset=US-ASCII",
21+
[88]],
22+
["data:,%FF",
23+
"text/plain;charset=US-ASCII",
24+
[255]],
25+
["data:text/plain,X",
26+
"text/plain",
27+
[88]],
28+
["data:text/plain ,X",
29+
"text/plain",
30+
[88]],
31+
["data:text/plain%20,X",
32+
"text/plain%20",
33+
[88]],
34+
["data:text/plain\f,X",
35+
"text/plain%0c",
36+
[88]],
37+
["data:text/plain%0C,X",
38+
"text/plain%0c",
39+
[88]],
40+
["data:text/plain;,X",
41+
"text/plain",
42+
[88]],
43+
["data:;x=x;charset=x,X",
44+
"text/plain;x=x;charset=x",
45+
[88]],
46+
["data:;x=x,X",
47+
"text/plain;x=x",
48+
[88]],
49+
["data:text/plain;charset=windows-1252,%C2%B1",
50+
"text/plain;charset=windows-1252",
51+
[194, 177]],
52+
["data:text/plain;Charset=UTF-8,%C2%B1",
53+
"text/plain;charset=UTF-8",
54+
[194, 177]],
55+
["data:image/gif,%C2%B1",
56+
"image/gif",
57+
[194, 177]],
58+
["data:IMAGE/gif,%C2%B1",
59+
"image/gif",
60+
[194, 177]],
61+
["data:IMAGE/gif;hi=x,%C2%B1",
62+
"image/gif;hi=x",
63+
[194, 177]],
64+
["data:IMAGE/gif;CHARSET=x,%C2%B1",
65+
"image/gif;charset=x",
66+
[194, 177]],
67+
["data: ,%FF",
68+
"text/plain;charset=US-ASCII",
69+
[255]],
70+
["data:%20,%FF",
71+
"text/plain;charset=US-ASCII",
72+
[255]],
73+
["data:\f,%FF",
74+
"text/plain;charset=US-ASCII",
75+
[255]],
76+
["data:%1F,%FF",
77+
"text/plain;charset=US-ASCII",
78+
[255]],
79+
["data:\u0000,%FF",
80+
"text/plain;charset=US-ASCII",
81+
[255]],
82+
["data:%00,%FF",
83+
"text/plain;charset=US-ASCII",
84+
[255]],
85+
["data:text/html ,X",
86+
"text/html",
87+
[88]],
88+
["data:text / html,X",
89+
"text/plain;charset=US-ASCII",
90+
[88]],
91+
["data:†,X",
92+
"text/plain;charset=US-ASCII",
93+
[88]],
94+
["data:†/†,X",
95+
"%e2%80%a0/%e2%80%a0",
96+
[88]],
97+
["data:X,X",
98+
"text/plain;charset=US-ASCII",
99+
[88]],
100+
["data:image/png,X X",
101+
"image/png",
102+
[88, 32, 88]],
103+
["data:application/xml,X X",
104+
"application/xml",
105+
[88, 32, 88]],
106+
["data:unknown/unknown,X X",
107+
"unknown/unknown",
108+
[88, 32, 88]],
109+
["data:text/plain;a=\",\",X",
110+
"text/plain",
111+
[34, 44, 88]],
112+
["data:text/plain;a=%2C,X",
113+
"text/plain;a=%2C",
114+
[88]],
115+
["data:;base64;base64,WA",
116+
"text/plain",
117+
[88]],
118+
["data:x/x;base64;base64,WA",
119+
"x/x",
120+
[88]],
121+
["data:x/x;base64;charset=x,WA",
122+
"x/x;charset=x",
123+
[87, 65]],
124+
["data:x/x;base64;charset=x;base64,WA",
125+
"x/x;charset=x",
126+
[88]],
127+
["data:x/x;base64;base64x,WA",
128+
"x/x",
129+
[87, 65]],
130+
["data:;base64,W%20A",
131+
"text/plain;charset=US-ASCII",
132+
[88]],
133+
["data:;base64,W%0CA",
134+
"text/plain;charset=US-ASCII",
135+
[88]],
136+
["data:x;base64x,WA",
137+
"text/plain;charset=US-ASCII",
138+
[87, 65]],
139+
["data:x;base64;x,WA",
140+
"text/plain;charset=US-ASCII",
141+
[87, 65]],
142+
["data:x;base64=x,WA",
143+
"text/plain;charset=US-ASCII",
144+
[87, 65]],
145+
["data:; base64,WA",
146+
"text/plain;charset=US-ASCII",
147+
[88]],
148+
["data:; base64,WA",
149+
"text/plain;charset=US-ASCII",
150+
[88]],
151+
["data: ;charset=x ; base64,WA",
152+
"text/plain;charset=x",
153+
[88]],
154+
["data:;base64;,WA",
155+
"text/plain",
156+
[87, 65]],
157+
["data:;base64 ,WA",
158+
"text/plain;charset=US-ASCII",
159+
[88]],
160+
["data:;base64 ,WA",
161+
"text/plain;charset=US-ASCII",
162+
[88]],
163+
["data:;base 64,WA",
164+
"text/plain",
165+
[87, 65]],
166+
["data:;BASe64,WA",
167+
"text/plain;charset=US-ASCII",
168+
[88]],
169+
["data:;%62ase64,WA",
170+
"text/plain",
171+
[87, 65]],
172+
["data:%3Bbase64,WA",
173+
"text/plain;charset=US-ASCII",
174+
[87, 65]],
175+
["data:;charset=x,X",
176+
"text/plain;charset=x",
177+
[88]],
178+
["data:; charset=x,X",
179+
"text/plain;charset=x",
180+
[88]],
181+
["data:;charset =x,X",
182+
"text/plain",
183+
[88]],
184+
["data:;charset= x,X",
185+
"text/plain;charset=\" x\"",
186+
[88]],
187+
["data:;charset=,X",
188+
"text/plain",
189+
[88]],
190+
["data:;charset,X",
191+
"text/plain",
192+
[88]],
193+
["data:;charset=\"x\",X",
194+
"text/plain;charset=x",
195+
[88]],
196+
["data:;CHARSET=\"X\",X",
197+
"text/plain;charset=X",
198+
[88]]
199+
]

0 commit comments

Comments
 (0)