forked from angular/zone.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathXMLHttpRequest.spec.ts
283 lines (252 loc) · 9.13 KB
/
XMLHttpRequest.spec.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ifEnvSupports, supportPatchXHROnProperty} from '../test-util';
describe('XMLHttpRequest', function() {
let testZone: Zone;
beforeEach(() => {
testZone = Zone.current.fork({name: 'test'});
});
it('should intercept XHRs and treat them as MacroTasks', function(done) {
let req: XMLHttpRequest;
const testZoneWithWtf =
Zone.current.fork((Zone as any)['wtfZoneSpec']).fork({name: 'TestZone'});
testZoneWithWtf.run(() => {
req = new XMLHttpRequest();
req.onload = () => {
// The last entry in the log should be the invocation for the current onload,
// which will vary depending on browser environment. The prior entries
// should be the invocation of the send macrotask.
expect(wtfMock.log[wtfMock.log.length - 3])
.toEqual('> Zone:invokeTask:XMLHttpRequest.send("<root>::ProxyZone::WTF::TestZone")');
expect(wtfMock.log[wtfMock.log.length - 2])
.toEqual('< Zone:invokeTask:XMLHttpRequest.send');
if (supportPatchXHROnProperty()) {
expect(wtfMock.log[wtfMock.log.length - 1])
.toMatch(/\> Zone\:invokeTask.*addEventListener\:load/);
}
done();
};
req.open('get', '/', true);
req.send();
const lastScheduled = wtfMock.log[wtfMock.log.length - 1];
expect(lastScheduled).toMatch('# Zone:schedule:macroTask:XMLHttpRequest.send');
}, null, null, 'unit-test');
});
it('should not trigger Zone callback of internal onreadystatechange', function(done) {
const scheduleSpy = jasmine.createSpy('schedule');
const xhrZone = Zone.current.fork({
name: 'xhr',
onScheduleTask: (delegate: ZoneDelegate, currentZone: Zone, targetZone, task: Task) => {
if (task.type === 'eventTask') {
scheduleSpy(task.source);
}
return delegate.scheduleTask(targetZone, task);
}
});
xhrZone.run(() => {
const req = new XMLHttpRequest();
req.onload = function() {
expect(Zone.current.name).toEqual('xhr');
if (supportPatchXHROnProperty()) {
expect(scheduleSpy).toHaveBeenCalled();
}
done();
};
req.open('get', '/', true);
req.send();
});
});
it('should work with onreadystatechange', function(done) {
let req: XMLHttpRequest;
testZone.run(function() {
req = new XMLHttpRequest();
req.onreadystatechange = function() {
// Make sure that the wrapCallback will only be called once
req.onreadystatechange = null;
expect(Zone.current).toBe(testZone);
done();
};
req.open('get', '/', true);
});
req.send();
});
it('should return null when access ontimeout first time without error', function() {
let req: XMLHttpRequest = new XMLHttpRequest();
expect(req.ontimeout).toBe(null);
});
const supportsOnProgress = function() {
return 'onprogress' in (new XMLHttpRequest());
};
(<any>supportsOnProgress).message = 'XMLHttpRequest.onprogress';
describe('onprogress', ifEnvSupports(supportsOnProgress, function() {
it('should work with onprogress', function(done) {
let req: XMLHttpRequest;
testZone.run(function() {
req = new XMLHttpRequest();
req.onprogress = function() {
// Make sure that the wrapCallback will only be called once
req.onprogress = null;
expect(Zone.current).toBe(testZone);
done();
};
req.open('get', '/', true);
});
req.send();
});
it('should allow canceling of an XMLHttpRequest', function(done) {
const spy = jasmine.createSpy('spy');
let req: XMLHttpRequest;
let pending = false;
const trackingTestZone = Zone.current.fork({
name: 'tracking test zone',
onHasTask: (delegate: ZoneDelegate, current: Zone, target: Zone,
hasTaskState: HasTaskState) => {
if (hasTaskState.change == 'macroTask') {
pending = hasTaskState.macroTask;
}
delegate.hasTask(target, hasTaskState);
}
});
trackingTestZone.run(function() {
req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
if (req.status !== 0) {
spy();
}
}
};
req.open('get', '/', true);
req.send();
req.abort();
});
setTimeout(function() {
expect(spy).not.toHaveBeenCalled();
expect(pending).toEqual(false);
done();
}, 0);
});
it('should allow aborting an XMLHttpRequest after its completed', function(done) {
let req: XMLHttpRequest;
testZone.run(function() {
req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === XMLHttpRequest.DONE) {
if (req.status !== 0) {
setTimeout(function() {
req.abort();
done();
}, 0);
}
}
};
req.open('get', '/', true);
req.send();
});
});
}));
it('should preserve other setters', function() {
const req = new XMLHttpRequest();
req.open('get', '/', true);
req.send();
try {
req.responseType = 'document';
expect(req.responseType).toBe('document');
} catch (e) {
// Android browser: using this setter throws, this should be preserved
expect(e.message).toBe('INVALID_STATE_ERR: DOM Exception 11');
}
});
it('should work with synchronous XMLHttpRequest', function() {
const log: HasTaskState[] = [];
Zone.current
.fork({
name: 'sync-xhr-test',
onHasTask: function(
delegate: ZoneDelegate, current: Zone, target: Zone, hasTaskState: HasTaskState) {
log.push(hasTaskState);
delegate.hasTask(target, hasTaskState);
}
})
.run(() => {
const req = new XMLHttpRequest();
req.open('get', '/', false);
req.send();
});
expect(log).toEqual([]);
});
it('should preserve static constants', function() {
expect(XMLHttpRequest.UNSENT).toEqual(0);
expect(XMLHttpRequest.OPENED).toEqual(1);
expect(XMLHttpRequest.HEADERS_RECEIVED).toEqual(2);
expect(XMLHttpRequest.LOADING).toEqual(3);
expect(XMLHttpRequest.DONE).toEqual(4);
});
it('should work properly when send request multiple times on single xmlRequest instance',
function(done) {
testZone.run(function() {
const req = new XMLHttpRequest();
req.open('get', '/', true);
req.send();
req.onload = function() {
req.onload = null;
req.open('get', '/', true);
req.onload = function() {
done();
};
expect(() => {
req.send();
}).not.toThrow();
};
});
});
it('should keep taskcount correctly when abort was called multiple times before request is done',
function(done) {
testZone.run(function() {
const req = new XMLHttpRequest();
req.open('get', '/', true);
req.send();
req.addEventListener('readystatechange', function(ev) {
if (req.readyState >= 2) {
expect(() => {
req.abort();
}).not.toThrow();
done();
}
});
});
});
it('should not throw error when get XMLHttpRequest.prototype.onreadystatechange the first time',
function() {
const func = function() {
testZone.run(function() {
const req = new XMLHttpRequest();
req.onreadystatechange;
});
};
expect(func).not.toThrow();
});
it('should be in the zone when use XMLHttpRequest.addEventListener', function(done) {
testZone.run(function() {
// sometimes this case will cause timeout
// so we set it longer
const interval = (<any>jasmine).DEFAULT_TIMEOUT_INTERVAL;
(<any>jasmine).DEFAULT_TIMEOUT_INTERVAL = 5000;
const req = new XMLHttpRequest();
req.open('get', '/', true);
req.addEventListener('readystatechange', function() {
if (req.readyState === 4) {
// expect(Zone.current.name).toEqual('test');
(<any>jasmine).DEFAULT_TIMEOUT_INTERVAL = interval;
done();
}
});
req.send();
});
});
});