-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathdata_uri_plugin_unit.js
107 lines (91 loc) · 3.5 KB
/
data_uri_plugin_unit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('DataUriPlugin', () => {
const retryParameters = shaka.net.NetworkingEngine.defaultRetryParameters();
it('supports MIME types', async () => {
await testSucceeds('data:text/plain,Hello', 'text/plain', 'Hello');
});
it('supports URI encoded text', async () => {
await testSucceeds(
'data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E',
'text/html',
'<h1>Hello, World!</h1>');
});
it('supports base64 encoded text', async () => {
await testSucceeds(
// cspell: disable-next-line
'data:;base64,SGVsbG8sIFdvcmxkIQ%3D%3D', '', 'Hello, World!');
});
it('supports extra colon', async () => {
await testSucceeds('data:,Hello:', '', 'Hello:');
});
it('supports extra semi-colon', async () => {
await testSucceeds('data:,Hello;', '', 'Hello;');
});
it('supports extra comma', async () => {
await testSucceeds('data:,Hello,', '', 'Hello,');
});
it('supports character set metadata', async () => {
await testSucceeds(
'data:text/plain;charset=UTF-8,Hello,', 'text/plain', 'Hello,');
});
it('supports arbitrary metadata', async () => {
await testSucceeds(
'data:text/plain;foo=bar,Hello,', 'text/plain', 'Hello,');
});
it('supports arbitrary metadata with base64 encoding', async () => {
await testSucceeds(
// cspell: disable-next-line
'data:text/plain;foo=bar;base64,SGVsbG8sIFdvcmxkIQ%3D%3D',
'text/plain',
'Hello, World!');
});
it('fails for empty URI', async () => {
await testFails('', shaka.util.Error.Code.MALFORMED_DATA_URI);
});
it('fails for non-data URIs', async () => {
await testFails(
'http://google.com/', shaka.util.Error.Code.MALFORMED_DATA_URI);
});
it('fails for decoding errors', async () => {
await testFails('data:Bad%', shaka.util.Error.Code.MALFORMED_DATA_URI);
});
it('fails if missing comma', async () => {
await testFails('data:Bad', shaka.util.Error.Code.MALFORMED_DATA_URI);
});
async function testSucceeds(uri, contentType, text) {
// An arbitrary request type.
const requestType = shaka.net.NetworkingEngine.RequestType.SEGMENT;
// A dummy progress callback.
const progressUpdated = (elapsedMs, bytes, bytesRemaining) => {};
const request =
shaka.net.NetworkingEngine.makeRequest([uri], retryParameters);
const response = await shaka.net.DataUriPlugin.parse(
uri, request, requestType, progressUpdated).promise;
expect(response).toBeTruthy();
expect(response.uri).toBe(uri);
expect(response.data).toBeTruthy();
expect(response.headers['content-type']).toBe(contentType);
const data =
shaka.util.StringUtils.fromBytesAutoDetect(response.data);
expect(data).toBe(text);
}
async function testFails(uri, code) {
// An arbitrary request type.
const requestType = shaka.net.NetworkingEngine.RequestType.SEGMENT;
// A dummy progress callback.
const progressUpdated = (elapsedMs, bytes, bytesRemaining) => {};
const request =
shaka.net.NetworkingEngine.makeRequest([uri], retryParameters);
const expected = shaka.test.Util.jasmineError(new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL, shaka.util.Error.Category.NETWORK,
code, uri));
await expectAsync(
shaka.net.DataUriPlugin.parse(
uri, request, requestType, progressUpdated).promise)
.toBeRejectedWith(expected);
}
});