refactor(http): use Http.request for all http shorthand methods (#12319)

This commit is contained in:
Dzmitry Shylovich 2016-10-18 09:04:25 +03:00 committed by Igor Minar
parent d55f747858
commit 8603d9c269
2 changed files with 43 additions and 36 deletions

View File

@ -8,9 +8,7 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {isPresent, isString} from '../src/facade/lang'; import {isPresent, isString} from '../src/facade/lang';
import {BaseRequestOptions, RequestOptions} from './base_request_options'; import {BaseRequestOptions, RequestOptions} from './base_request_options';
import {RequestMethod} from './enums'; import {RequestMethod} from './enums';
import {ConnectionBackend, RequestOptionsArgs} from './interfaces'; import {ConnectionBackend, RequestOptionsArgs} from './interfaces';
@ -24,7 +22,7 @@ function httpRequest(backend: ConnectionBackend, request: Request): Observable<R
function mergeOptions( function mergeOptions(
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod, defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod,
url: string): RequestOptions { url: string): RequestOptions {
var newOptions = defaultOpts; const newOptions = defaultOpts;
if (isPresent(providedOpts)) { if (isPresent(providedOpts)) {
// Hack so Dart can used named parameters // Hack so Dart can used named parameters
return newOptions.merge(new RequestOptions({ return newOptions.merge(new RequestOptions({
@ -115,7 +113,7 @@ export class Http {
* of {@link BaseRequestOptions} before performing the request. * of {@link BaseRequestOptions} before performing the request.
*/ */
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> { request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
var responseObservable: any; let responseObservable: any;
if (isString(url)) { if (isString(url)) {
responseObservable = httpRequest( responseObservable = httpRequest(
this._backend, this._backend,
@ -132,8 +130,7 @@ export class Http {
* Performs a request with `get` http method. * Performs a request with `get` http method.
*/ */
get(url: string, options?: RequestOptionsArgs): Observable<Response> { get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(
this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url))); new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
} }
@ -141,28 +138,25 @@ export class Http {
* Performs a request with `post` http method. * Performs a request with `post` http method.
*/ */
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(new Request(mergeOptions(
this._backend, new Request(mergeOptions( this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Post,
this._defaultOptions.merge(new RequestOptions({body: body})), options, url)));
RequestMethod.Post, url)));
} }
/** /**
* Performs a request with `put` http method. * Performs a request with `put` http method.
*/ */
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(new Request(mergeOptions(
this._backend, new Request(mergeOptions( this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Put,
this._defaultOptions.merge(new RequestOptions({body: body})), options, url)));
RequestMethod.Put, url)));
} }
/** /**
* Performs a request with `delete` http method. * Performs a request with `delete` http method.
*/ */
delete (url: string, options?: RequestOptionsArgs): Observable<Response> { delete (url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(
this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url))); new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url)));
} }
@ -170,18 +164,16 @@ export class Http {
* Performs a request with `patch` http method. * Performs a request with `patch` http method.
*/ */
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> { patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(new Request(mergeOptions(
this._backend, new Request(mergeOptions( this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Patch,
this._defaultOptions.merge(new RequestOptions({body: body})), options, url)));
RequestMethod.Patch, url)));
} }
/** /**
* Performs a request with `head` http method. * Performs a request with `head` http method.
*/ */
head(url: string, options?: RequestOptionsArgs): Observable<Response> { head(url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(
this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url))); new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url)));
} }
@ -189,8 +181,7 @@ export class Http {
* Performs a request with `options` http method. * Performs a request with `options` http method.
*/ */
options(url: string, options?: RequestOptionsArgs): Observable<Response> { options(url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return this.request(
this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url))); new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url)));
} }
} }
@ -220,7 +211,7 @@ export class Jsonp extends Http {
* [Security Guide](http://g.co/ng/security). * [Security Guide](http://g.co/ng/security).
*/ */
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> { request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
var responseObservable: any; let responseObservable: any;
if (isString(url)) { if (isString(url)) {
url = url =
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)); new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url));

View File

@ -72,12 +72,13 @@ export function main() {
}); });
describe('http', () => { describe('http', () => {
var url = 'http://foo.bar'; let url = 'http://foo.bar';
var http: Http; let http: Http;
var injector: Injector; let injector: Injector;
var backend: MockBackend; let backend: MockBackend;
var baseResponse: Response; let baseResponse: Response;
var jsonp: Jsonp; let jsonp: Jsonp;
beforeEach(() => { beforeEach(() => {
injector = ReflectiveInjector.resolveAndCreate([ injector = ReflectiveInjector.resolveAndCreate([
BaseRequestOptions, MockBackend, { BaseRequestOptions, MockBackend, {
@ -99,6 +100,7 @@ export function main() {
jsonp = injector.get(Jsonp); jsonp = injector.get(Jsonp);
backend = injector.get(MockBackend); backend = injector.get(MockBackend);
baseResponse = new Response(new ResponseOptions({body: 'base response'})); baseResponse = new Response(new ResponseOptions({body: 'base response'}));
spyOn(Http.prototype, 'request').and.callThrough();
}); });
afterEach(() => backend.verifyNoPendingRequests()); afterEach(() => backend.verifyNoPendingRequests());
@ -149,7 +151,7 @@ export function main() {
expect(c.request.method).toEqual(RequestMethod.Post); expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse); c.mockRespond(baseResponse);
}); });
let requestOptions = new RequestOptions({method: RequestMethod.Post}); const requestOptions = new RequestOptions({method: RequestMethod.Post});
http.request('http://basic.connection', requestOptions).subscribe((res: Response) => { http.request('http://basic.connection', requestOptions).subscribe((res: Response) => {
expect(res.text()).toBe('base response'); expect(res.text()).toBe('base response');
async.done(); async.done();
@ -192,7 +194,7 @@ export function main() {
})); }));
it('should throw if url is not a string or Request', () => { it('should throw if url is not a string or Request', () => {
var req = <Request>{}; const req = <Request>{};
expect(() => http.request(req)) expect(() => http.request(req))
.toThrowError('First argument must be a url string or Request instance.'); .toThrowError('First argument must be a url string or Request instance.');
}); });
@ -204,9 +206,11 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Get); expect(c.request.method).toBe(RequestMethod.Get);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.get(url).subscribe((res: Response) => {}); http.get(url).subscribe((res: Response) => {});
})); }));
}); });
@ -217,16 +221,18 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Post); expect(c.request.method).toBe(RequestMethod.Post);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.post(url, 'post me').subscribe((res: Response) => {}); http.post(url, 'post me').subscribe((res: Response) => {});
})); }));
it('should attach the provided body to the request', it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var body = 'this is my post body'; const body = 'this is my post body';
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.text()).toBe(body); expect(c.request.text()).toBe(body);
backend.resolveAllConnections(); backend.resolveAllConnections();
@ -242,15 +248,17 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Put); expect(c.request.method).toBe(RequestMethod.Put);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.put(url, 'put me').subscribe((res: Response) => {}); http.put(url, 'put me').subscribe((res: Response) => {});
})); }));
it('should attach the provided body to the request', it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var body = 'this is my put body'; const body = 'this is my put body';
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.text()).toBe(body); expect(c.request.text()).toBe(body);
backend.resolveAllConnections(); backend.resolveAllConnections();
@ -266,9 +274,11 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Delete); expect(c.request.method).toBe(RequestMethod.Delete);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.delete(url).subscribe((res: Response) => {}); http.delete(url).subscribe((res: Response) => {});
})); }));
}); });
@ -279,15 +289,17 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Patch); expect(c.request.method).toBe(RequestMethod.Patch);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.patch(url, 'this is my patch body').subscribe((res: Response) => {}); http.patch(url, 'this is my patch body').subscribe((res: Response) => {});
})); }));
it('should attach the provided body to the request', it('should attach the provided body to the request',
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
var body = 'this is my patch body'; const body = 'this is my patch body';
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.text()).toBe(body); expect(c.request.text()).toBe(body);
backend.resolveAllConnections(); backend.resolveAllConnections();
@ -303,9 +315,11 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Head); expect(c.request.method).toBe(RequestMethod.Head);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.head(url).subscribe((res: Response) => {}); http.head(url).subscribe((res: Response) => {});
})); }));
}); });
@ -316,9 +330,11 @@ export function main() {
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
backend.connections.subscribe((c: MockConnection) => { backend.connections.subscribe((c: MockConnection) => {
expect(c.request.method).toBe(RequestMethod.Options); expect(c.request.method).toBe(RequestMethod.Options);
expect(http.request).toHaveBeenCalled();
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
expect(http.request).not.toHaveBeenCalled();
http.options(url).subscribe((res: Response) => {}); http.options(url).subscribe((res: Response) => {});
})); }));
}); });