angular-cn/modules/@angular/http/test/http_spec.ts

384 lines
15 KiB
TypeScript
Raw Normal View History

/**
* @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, ReflectiveInjector, provide} from '@angular/core';
import {afterEach, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
import {AsyncTestCompleter} from '@angular/core/testing/testing_internal';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
import {BaseRequestOptions, ConnectionBackend, HTTP_PROVIDERS, Http, JSONPBackend, JSONP_PROVIDERS, Jsonp, Request, RequestMethod, RequestOptions, Response, ResponseOptions, URLSearchParams, XHRBackend} from '../http';
import {MockBackend, MockConnection} from '../testing/mock_backend';
export function main() {
describe('injectables', () => {
var url = 'http://foo.bar';
var http: Http;
var parentInjector: ReflectiveInjector;
var childInjector: ReflectiveInjector;
var jsonpBackend: MockBackend;
var xhrBackend: MockBackend;
var jsonp: Jsonp;
it('should allow using jsonpInjectables and httpInjectables in same injector',
2016-02-01 20:05:50 -05:00
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
parentInjector = ReflectiveInjector.resolveAndCreate([
{provide: XHRBackend, useClass: MockBackend},
{provide: JSONPBackend, useClass: MockBackend}
]);
childInjector = parentInjector.resolveAndCreateChild([
HTTP_PROVIDERS, JSONP_PROVIDERS, {provide: XHRBackend, useClass: MockBackend},
{provide: JSONPBackend, useClass: MockBackend}
]);
http = childInjector.get(Http);
jsonp = childInjector.get(Jsonp);
jsonpBackend = childInjector.get(JSONPBackend);
xhrBackend = childInjector.get(XHRBackend);
var xhrCreatedConnections = 0;
var 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', () => {
var url = 'http://foo.bar';
var http: Http;
var injector: Injector;
var backend: MockBackend;
2016-02-01 20:05:50 -05:00
var baseResponse: Response;
var jsonp: Jsonp;
beforeEach(() => {
injector = ReflectiveInjector.resolveAndCreate([
BaseRequestOptions, MockBackend, {
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]
}
2015-06-13 18:49:05 -04:00
]);
http = injector.get(Http);
jsonp = injector.get(Jsonp);
backend = injector.get(MockBackend);
baseResponse = new Response(new ResponseOptions({body: 'base response'}));
});
2015-06-13 18:49:05 -04:00
afterEach(() => backend.verifyNoPendingRequests());
2015-06-13 18:49:05 -04:00
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',
2016-02-01 20:05:50 -05:00
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'})))
2016-02-01 20:05:50 -05:00
.subscribe((res: Response) => {});
}));
2015-06-13 18:49:05 -04:00
it('should accept a fully-qualified request as its only parameter',
2016-02-01 20:05:50 -05:00
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})))
2016-02-01 20:05:50 -05:00
.subscribe((res: Response) => {});
}));
2015-06-13 18:49:05 -04:00
it('should perform a get request for given url if only passed a string',
2016-02-01 20:05:50 -05:00
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',
2016-02-01 20:05:50 -05:00
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse);
});
let 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',
2016-02-01 20:05:50 -05:00
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse);
});
let 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',
2016-02-01 20:05:50 -05:00
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(); });
}));
2015-06-13 18:49:05 -04:00
it('should perform multiple get requests and complete the responses',
2016-02-01 20:05:50 -05:00
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', () => {
var req = <Request>{};
expect(() => http.request(req))
.toThrowError('First argument must be a url string or Request instance.');
});
});
2015-06-13 18:49:05 -04:00
describe('.get()', () => {
2016-02-01 20:05:50 -05:00
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);
2015-06-13 18:49:05 -04:00
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.get(url).subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
});
describe('.post()', () => {
2016-02-01 20:05:50 -05:00
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);
2015-06-13 18:49:05 -04:00
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.post(url, 'post me').subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
2016-02-01 20:05:50 -05:00
it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var body = 'this is my post body';
2016-02-01 20:05:50 -05:00
backend.connections.subscribe((c: MockConnection) => {
2015-06-13 18:49:05 -04:00
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.post(url, body).subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
});
describe('.put()', () => {
2016-02-01 20:05:50 -05:00
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);
2015-06-13 18:49:05 -04:00
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.put(url, 'put me').subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
2016-02-01 20:05:50 -05:00
it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
2015-06-13 18:49:05 -04:00
var body = 'this is my put body';
2016-02-01 20:05:50 -05:00
backend.connections.subscribe((c: MockConnection) => {
2015-06-13 18:49:05 -04:00
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.put(url, body).subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
});
describe('.delete()', () => {
2016-02-01 20:05:50 -05:00
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);
2015-06-13 18:49:05 -04:00
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.delete(url).subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
});
describe('.patch()', () => {
2016-02-01 20:05:50 -05:00
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);
2015-06-13 18:49:05 -04:00
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.patch(url, 'this is my patch body').subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
2016-02-01 20:05:50 -05:00
it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var body = 'this is my patch body';
2016-02-01 20:05:50 -05:00
backend.connections.subscribe((c: MockConnection) => {
2015-06-13 18:49:05 -04:00
expect(c.request.text()).toBe(body);
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.patch(url, body).subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
});
2015-06-13 18:49:05 -04:00
describe('.head()', () => {
2016-02-01 20:05:50 -05:00
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);
2015-06-13 18:49:05 -04:00
backend.resolveAllConnections();
async.done();
});
2016-02-01 20:05:50 -05:00
http.head(url).subscribe((res: Response) => {});
2015-06-13 18:49:05 -04:00
}));
});
describe('searchParams', () => {
2016-02-01 20:05:50 -05:00
it('should append search params to url',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var params = new URLSearchParams();
params.append('q', 'puppies');
2016-02-01 20:05:50 -05:00
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}))
2016-02-01 20:05:50 -05:00
.subscribe((res: Response) => {});
}));
2016-02-01 20:05:50 -05:00
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'}))
2016-02-01 20:05:50 -05:00
.subscribe((res: Response) => {});
}));
it('should produce valid url when url already contains a query',
2016-02-01 20:05:50 -05:00
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'}))
2016-02-01 20:05:50 -05:00
.subscribe((res: Response) => {});
}));
});
describe('string method names', () => {
it('should allow case insensitive strings for method names', () => {
2016-02-01 20:05:50 -05:00
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'})))
2016-02-01 20:05:50 -05:00
.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.');
});
});
2015-06-13 18:49:05 -04:00
});
describe('Jsonp', () => {
describe('.request()', () => {
it('should throw if url is not a string or Request', () => {
var req = <Request>{};
expect(() => jsonp.request(req))
.toThrowError('First argument must be a url string or Request instance.');
});
});
});
});
}