From ee6f85abacc73d52bc0bb95f34f45a5a3d6eddc1 Mon Sep 17 00:00:00 2001 From: mgechev Date: Mon, 21 Sep 2015 10:46:16 +0300 Subject: [PATCH] feat(http): Add support for strings as http method names --- .../angular2/src/http/base_request_options.ts | 7 +++--- modules/angular2/src/http/http_utils.ts | 16 +++++++++++++ modules/angular2/src/http/interfaces.ts | 2 +- modules/angular2/src/http/static_request.ts | 4 ++-- modules/angular2/test/http/http_spec.ts | 23 +++++++++++++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/modules/angular2/src/http/base_request_options.ts b/modules/angular2/src/http/base_request_options.ts index eac87c0b53ca..18ed3719837e 100644 --- a/modules/angular2/src/http/base_request_options.ts +++ b/modules/angular2/src/http/base_request_options.ts @@ -1,9 +1,10 @@ -import {CONST_EXPR, CONST, isPresent, isString} from 'angular2/src/core/facade/lang'; +import {isPresent, isString} from 'angular2/src/core/facade/lang'; import {Headers} from './headers'; import {RequestMethods} from './enums'; import {RequestOptionsArgs} from './interfaces'; import {Injectable} from 'angular2/src/core/di'; import {URLSearchParams} from './url_search_params'; +import {normalizeMethodName} from './http_utils'; /** * Creates a request options object similar to the `RequestInit` description @@ -19,7 +20,7 @@ export class RequestOptions { * * Defaults to "GET". */ - method: RequestMethods; + method: RequestMethods | string; /** * Headers object based on the `Headers` class in the [Fetch * Spec](https://fetch.spec.whatwg.org/#headers-class). @@ -33,7 +34,7 @@ export class RequestOptions { url: string; search: URLSearchParams; constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) { - this.method = isPresent(method) ? method : null; + this.method = isPresent(method) ? normalizeMethodName(method) : null; this.headers = isPresent(headers) ? headers : null; this.body = isPresent(body) ? body : null; this.url = isPresent(url) ? url : null; diff --git a/modules/angular2/src/http/http_utils.ts b/modules/angular2/src/http/http_utils.ts index 9338985a502b..1fcc3d222d6c 100644 --- a/modules/angular2/src/http/http_utils.ts +++ b/modules/angular2/src/http/http_utils.ts @@ -1 +1,17 @@ +import {isString} from 'angular2/src/core/facade/lang'; +import {RequestMethods} from './enums'; +import {makeTypeError} from 'angular2/src/core/facade/exceptions'; + +export function normalizeMethodName(method): RequestMethods { + if (isString(method)) { + var originalMethod = method; + method = method.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()); + method = RequestMethods[method]; + if (typeof method !== 'number') + throw makeTypeError( + `Invalid request method. The method "${originalMethod}" is not supported.`); + } + return method; +} + export {isJsObject} from 'angular2/src/core/facade/lang'; diff --git a/modules/angular2/src/http/interfaces.ts b/modules/angular2/src/http/interfaces.ts index aa3173d1da1c..ed9509e36fce 100644 --- a/modules/angular2/src/http/interfaces.ts +++ b/modules/angular2/src/http/interfaces.ts @@ -37,7 +37,7 @@ export class Connection { // https://github.com/angular/angular/issues/3828 export interface RequestOptionsArgs { url?: string; - method?: RequestMethods; + method?: string | RequestMethods; search?: string | URLSearchParams; headers?: Headers; // TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData diff --git a/modules/angular2/src/http/static_request.ts b/modules/angular2/src/http/static_request.ts index 19595221adce..b4572af94910 100644 --- a/modules/angular2/src/http/static_request.ts +++ b/modules/angular2/src/http/static_request.ts @@ -1,6 +1,7 @@ import {RequestMethods} from './enums'; import {RequestOptions} from './base_request_options'; import {Headers} from './headers'; +import {normalizeMethodName} from './http_utils'; import { RegExpWrapper, CONST_EXPR, @@ -9,7 +10,6 @@ import { StringWrapper } from 'angular2/src/core/facade/lang'; - // TODO(jeffbcross): properly implement body accessors /** * Creates `Request` instances from provided values. @@ -51,7 +51,7 @@ export class Request { } } this._body = requestOptions.body; - this.method = requestOptions.method; + this.method = normalizeMethodName(requestOptions.method); // TODO(jeffbcross): implement behavior // Defaults to 'omit', consistent with browser // TODO(jeffbcross): implement behavior diff --git a/modules/angular2/test/http/http_spec.ts b/modules/angular2/test/http/http_spec.ts index 4e61760340b3..24e273177eff 100644 --- a/modules/angular2/test/http/http_spec.ts +++ b/modules/angular2/test/http/http_spec.ts @@ -319,6 +319,29 @@ export function main() { res => {}); })); }); + + describe('string method names', () => { + it('should allow case insensitive strings for method names', () => { + inject([AsyncTestCompleter], (async) => { + ObservableWrapper.subscribe(backend.connections, c => { + expect(c.request.method) + .toBe(RequestMethods.Post) + c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'}))); + async.done(); + }); + ObservableWrapper.subscribe(http.request(new Request(new RequestOptions( + {url: 'https://google.com', method: 'PosT'}))), + (res) => {}); + }); + }); + + it('should throw when invalid string parameter is passed for method name', () => { + expect(() => { + http.request( + new Request(new RequestOptions({url: 'https://google.com', method: 'Invalid'}))); + }).toThrowError('Invalid request method. The method "Invalid" is not supported.'); + }); + }); }); describe('Jsonp', () => {