From 70ffd267f8d069dbaafef488533701cbc7535224 Mon Sep 17 00:00:00 2001 From: Jeff Cross Date: Sat, 13 Jun 2015 16:44:32 -0700 Subject: [PATCH] refactor(Http): implement Request object parameter for http.request Fixes #2416 --- modules/angular2/src/http/http.ts | 16 ++++++++++++---- modules/angular2/test/http/http_spec.ts | 24 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/modules/angular2/src/http/http.ts b/modules/angular2/src/http/http.ts index 7d257921a4..c1bb3a7e27 100644 --- a/modules/angular2/src/http/http.ts +++ b/modules/angular2/src/http/http.ts @@ -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 { - return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options))); + request(url: string|Request, options?: RequestOptions): Rx.Observable { + 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 httpRequest(backend, new Request(url, defaultOptions.merge(options))); + 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); + } } } diff --git a/modules/angular2/test/http/http_spec.ts b/modules/angular2/test/http/http_spec.ts index 14153d9942..9860a0e8a0 100644 --- a/modules/angular2/test/http/http_spec.ts +++ b/modules/angular2/test/http/http_spec.ts @@ -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,9 +115,20 @@ export function main() { describe('Http', () => { - it('should return an Observable', () => { - expect(typeof http.request(url).subscribe).toBe('function'); - backend.resolveAllConnections(); + 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(() =>{}); + }); });