2017-03-22 20:13:24 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2018-07-05 11:10:09 -04:00
|
|
|
import {ddescribe, describe, fit, it} from '@angular/core/testing/src/testing_internal';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {toArray} from 'rxjs/operators';
|
2017-03-22 20:13:24 -04:00
|
|
|
|
|
|
|
import {HttpClient} from '../src/client';
|
2017-07-17 15:16:27 -04:00
|
|
|
import {HttpErrorResponse, HttpEventType, HttpResponse} from '../src/response';
|
2017-03-22 20:13:24 -04:00
|
|
|
import {HttpClientTestingBackend} from '../testing/src/backend';
|
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2017-03-22 20:13:24 -04:00
|
|
|
describe('HttpClient', () => {
|
|
|
|
let client: HttpClient = null !;
|
|
|
|
let backend: HttpClientTestingBackend = null !;
|
|
|
|
beforeEach(() => {
|
|
|
|
backend = new HttpClientTestingBackend();
|
|
|
|
client = new HttpClient(backend);
|
|
|
|
});
|
|
|
|
afterEach(() => { backend.verify(); });
|
|
|
|
describe('makes a basic request', () => {
|
2018-07-05 11:10:09 -04:00
|
|
|
it('for JSON data', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
client.get('/test').subscribe(res => {
|
|
|
|
expect((res as any)['data']).toEqual('hello world');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush({'data': 'hello world'});
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('for text data', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
client.get('/test', {responseType: 'text'}).subscribe(res => {
|
|
|
|
expect(res).toEqual('hello world');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush('hello world');
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with headers', done => {
|
2017-08-01 18:43:39 -04:00
|
|
|
client.get('/test', {headers: {'X-Option': 'true'}}).subscribe(() => done());
|
|
|
|
const req = backend.expectOne('/test');
|
|
|
|
expect(req.request.headers.get('X-Option')).toEqual('true');
|
|
|
|
req.flush({});
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with params', done => {
|
2017-08-01 18:43:39 -04:00
|
|
|
client.get('/test', {params: {'test': 'true'}}).subscribe(() => done());
|
|
|
|
backend.expectOne('/test?test=true').flush({});
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('for an arraybuffer', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
const body = new ArrayBuffer(4);
|
|
|
|
client.get('/test', {responseType: 'arraybuffer'}).subscribe(res => {
|
|
|
|
expect(res).toBe(body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush(body);
|
|
|
|
});
|
|
|
|
if (typeof Blob !== 'undefined') {
|
2018-07-05 11:10:09 -04:00
|
|
|
it('for a blob', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
const body = new Blob([new ArrayBuffer(4)]);
|
|
|
|
client.get('/test', {responseType: 'blob'}).subscribe(res => {
|
|
|
|
expect(res).toBe(body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush(body);
|
|
|
|
});
|
|
|
|
}
|
2018-07-05 11:10:09 -04:00
|
|
|
it('that returns a response', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
const body = {'data': 'hello world'};
|
|
|
|
client.get('/test', {observe: 'response'}).subscribe(res => {
|
|
|
|
expect(res instanceof HttpResponse).toBe(true);
|
|
|
|
expect(res.body).toBe(body);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush(body);
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('that returns a stream of events', done => {
|
2018-02-27 17:06:06 -05:00
|
|
|
client.get('/test', {observe: 'events'}).pipe(toArray()).toPromise().then(events => {
|
2017-03-22 20:13:24 -04:00
|
|
|
expect(events.length).toBe(2);
|
2017-08-01 18:43:39 -04:00
|
|
|
let x = HttpResponse;
|
2017-03-22 20:13:24 -04:00
|
|
|
expect(events[0].type).toBe(HttpEventType.Sent);
|
|
|
|
expect(events[1].type).toBe(HttpEventType.Response);
|
|
|
|
expect(events[1] instanceof HttpResponse).toBeTruthy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush({'data': 'hello world'});
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with progress events enabled', done => {
|
2017-07-13 19:19:52 -04:00
|
|
|
client.get('/test', {reportProgress: true}).subscribe(() => done());
|
|
|
|
const req = backend.expectOne('/test');
|
|
|
|
expect(req.request.reportProgress).toEqual(true);
|
|
|
|
req.flush({});
|
|
|
|
});
|
2017-03-22 20:13:24 -04:00
|
|
|
});
|
|
|
|
describe('makes a POST request', () => {
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with text data', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
client.post('/test', 'text body', {observe: 'response', responseType: 'text'})
|
|
|
|
.subscribe(res => {
|
|
|
|
expect(res.ok).toBeTruthy();
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush('hello world');
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with json data', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
const body = {data: 'json body'};
|
|
|
|
client.post('/test', body, {observe: 'response', responseType: 'text'}).subscribe(res => {
|
|
|
|
expect(res.ok).toBeTruthy();
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
const testReq = backend.expectOne('/test');
|
|
|
|
expect(testReq.request.body).toBe(body);
|
|
|
|
testReq.flush('hello world');
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with a json body of false', done => {
|
2017-10-25 18:55:36 -04:00
|
|
|
client.post('/test', false, {observe: 'response', responseType: 'text'}).subscribe(res => {
|
|
|
|
expect(res.ok).toBeTruthy();
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
const testReq = backend.expectOne('/test');
|
|
|
|
expect(testReq.request.body).toBe(false);
|
|
|
|
testReq.flush('hello world');
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with a json body of 0', done => {
|
2017-10-25 18:55:36 -04:00
|
|
|
client.post('/test', 0, {observe: 'response', responseType: 'text'}).subscribe(res => {
|
|
|
|
expect(res.ok).toBeTruthy();
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
const testReq = backend.expectOne('/test');
|
|
|
|
expect(testReq.request.body).toBe(0);
|
|
|
|
testReq.flush('hello world');
|
|
|
|
});
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with an arraybuffer', done => {
|
2017-03-22 20:13:24 -04:00
|
|
|
const body = new ArrayBuffer(4);
|
|
|
|
client.post('/test', body, {observe: 'response', responseType: 'text'}).subscribe(res => {
|
|
|
|
expect(res.ok).toBeTruthy();
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
const testReq = backend.expectOne('/test');
|
|
|
|
expect(testReq.request.body).toBe(body);
|
|
|
|
testReq.flush('hello world');
|
|
|
|
});
|
|
|
|
});
|
2017-07-07 17:56:36 -04:00
|
|
|
describe('makes a JSONP request', () => {
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with properly set method and callback', done => {
|
2017-07-07 17:56:36 -04:00
|
|
|
client.jsonp('/test', 'myCallback').subscribe(() => done());
|
|
|
|
backend.expectOne({method: 'JSONP', url: '/test?myCallback=JSONP_CALLBACK'})
|
|
|
|
.flush('hello world');
|
|
|
|
});
|
|
|
|
});
|
2017-07-17 15:16:27 -04:00
|
|
|
describe('makes a request for an error response', () => {
|
2018-07-05 11:10:09 -04:00
|
|
|
it('with a JSON body', done => {
|
2017-07-17 15:16:27 -04:00
|
|
|
client.get('/test').subscribe(() => {}, (res: HttpErrorResponse) => {
|
|
|
|
expect(res.error.data).toEqual('hello world');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
backend.expectOne('/test').flush(
|
|
|
|
{'data': 'hello world'}, {status: 500, statusText: 'Server error'});
|
|
|
|
});
|
|
|
|
});
|
2017-03-22 20:13:24 -04:00
|
|
|
});
|
2017-07-17 15:16:27 -04:00
|
|
|
}
|