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> {
|
||||||
|
if (typeof url === 'string') {
|
||||||
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
|
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) {
|
||||||
|
if (typeof url === 'string') {
|
||||||
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
|
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,12 +115,23 @@ export function main() {
|
||||||
|
|
||||||
|
|
||||||
describe('Http', () => {
|
describe('Http', () => {
|
||||||
|
describe('.request()', () => {
|
||||||
it('should return an Observable', () => {
|
it('should return an Observable', () => {
|
||||||
expect(typeof http.request(url).subscribe).toBe('function');
|
expect(typeof http.request(url).subscribe).toBe('function');
|
||||||
backend.resolveAllConnections();
|
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',
|
it('should perform a get request for given url if only passed a string',
|
||||||
inject([AsyncTestCompleter], (async) => {
|
inject([AsyncTestCompleter], (async) => {
|
||||||
var connection;
|
var connection;
|
||||||
|
|
Loading…
Reference in New Issue