forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_spec.ts
539 lines (476 loc) · 22.4 KB
/
http_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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
* @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 {Injector} from '@angular/core';
import {TestBed, getTestBed} from '@angular/core/testing';
import {AsyncTestCompleter, afterEach, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal';
import {stringToArrayBuffer} from '@angular/http/src/http_utils';
import {MockBackend, MockConnection} from '@angular/http/testing/src/mock_backend';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {Observable, zip} from 'rxjs';
import {BaseRequestOptions, ConnectionBackend, Http, HttpModule, JSONPBackend, Jsonp, JsonpModule, Request, RequestMethod, RequestOptions, Response, ResponseContentType, ResponseOptions, URLSearchParams, XHRBackend} from '../index';
{
describe('injectables', () => {
const url = 'http://foo.bar';
let http: Http;
let injector: Injector;
let jsonpBackend: MockBackend;
let xhrBackend: MockBackend;
let jsonp: Jsonp;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule, JsonpModule],
providers: [
{provide: XHRBackend, useClass: MockBackend},
{provide: JSONPBackend, useClass: MockBackend}
]
});
injector = getTestBed();
});
it('should allow using jsonpInjectables and httpInjectables in same injector',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
http = injector.get(Http);
jsonp = injector.get(Jsonp);
jsonpBackend = injector.get(JSONPBackend) as any as MockBackend;
xhrBackend = injector.get(XHRBackend) as any as MockBackend;
let xhrCreatedConnections = 0;
let jsonpCreatedConnections = 0;
xhrBackend.connections.subscribe(() => {
xhrCreatedConnections++;
expect(xhrCreatedConnections).toEqual(1);
if (jsonpCreatedConnections) {
async.done();
}
});
http.get(url).subscribe(() => {});
jsonpBackend.connections.subscribe(() => {
jsonpCreatedConnections++;
expect(jsonpCreatedConnections).toEqual(1);
if (xhrCreatedConnections) {
async.done();
}
});
jsonp.request(url).subscribe(() => {});
}));
});
describe('http', () => {
const url = 'http://foo.bar';
let http: Http;
let injector: Injector;
let backend: MockBackend;
let baseResponse: Response;
let jsonp: Jsonp;
beforeEach(() => {
injector = Injector.create([
{provide: BaseRequestOptions, deps: []}, {provide: MockBackend, deps: []}, {
provide: Http,
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
{
provide: Jsonp,
useFactory: function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
return new Jsonp(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
}
]);
http = injector.get(Http);
jsonp = injector.get(Jsonp);
backend = injector.get(MockBackend);
baseResponse = new Response(new ResponseOptions({body: 'base response'}));
spyOn(Http.prototype, 'request').and.callThrough();
});
afterEach(() => backend.verifyNoPendingRequests());
describe('Http', () => {
describe('.request()', () => {
it('should return an Observable',
() => { expect(http.request(url)).toBeAnInstanceOf(Observable); });
it('should accept a fully-qualified request as its only parameter',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toBe('https://google.com');
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
async.done();
});
http.request(new Request(new RequestOptions({url: 'https://google.com'})))
.subscribe((res: Response) => {});
}));
it('should accept a fully-qualified request as its only parameter',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toBe('https://google.com');
expect(c.request.method).toBe(RequestMethod.Post);
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
async.done();
});
http.request(new Request(new RequestOptions(
{url: 'https://google.com', method: RequestMethod.Post})))
.subscribe((res: Response) => {});
}));
it('should perform a get request for given url if only passed a string',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
http.request('http://basic.connection').subscribe((res: Response) => {
expect(res.text()).toBe('base response');
async.done();
});
}));
it('should perform a post request for given url if options include a method',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse);
});
const requestOptions = new RequestOptions({method: RequestMethod.Post});
http.request('http://basic.connection', requestOptions).subscribe((res: Response) => {
expect(res.text()).toBe('base response');
async.done();
});
}));
it('should perform a post request for given url if options include a method',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse);
});
const requestOptions = {method: RequestMethod.Post};
http.request('http://basic.connection', requestOptions).subscribe((res: Response) => {
expect(res.text()).toBe('base response');
async.done();
});
}));
it('should perform a get request and complete the response',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
http.request('http://basic.connection')
.subscribe(
(res: Response) => { expect(res.text()).toBe('base response'); }, null !,
() => { async.done(); });
}));
it('should perform multiple get requests and complete the responses',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
http.request('http://basic.connection').subscribe((res: Response) => {
expect(res.text()).toBe('base response');
});
http.request('http://basic.connection')
.subscribe(
(res: Response) => { expect(res.text()).toBe('base response'); }, null !,
() => { async.done(); });
}));
it('should throw if url is not a string or Request', () => {
const req = <Request>{};
expect(() => http.request(req))
.toThrowError('First argument must be a url string or Request instance.');
});
});
describe('.get()', () => {
it('should perform a get request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Get);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.get(url).subscribe((res: Response) => {});
}));
});
describe('.post()', () => {
it('should perform a post request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Post);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.post(url, 'post me').subscribe((res: Response) => {});
}));
it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const body = 'this is my post body';
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
http.post(url, body).subscribe((res: Response) => {});
}));
});
describe('.put()', () => {
it('should perform a put request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Put);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.put(url, 'put me').subscribe((res: Response) => {});
}));
it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const body = 'this is my put body';
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
http.put(url, body).subscribe((res: Response) => {});
}));
});
describe('.delete()', () => {
it('should perform a delete request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Delete);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.delete(url).subscribe((res: Response) => {});
}));
});
describe('.patch()', () => {
it('should perform a patch request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Patch);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.patch(url, 'this is my patch body').subscribe((res: Response) => {});
}));
it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const body = 'this is my patch body';
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
http.patch(url, body).subscribe((res: Response) => {});
}));
});
describe('.head()', () => {
it('should perform a head request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Head);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.head(url).subscribe((res: Response) => {});
}));
});
describe('.options()', () => {
it('should perform an options request for given url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Options);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections();
async.done();
});
expect(http.request).not.toHaveBeenCalled();
http.options(url).subscribe((res: Response) => {});
}));
});
describe('searchParams', () => {
it('should append search params to url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const params = new URLSearchParams();
params.append('q', 'puppies');
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toEqual('https://www.google.com?q=puppies');
backend.resolveAllConnections();
async.done();
});
http.get('https://www.google.com', new RequestOptions({search: params}))
.subscribe((res: Response) => {});
}));
it('should append string search params to url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toEqual('https://www.google.com?q=piggies');
backend.resolveAllConnections();
async.done();
});
http.get('https://www.google.com', new RequestOptions({search: 'q=piggies'}))
.subscribe((res: Response) => {});
}));
it('should produce valid url when url already contains a query',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toEqual('https://www.google.com?q=angular&as_eq=1.x');
backend.resolveAllConnections();
async.done();
});
http.get('https://www.google.com?q=angular', new RequestOptions({search: 'as_eq=1.x'}))
.subscribe((res: Response) => {});
}));
});
describe('params', () => {
it('should append params to url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.url).toEqual('https://www.google.com?q=puppies');
backend.resolveAllConnections();
async.done();
});
http.get('https://www.google.com', {params: {q: 'puppies'}})
.subscribe((res: Response) => {});
}));
});
describe('string method names', () => {
it('should allow case insensitive strings for method names', () => {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Post);
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
async.done();
});
http.request(
new Request(new RequestOptions({url: 'https://google.com', method: 'PosT'})))
.subscribe((res: Response) => {});
});
});
it('should throw when invalid string parameter is passed for method name', () => {
expect(() => {
http.request(
new Request(new RequestOptions({url: 'https://google.com', method: 'Invalid'})));
}).toThrowError('Invalid request method. The method "Invalid" is not supported.');
});
});
});
describe('Jsonp', () => {
describe('.request()', () => {
it('should throw if url is not a string or Request', () => {
const req = <Request>{};
expect(() => jsonp.request(req))
.toThrowError('First argument must be a url string or Request instance.');
});
});
});
describe('response buffer', () => {
it('should attach the provided buffer to the response',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.responseType).toBe(ResponseContentType.ArrayBuffer);
c.mockRespond(new Response(new ResponseOptions({body: new ArrayBuffer(32)})));
async.done();
});
http.get(
'https://www.google.com',
new RequestOptions({responseType: ResponseContentType.ArrayBuffer}))
.subscribe((res: Response) => {});
}));
it('should be able to consume a buffer containing a String as any response type',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => c.mockRespond(baseResponse));
http.get('https://www.google.com').subscribe((res: Response) => {
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
expect(res.text()).toBe('base response');
async.done();
});
}));
it('should be able to consume a buffer containing an ArrayBuffer as any response type',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const arrayBuffer = stringToArrayBuffer('{"response": "ok"}');
backend.connections.subscribe(
(c: MockConnection) =>
c.mockRespond(new Response(new ResponseOptions({body: arrayBuffer}))));
http.get('https://www.google.com').subscribe((res: Response) => {
expect(res.arrayBuffer()).toBe(arrayBuffer);
expect(res.text()).toEqual('{"response": "ok"}');
expect(res.json()).toEqual({response: 'ok'});
async.done();
});
}));
it('should be able to consume a buffer containing an Object as any response type',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const simpleObject = {'content': 'ok'};
backend.connections.subscribe(
(c: MockConnection) =>
c.mockRespond(new Response(new ResponseOptions({body: simpleObject}))));
http.get('https://www.google.com').subscribe((res: Response) => {
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
expect(res.text()).toEqual(JSON.stringify(simpleObject, null, 2));
expect(res.json()).toBe(simpleObject);
async.done();
});
}));
it('should preserve encoding of ArrayBuffer response',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const message = 'é@θЂ';
const arrayBuffer = stringToArrayBuffer(message);
backend.connections.subscribe(
(c: MockConnection) =>
c.mockRespond(new Response(new ResponseOptions({body: arrayBuffer}))));
http.get('https://www.google.com').subscribe((res: Response) => {
expect(res.arrayBuffer()).toBeAnInstanceOf(ArrayBuffer);
expect(res.text()).toEqual(message);
async.done();
});
}));
it('should preserve encoding of String response',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const message = 'é@θЂ';
backend.connections.subscribe(
(c: MockConnection) =>
c.mockRespond(new Response(new ResponseOptions({body: message}))));
http.get('https://www.google.com').subscribe((res: Response) => {
expect(res.arrayBuffer()).toEqual(stringToArrayBuffer(message));
async.done();
});
}));
it('should have an equivalent response independently of the buffer used',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
const message = {'param': 'content'};
backend.connections.subscribe((c: MockConnection) => {
const body = (): any => {
switch (c.request.responseType) {
case ResponseContentType.Text:
return JSON.stringify(message, null, 2);
case ResponseContentType.Json:
return message;
case ResponseContentType.ArrayBuffer:
return stringToArrayBuffer(JSON.stringify(message, null, 2));
}
};
c.mockRespond(new Response(new ResponseOptions({body: body()})));
});
zip(http.get(
'https://www.google.com',
new RequestOptions({responseType: ResponseContentType.Text})),
http.get(
'https://www.google.com',
new RequestOptions({responseType: ResponseContentType.Json})),
http.get(
'https://www.google.com',
new RequestOptions({responseType: ResponseContentType.ArrayBuffer})))
.subscribe((res: Array<any>) => {
expect(res[0].text()).toEqual(res[1].text());
expect(res[1].text()).toEqual(res[2].text());
async.done();
});
}));
});
});
}