refactor(Http): implement Request object parameter for http.request

Fixes #2416
This commit is contained in:
Jeff Cross 2015-06-13 16:44:32 -07:00
parent b68e561c0f
commit 70ffd267f8
2 changed files with 33 additions and 7 deletions

View File

@ -61,8 +61,12 @@ function httpRequest(backend: XHRBackend, request: Request) {
export class Http {
constructor(private backend: XHRBackend, private defaultOptions: BaseRequestOptions) {}
request(url: string, options?: RequestOptions): Rx.Observable<Response> {
request(url: string|Request, options?: RequestOptions): Rx.Observable<Response> {
if (typeof url === 'string') {
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
} else if (url instanceof Request) {
return httpRequest(this.backend, url);
}
}
get(url: string, options?: RequestOptions) {
@ -107,7 +111,11 @@ if (Rx.hasOwnProperty('default')) {
Observable = Rx.Observable;
}
export function HttpFactory(backend: XHRBackend, defaultOptions: BaseRequestOptions) {
return function(url: string, options?: RequestOptions) {
return function(url: string | Request, options?: RequestOptions) {
if (typeof url === 'string') {
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
} else if (url instanceof Request) {
return httpRequest(backend, url);
}
}
}

View File

@ -17,6 +17,7 @@ import {MockBackend} from 'angular2/src/http/backends/mock_backend';
import {Response} from 'angular2/src/http/static_response';
import {RequestMethods} from 'angular2/src/http/enums';
import {BaseRequestOptions} from 'angular2/src/http/base_request_options';
import {Request} from 'angular2/src/http/static_request';
class SpyObserver extends SpyObject {
onNext: Function;
@ -79,6 +80,12 @@ export function main() {
connection.mockRespond(baseResponse)
}));
it('should accept a fully-qualified request as its only parameter', () => {
var req = new Request('https://google.com');
backend.connections.subscribe(c => { expect(c.request.url).toBe('https://google.com'); });
httpFactory(req).subscribe(() => {});
});
it('should perform a get request for given url if passed a ConnectionConfig instance',
inject([AsyncTestCompleter], async => {
@ -108,12 +115,23 @@ export function main() {
describe('Http', () => {
describe('.request()', () => {
it('should return an Observable', () => {
expect(typeof http.request(url).subscribe).toBe('function');
backend.resolveAllConnections();
});
it('should accept a fully-qualified request as its only parameter', () => {
var req = new Request('https://google.com');
backend.connections.subscribe(c => {
expect(c.request.url).toBe('https://google.com');
});
http.request(req).subscribe(() =>{});
});
});
it('should perform a get request for given url if only passed a string',
inject([AsyncTestCompleter], (async) => {
var connection;