From e1d7bdcfe7f25156c8a462452db5367b68a02df6 Mon Sep 17 00:00:00 2001 From: Rob Wormald Date: Thu, 19 Nov 2015 19:54:23 -0800 Subject: [PATCH] fix(http): Fix all requests defaulting to Get Honor method parameter passed to http.request(). Closes #5309 Closes #5397 --- modules/angular2/src/http/http.ts | 6 +-- modules/angular2/test/http/http_spec.ts | 53 +++++++++++++++++++------ 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/modules/angular2/src/http/http.ts b/modules/angular2/src/http/http.ts index bba1d73e3d..09f4815999 100644 --- a/modules/angular2/src/http/http.ts +++ b/modules/angular2/src/http/http.ts @@ -16,9 +16,9 @@ function mergeOptions(defaultOpts, providedOpts, method, url): RequestOptions { var newOptions = defaultOpts; if (isPresent(providedOpts)) { // Hack so Dart can used named parameters - newOptions = newOptions.merge(new RequestOptions({ - method: providedOpts.method, - url: providedOpts.url, + return newOptions.merge(new RequestOptions({ + method: providedOpts.method || method, + url: providedOpts.url || url, search: providedOpts.search, headers: providedOpts.headers, body: providedOpts.body diff --git a/modules/angular2/test/http/http_spec.ts b/modules/angular2/test/http/http_spec.ts index 1c5fd71f57..f67bc03f09 100644 --- a/modules/angular2/test/http/http_spec.ts +++ b/modules/angular2/test/http/http_spec.ts @@ -151,6 +151,19 @@ export function main() { .subscribe((res) => {}); })); + it('should accept a fully-qualified request as its only parameter', + inject([AsyncTestCompleter], (async) => { + backend.connections.subscribe(c => { + expect(c.request.url).toBe('https://google.com'); + expect(c.request.method).toBe(RequestMethods.Post); + c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'}))); + async.done(); + }); + http.request(new Request(new RequestOptions( + {url: 'https://google.com', method: RequestMethods.Post}))) + .subscribe((res) => {}); + })); + it('should perform a get request for given url if only passed a string', inject([AsyncTestCompleter], (async) => { @@ -162,6 +175,34 @@ export function main() { }); })); + it('should perform a post request for given url if options include a method', + inject([AsyncTestCompleter], (async) => { + backend.connections.subscribe(c => { + expect(c.request.method).toEqual(RequestMethods.Post); + c.mockRespond(baseResponse); + }); + let requestOptions = new RequestOptions({method: RequestMethods.Post}); + http.request('http://basic.connection', requestOptions) + .subscribe(res => { + expect(res.text()).toBe('base response'); + async.done(); + }); + })); + + it('should perform a post request for given url if options include a method', + inject([AsyncTestCompleter], (async) => { + backend.connections.subscribe(c => { + expect(c.request.method).toEqual(RequestMethods.Post); + c.mockRespond(baseResponse); + }); + let requestOptions = {method: RequestMethods.Post}; + http.request('http://basic.connection', requestOptions) + .subscribe(res => { + expect(res.text()).toBe('base response'); + async.done(); + }); + })); + it('should perform a get request and complete the response', inject([AsyncTestCompleter], (async) => { backend.connections.subscribe(c => c.mockRespond(baseResponse)); @@ -180,18 +221,6 @@ export function main() { .subscribe(res => { expect(res.text()).toBe('base response'); }, null, () => { async.done(); }); })); - // TODO: make dart not complain about "argument type 'Map' cannot be assigned to the - // parameter type 'IRequestOptions'" - // xit('should perform a get request for given url if passed a dictionary', - // inject([AsyncTestCompleter], async => { - // ObservableWrapper.subscribe(backend.connections, c => c.mockRespond(baseResponse)); - // ObservableWrapper.subscribe(http.request(url, {method: RequestMethods.GET}), res => - // { - // expect(res.text()).toBe('base response'); - // async.done(); - // }); - // })); - it('should throw if url is not a string or Request', () => { var req = {};