forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax-spec.ts
505 lines (412 loc) Β· 12.9 KB
/
ajax-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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import {expect} from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../../dist/cjs/Rx';
import {root} from '../../../dist/cjs/util/root';
import {MockXMLHttpRequest} from '../../helpers/ajax-helper';
declare const global: any;
/** @test {ajax} */
describe('Observable.ajax', () => {
let gXHR: XMLHttpRequest;
let rXHR: XMLHttpRequest;
let sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
gXHR = global.XMLHttpRequest;
rXHR = root.XMLHttpRequest;
global.XMLHttpRequest = MockXMLHttpRequest;
root.XMLHttpRequest = MockXMLHttpRequest;
});
afterEach(() => {
sandbox.restore();
MockXMLHttpRequest.clearRequest();
global.XMLHttpRequest = gXHR;
root.XMLHttpRequest = rXHR;
root.XDomainRequest = null;
root.ActiveXObject = null;
});
it('should create default XMLHttpRequest for non CORS', () => {
const obj: Rx.AjaxRequest = {
url: '/',
method: ''
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.withCredentials).to.be.false;
});
it('should try to create AXObject for XHR in old version of IE', () => {
const axObjectStub = sandbox.stub();
axObjectStub.returns(sinon.stub(new MockXMLHttpRequest()));
root.ActiveXObject = axObjectStub;
root.XMLHttpRequest = null;
const obj: Rx.AjaxRequest = {
url: '/',
method: ''
};
Rx.Observable.ajax(obj).subscribe();
expect(axObjectStub).to.have.been.called;
});
it('should throw if not able to create XMLHttpRequest', () => {
root.XMLHttpRequest = null;
root.ActiveXObject = null;
const obj: Rx.AjaxRequest = {
url: '/',
method: ''
};
expect(() => {
Rx.Observable.ajax(obj).subscribe();
}).to.throw();
});
it('should create XMLHttpRequest for CORS', () => {
const obj: Rx.AjaxRequest = {
url: '/',
method: '',
crossDomain: true,
withCredentials: true
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.withCredentials).to.be.true;
});
it('should try to create XDomainRequest for CORS if XMLHttpRequest is not available', () => {
const xDomainStub = sandbox.stub();
xDomainStub.returns(sinon.stub(new MockXMLHttpRequest()));
root.XDomainRequest = xDomainStub;
root.XMLHttpRequest = null;
const obj: Rx.AjaxRequest = {
url: '/',
method: '',
crossDomain: true,
withCredentials: true
};
Rx.Observable.ajax(obj).subscribe();
expect(xDomainStub).to.have.been.called;
});
it('should throw if not able to create CORS request', () => {
root.XMLHttpRequest = null;
root.XDomainRequest = null;
const obj: Rx.AjaxRequest = {
url: '/',
method: '',
crossDomain: true,
withCredentials: true
};
expect(() => {
Rx.Observable.ajax(obj).subscribe();
}).to.throw();
});
it('should set headers', () => {
const obj: Rx.AjaxRequest = {
url: '/talk-to-me-goose',
headers: {
'Content-Type': 'kenny/loggins',
'Fly-Into-The': 'Dangah Zone!',
'Take-A-Ride-Into-The': 'Danger ZoooOoone!'
},
method: ''
};
Rx.Observable.ajax(obj).subscribe();
const request = MockXMLHttpRequest.mostRecent;
expect(request.url).to.equal('/talk-to-me-goose');
expect(request.requestHeaders).to.deep.equal({
'Content-Type': 'kenny/loggins',
'Fly-Into-The': 'Dangah Zone!',
'Take-A-Ride-Into-The': 'Danger ZoooOoone!',
'X-Requested-With': 'XMLHttpRequest'
});
});
it('should not set default Content-Type header when no body is sent', () => {
const obj: Rx.AjaxRequest = {
url: '/talk-to-me-goose',
method: 'GET'
};
Rx.Observable.ajax(obj).subscribe();
const request = MockXMLHttpRequest.mostRecent;
expect(request.url).to.equal('/talk-to-me-goose');
expect(request.requestHeaders).to.not.have.keys('Content-Type');
});
it('should error if createXHR throws', () => {
let error;
const obj = {
url: '/flibbertyJibbet',
responseType: 'text',
createXHR: () => {
throw new Error('wokka wokka');
}
};
Rx.Observable.ajax(<any>obj)
.subscribe((x: any) => {
throw 'should not next';
}, (err: any) => {
error = err;
}, () => {
throw 'should not complete';
});
expect(error).to.be.an('error', 'wokka wokka');
});
it('should succeed on 200', () => {
const expected = { foo: 'bar' };
let result;
let complete = false;
const obj = {
url: '/flibbertyJibbet',
responseType: 'text',
method: ''
};
Rx.Observable.ajax(obj)
.subscribe((x: any) => {
result = x;
}, null, () => {
complete = true;
});
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
MockXMLHttpRequest.mostRecent.respondWith({
'status': 200,
'contentType': 'application/json',
'responseText': JSON.stringify(expected)
});
expect(result.xhr).exist;
expect(result.response).to.deep.equal(JSON.stringify({ foo: 'bar' }));
expect(complete).to.be.true;
});
it('should fail on 404', () => {
let error;
const obj = {
url: '/flibbertyJibbet',
normalizeError: (e: any, xhr: any, type: any) => {
return xhr.response || xhr.responseText;
},
responseType: 'text',
method: ''
};
Rx.Observable.ajax(obj)
.subscribe((x: any) => {
throw 'should not next';
}, (err: any) => {
error = err;
}, () => {
throw 'should not complete';
});
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
MockXMLHttpRequest.mostRecent.respondWith({
'status': 404,
'contentType': 'text/plain',
'responseText': 'Wee! I am text!'
});
expect(error instanceof Rx.AjaxError).to.be.true;
expect(error.message).to.equal('ajax error 404');
expect(error.status).to.equal(404);
});
it('should fail on 404', () => {
let error;
const obj = {
url: '/flibbertyJibbet',
normalizeError: (e: any, xhr: any, type: any) => {
return xhr.response || xhr.responseText;
},
responseType: 'text',
method: ''
};
Rx.Observable.ajax(obj).subscribe(x => {
throw 'should not next';
}, (err: any) => {
error = err;
}, () => {
throw 'should not complete';
});
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
MockXMLHttpRequest.mostRecent.respondWith({
'status': 300,
'contentType': 'text/plain',
'responseText': 'Wee! I am text!'
});
expect(error instanceof Rx.AjaxError).to.be.true;
expect(error.message).to.equal('ajax error 300');
expect(error.status).to.equal(300);
});
it('should succeed no settings', () => {
const expected = JSON.stringify({ foo: 'bar' });
Rx.Observable.ajax('/flibbertyJibbet')
.subscribe((x: any) => {
expect(x.status).to.equal(200);
expect(x.xhr.method).to.equal('GET');
expect(x.xhr.responseText).to.equal(expected);
}, () => {
throw 'should not have been called';
});
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
MockXMLHttpRequest.mostRecent.respondWith({
'status': 200,
'contentType': 'text/plain',
'responseText': expected
});
});
it('should fail no settings', () => {
const expected = JSON.stringify({ foo: 'bar' });
Rx.Observable.ajax('/flibbertyJibbet')
.subscribe(() => {
throw 'should not have been called';
}, (x: any) => {
expect(x.status).to.equal(500);
expect(x.xhr.method).to.equal('GET');
expect(x.xhr.responseText).to.equal(expected);
}, () => {
throw 'should not have been called';
});
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
MockXMLHttpRequest.mostRecent.respondWith({
'status': 500,
'contentType': 'text/plain',
'responseText': expected
});
});
describe('ajax request body', () => {
let rFormData: FormData;
beforeEach(() => {
rFormData = root.FormData;
root.FormData = root.FormData || class { };
});
afterEach(() => {
root.FormData = rFormData;
});
it('can take string body', () => {
const obj = {
url: '/flibbertyJibbet',
method: '',
body: 'foobar'
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.equal('foobar');
});
it('can take FormData body', () => {
const body = new root.FormData();
const obj = {
url: '/flibbertyJibbet',
method: '',
body: body
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.deep.equal(body);
expect(MockXMLHttpRequest.mostRecent.requestHeaders).to.deep.equal({
'X-Requested-With': 'XMLHttpRequest',
});
});
it('should not fail when FormData is undefined', () => {
root.FormData = void 0;
const obj = {
url: '/flibbertyJibbet',
method: '',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: { 'π': 'π' }
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
});
it('should send by form-urlencoded format', () => {
const body = {
'π': 'π'
};
const obj = {
url: '/flibbertyJibbet',
method: '',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: body
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.equal('%F0%9F%8C%9F=%F0%9F%9A%80');
});
it('should send by JSON', () => {
const body = {
'π': 'π'
};
const obj = {
url: '/flibbertyJibbet',
method: '',
headers: {
'Content-Type': 'application/json'
},
body: body
};
Rx.Observable.ajax(obj).subscribe();
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
expect(MockXMLHttpRequest.mostRecent.data).to.equal('{"π":"π"}');
});
});
describe('ajax.get', () => {
it('should succeed on 200', () => {
const expected = { foo: 'bar' };
let result;
let complete = false;
Rx.Observable
.ajax.get('/flibbertyJibbet')
.subscribe(x => {
result = x.response;
}, null, () => {
complete = true;
});
const request = MockXMLHttpRequest.mostRecent;
expect(request.url).to.equal('/flibbertyJibbet');
request.respondWith({
'status': 200,
'contentType': 'application/json',
'responseText': JSON.stringify(expected)
});
expect(result).to.deep.equal(expected);
expect(complete).to.be.true;
});
it('should able to select json response via getJSON', () => {
const expected = { foo: 'bar' };
let result;
let complete = false;
Rx.Observable
.ajax.getJSON('/flibbertyJibbet')
.subscribe(x => {
result = x;
}, null, () => {
complete = true;
});
const request = MockXMLHttpRequest.mostRecent;
expect(request.url).to.equal('/flibbertyJibbet');
request.respondWith({
'status': 200,
'contentType': 'application/json',
'responseText': JSON.stringify(expected)
});
expect(result).to.deep.equal(expected);
expect(complete).to.be.true;
});
});
describe('ajax.post', () => {
it('should succeed on 200', () => {
const expected = { foo: 'bar', hi: 'there you' };
let result: Rx.AjaxResponse;
let complete = false;
Rx.Observable
.ajax.post('/flibbertyJibbet', expected)
.subscribe(x => {
result = x;
}, null, () => {
complete = true;
});
const request = MockXMLHttpRequest.mostRecent;
expect(request.method).to.equal('POST');
expect(request.url).to.equal('/flibbertyJibbet');
expect(request.requestHeaders).to.deep.equal({
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
});
request.respondWith({
'status': 200,
'contentType': 'application/json',
'responseText': JSON.stringify(expected)
});
expect(request.data).to.equal('foo=bar&hi=there%20you');
expect(result.response).to.deep.equal(expected);
expect(complete).to.be.true;
});
});
});