refactor(Http): implement Request object parameter for http.request
Fixes #2416
This commit is contained in:
parent
b68e561c0f
commit
70ffd267f8
|
@ -61,8 +61,12 @@ function httpRequest(backend: XHRBackend, request: Request) {
|
||||||
export class Http {
|
export class Http {
|
||||||
constructor(private backend: XHRBackend, private defaultOptions: BaseRequestOptions) {}
|
constructor(private backend: XHRBackend, private defaultOptions: BaseRequestOptions) {}
|
||||||
|
|
||||||
request(url: string, options?: RequestOptions): Rx.Observable<Response> {
|
request(url: string|Request, options?: RequestOptions): Rx.Observable<Response> {
|
||||||
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
|
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) {
|
get(url: string, options?: RequestOptions) {
|
||||||
|
@ -107,7 +111,11 @@ if (Rx.hasOwnProperty('default')) {
|
||||||
Observable = Rx.Observable;
|
Observable = Rx.Observable;
|
||||||
}
|
}
|
||||||
export function HttpFactory(backend: XHRBackend, defaultOptions: BaseRequestOptions) {
|
export function HttpFactory(backend: XHRBackend, defaultOptions: BaseRequestOptions) {
|
||||||
return function(url: string, options?: RequestOptions) {
|
return function(url: string | Request, options?: RequestOptions) {
|
||||||
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
|
if (typeof url === 'string') {
|
||||||
|
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
|
||||||
|
} else if (url instanceof Request) {
|
||||||
|
return httpRequest(backend, url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import {MockBackend} from 'angular2/src/http/backends/mock_backend';
|
||||||
import {Response} from 'angular2/src/http/static_response';
|
import {Response} from 'angular2/src/http/static_response';
|
||||||
import {RequestMethods} from 'angular2/src/http/enums';
|
import {RequestMethods} from 'angular2/src/http/enums';
|
||||||
import {BaseRequestOptions} from 'angular2/src/http/base_request_options';
|
import {BaseRequestOptions} from 'angular2/src/http/base_request_options';
|
||||||
|
import {Request} from 'angular2/src/http/static_request';
|
||||||
|
|
||||||
class SpyObserver extends SpyObject {
|
class SpyObserver extends SpyObject {
|
||||||
onNext: Function;
|
onNext: Function;
|
||||||
|
@ -79,6 +80,12 @@ export function main() {
|
||||||
connection.mockRespond(baseResponse)
|
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',
|
it('should perform a get request for given url if passed a ConnectionConfig instance',
|
||||||
inject([AsyncTestCompleter], async => {
|
inject([AsyncTestCompleter], async => {
|
||||||
|
@ -108,9 +115,20 @@ export function main() {
|
||||||
|
|
||||||
|
|
||||||
describe('Http', () => {
|
describe('Http', () => {
|
||||||
it('should return an Observable', () => {
|
describe('.request()', () => {
|
||||||
expect(typeof http.request(url).subscribe).toBe('function');
|
it('should return an Observable', () => {
|
||||||
backend.resolveAllConnections();
|
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(() =>{});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue