Skip to content

Commit 70ffd26

Browse files
committed
refactor(Http): implement Request object parameter for http.request
Fixes angular#2416
1 parent b68e561 commit 70ffd26

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

modules/angular2/src/http/http.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ function httpRequest(backend: XHRBackend, request: Request) {
6161
export class Http {
6262
constructor(private backend: XHRBackend, private defaultOptions: BaseRequestOptions) {}
6363

64-
request(url: string, options?: RequestOptions): Rx.Observable<Response> {
65-
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
64+
request(url: string|Request, options?: RequestOptions): Rx.Observable<Response> {
65+
if (typeof url === 'string') {
66+
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
67+
} else if (url instanceof Request) {
68+
return httpRequest(this.backend, url);
69+
}
6670
}
6771

6872
get(url: string, options?: RequestOptions) {
@@ -107,7 +111,11 @@ if (Rx.hasOwnProperty('default')) {
107111
Observable = Rx.Observable;
108112
}
109113
export function HttpFactory(backend: XHRBackend, defaultOptions: BaseRequestOptions) {
110-
return function(url: string, options?: RequestOptions) {
111-
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
114+
return function(url: string | Request, options?: RequestOptions) {
115+
if (typeof url === 'string') {
116+
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
117+
} else if (url instanceof Request) {
118+
return httpRequest(backend, url);
119+
}
112120
}
113121
}

modules/angular2/test/http/http_spec.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {MockBackend} from 'angular2/src/http/backends/mock_backend';
1717
import {Response} from 'angular2/src/http/static_response';
1818
import {RequestMethods} from 'angular2/src/http/enums';
1919
import {BaseRequestOptions} from 'angular2/src/http/base_request_options';
20+
import {Request} from 'angular2/src/http/static_request';
2021

2122
class SpyObserver extends SpyObject {
2223
onNext: Function;
@@ -79,6 +80,12 @@ export function main() {
7980
connection.mockRespond(baseResponse)
8081
}));
8182

83+
it('should accept a fully-qualified request as its only parameter', () => {
84+
var req = new Request('https://google.com');
85+
backend.connections.subscribe(c => { expect(c.request.url).toBe('https://google.com'); });
86+
httpFactory(req).subscribe(() => {});
87+
});
88+
8289

8390
it('should perform a get request for given url if passed a ConnectionConfig instance',
8491
inject([AsyncTestCompleter], async => {
@@ -108,9 +115,20 @@ export function main() {
108115

109116

110117
describe('Http', () => {
111-
it('should return an Observable', () => {
112-
expect(typeof http.request(url).subscribe).toBe('function');
113-
backend.resolveAllConnections();
118+
describe('.request()', () => {
119+
it('should return an Observable', () => {
120+
expect(typeof http.request(url).subscribe).toBe('function');
121+
backend.resolveAllConnections();
122+
});
123+
124+
125+
it('should accept a fully-qualified request as its only parameter', () => {
126+
var req = new Request('https://google.com');
127+
backend.connections.subscribe(c => {
128+
expect(c.request.url).toBe('https://google.com');
129+
});
130+
http.request(req).subscribe(() =>{});
131+
});
114132
});
115133

116134

0 commit comments

Comments
 (0)