-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathxhr.test.js
More file actions
212 lines (172 loc) · 6.88 KB
/
Copy pathxhr.test.js
File metadata and controls
212 lines (172 loc) · 6.88 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import window from 'videojs-global-compat/window';
import QUnit from 'qunit';
import {default as xhrFactory, byterangeStr} from '../src/xhr';
import { useFakeEnvironment } from './test-helpers.js';
import videojs from 'video.js';
// needed for plugin registration
import '../src/videojs-http-streaming';
QUnit.module('xhr', {
beforeEach(assert) {
this.env = useFakeEnvironment(assert);
this.clock = this.env.clock;
this.requests = this.env.requests;
this.xhr = xhrFactory();
},
afterEach() {
this.env.restore();
}
});
QUnit.test('xhr respects beforeRequest', function(assert) {
const defaultOptions = {
url: 'default'
};
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'default', 'url the same without override');
this.xhr.beforeRequest = (options) => {
options.url = 'player';
return options;
};
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'player', 'url changed with player override');
assert.equal(this.env.log.warn.calls, 1, 'warning logged for deprecation');
videojs.Vhs.xhr.beforeRequest = (options) => {
options.url = 'videojs-global-compat';
return options;
};
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'player', 'prioritizes player override');
assert.equal(this.env.log.warn.calls, 1, 'warning logged for deprecation');
delete this.xhr.beforeRequest;
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'videojs-global-compat', 'url changed with global override');
assert.equal(this.env.log.warn.calls, 1, 'warning logged for deprecation');
delete videojs.Vhs.xhr.beforeRequest;
});
QUnit.test('beforeRequest can return a new options object', function(assert) {
const defaultOptions = {
url: 'default'
};
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'default', 'url the same without override');
videojs.Vhs.xhr.beforeRequest = () => {
return { uri: 'videojs-global-compat-newOptions'};
};
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'videojs-global-compat-newOptions', 'url changed with global override');
assert.equal(this.env.log.warn.calls, 1, 'warning logged for deprecation');
this.xhr.beforeRequest = () => {
return { uri: 'player-newOptions'};
};
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'player-newOptions', 'url changed with player override');
assert.equal(this.env.log.warn.calls, 1, 'warning logged for deprecation');
delete this.xhr.beforeRequest;
delete videojs.Vhs.xhr.beforeRequest;
this.xhr(defaultOptions);
assert.equal(this.requests.shift().url, 'default', 'url the same without override');
});
QUnit.test('calls global and player onRequest hooks respectively', function(assert) {
const defaultOptions = {
url: 'default'
};
this.xhr(defaultOptions);
let xhrRequest = this.requests.shift();
// create the global onRequest set and 2 hooks
videojs.Vhs.xhr._requestCallbackSet = new Set();
const globalRequestHook1 = (options) => {
options.url = 'videojs-global-compat';
return options;
};
const globalRequestHook2 = (options) => {
options.headers = {
foo: 'bar'
};
return options;
};
// add them to the set
videojs.Vhs.xhr._requestCallbackSet.add(globalRequestHook1);
videojs.Vhs.xhr._requestCallbackSet.add(globalRequestHook2);
this.xhr(defaultOptions);
xhrRequest = this.requests.shift();
assert.equal(xhrRequest.url, 'videojs-global-compat', 'url changed with global onRequest hooks');
assert.equal(xhrRequest.headers.foo, 'bar', 'headers changed with global onRequest hooks');
// create the player onRequest set and 2 hooks
this.xhr._requestCallbackSet = new Set();
const playerRequestHook1 = (options) => {
options.url = 'player';
return options;
};
const playerRequestHook2 = (options) => {
options.headers = {
bar: 'foo'
};
return options;
};
// add them to the set
this.xhr._requestCallbackSet.add(playerRequestHook1);
this.xhr._requestCallbackSet.add(playerRequestHook2);
this.xhr(defaultOptions);
xhrRequest = this.requests.shift();
// player level request hooks override global
assert.equal(xhrRequest.url, 'player', 'url changed with player onRequest hooks');
assert.equal(xhrRequest.headers.bar, 'foo', 'headers changed with player onRequest hooks');
// delete player level request hooks and check to ensure global are still used
delete this.xhr._requestCallbackSet;
this.xhr(defaultOptions);
xhrRequest = this.requests.shift();
assert.equal(xhrRequest.url, 'videojs-global-compat', 'url changed with player onRequest hooks');
assert.equal(xhrRequest.headers.foo, 'bar', 'headers changed with player onRequest hooks');
delete videojs.Vhs.xhr._requestCallbackSet;
this.xhr(defaultOptions);
xhrRequest = this.requests.shift();
assert.notEqual(xhrRequest.headers.foo, 'bar', 'headers the same without onRequest hooks');
});
QUnit.test('xhr calls global and player onResponse hooks respectively', function(assert) {
const done = assert.async();
const defaultOptions = {
url: 'default'
};
let globalHookCallCount = 0;
// Create global onResponse set and 2 hooks
videojs.Vhs.xhr._responseCallbackSet = new Set();
const globalOnResponseHook1 = (request, error, response) => {
globalHookCallCount++;
};
const globalOnResponseHook2 = (request, error, response) => {
globalHookCallCount++;
};
videojs.Vhs.xhr._responseCallbackSet.add(globalOnResponseHook1);
videojs.Vhs.xhr._responseCallbackSet.add(globalOnResponseHook2);
// Create player onResponse set and 2 hooks
this.xhr._responseCallbackSet = new Set();
const playerOnResponseHook1 = (request, error, response) => {
assert.equal(response.body, 'foo-bar', 'expected response body');
assert.equal(response.method, 'GET', 'expected method');
};
const playerOnResponseHook2 = (request, error, response) => {
assert.equal(response.headers.foo, 'bar', 'expected headers');
assert.equal(response.statusCode, 200, 'expected statusCode');
assert.equal(globalHookCallCount, 0, 'videojs-global-compat response hooks not called yet');
done();
};
this.xhr._responseCallbackSet.add(playerOnResponseHook1);
this.xhr._responseCallbackSet.add(playerOnResponseHook2);
this.xhr(defaultOptions, () => { });
this.requests.shift().respond(200, { foo: 'bar' }, 'foo-bar');
});
QUnit.test('byterangeStr works as expected', function(assert) {
assert.equal(byterangeStr({offset: 20, length: 15}), 'bytes=20-34', 'as expected');
assert.equal(byterangeStr({offset: 0, length: 40}), 'bytes=0-39', 'as expected');
if (window.BigInt) {
assert.equal(
byterangeStr({offset: window.BigInt(20), length: window.BigInt(15)}),
'bytes=20-34',
'bigint result as expected'
);
assert.equal(
byterangeStr({offset: window.BigInt(0), length: window.BigInt(40)}),
'bytes=0-39',
'bigint result as expected'
);
}
});