This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.js
131 lines (95 loc) · 2.99 KB
/
tests.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
///////////////////////////////////
var timeout = 10000;
var bigChunkUrl = "http://www.streambox.fr/playlists/test_001/stream_110k_48k_416x234_000.ts";
var fooUrl = '/data/foo.txt';
var barUrl = '/data/barbar.txt';
////////////////////////////////
describe('xhr-shim', function() {
this.timeout(timeout);
before(function() {
XHRShaper.useGlobal();
});
afterEach(function() {
// just make sure to reset to default this in case it was modified in tests
XMLHttpRequest.Shaper.minLatency = 0;
XMLHttpRequest.Shaper.maxBandwidth = Infinity;
});
it('should have a shaper instance', function() {
var xhr = new XMLHttpRequest();
(typeof xhr.shaper).should.equal('object');
});
it('should have a global Shaper object', function() {
(typeof XMLHttpRequest.Shaper).should.equal('function');
});
it('should make a large throttled request', function(done) {
var xhr = makeRequest(bigChunkUrl, done, 1000, 512);
});
it('should make a large throttled request and accurately limit the bandwidth', function(done) {
var xhr = makeRequest(bigChunkUrl, done, 0, 512);
});
it('should make two consequent requests', function(done) {
var xhr = makeRequest(fooUrl, function() {
xhr.response.byteLength.should.equal(3);
xhr = makeRequest(barUrl, function() {
xhr.response.byteLength.should.equal(6);
done();
});
});
});
it('should make two concurrent requests', function(done) {
var doneCount = 0;
function incrDone() {
doneCount++;
if (doneCount == 2) {
done();
}
}
var fooXhr = makeRequest(fooUrl, function() {
console.log('foo done');
fooXhr.response.byteLength.should.equal(3);
incrDone();
});
var barbarXhr = makeRequest(barUrl, function() {
console.log('barbar done');
barbarXhr.response.byteLength.should.equal(6);
incrDone();
});
});
it('should make one request', function(done) {
var doneCount = 0;
function incrDone() {
doneCount++;
if (doneCount == 1) {
done();
}
}
var barbarXhr = makeRequest(barUrl, function() {
barbarXhr.response.byteLength.should.equal(6);
incrDone();
});
});
it('should allow to use listeners on XHR EventTarget API', function(done) {
var xhr = new XMLHttpRequest();
var url = barUrl;
xhr.addEventListener('loadend', function() {
done();
});
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
});
it('should allow to use listeners on XHR EventTarget API (with throttling)', function(done) {
var startTime = Date.now();
var xhr = new XMLHttpRequest();
var url = bigChunkUrl;
xhr.shaper.minLatency = 2000;
xhr.shaper.maxBandwidth = 1024;
xhr.addEventListener('loadend', function() {
console.log('done in ', Date.now() - startTime, ' ms');
done();
});
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send();
});
});