Skip to content

Commit

Permalink
feat(http): Add support for strings as http method names
Browse files Browse the repository at this point in the history
Closes #4331
  • Loading branch information
mgechev authored and jeffbcross committed Oct 2, 2015
1 parent a251df9 commit 34518f0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
7 changes: 4 additions & 3 deletions modules/angular2/src/http/base_request_options.ts
Original file line number Diff line number Diff line change
@@ -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 to be optionally provided when instantiating a
Expand Down Expand Up @@ -34,7 +35,7 @@ export class RequestOptions {
* Http method with which to execute a {@link Request}.
* Acceptable methods are defined in the {@link RequestMethods} enum.
*/
method: RequestMethods;
method: RequestMethods | string;
/**
* {@link Headers} to be attached to a {@link Request}.
*/
Expand All @@ -53,7 +54,7 @@ export class RequestOptions {
*/
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;
Expand Down
16 changes: 16 additions & 0 deletions modules/angular2/src/http/http_utils.ts
Original file line number Diff line number Diff line change
@@ -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';
2 changes: 1 addition & 1 deletion modules/angular2/src/http/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export abstract class Connection {
*/
export type RequestOptionsArgs = {
url?: string;
method?: RequestMethods;
method?: string | RequestMethods;
search?: string | URLSearchParams;
headers?: Headers;
// TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData
Expand Down
4 changes: 2 additions & 2 deletions modules/angular2/src/http/static_request.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,7 +10,6 @@ import {
StringWrapper
} from 'angular2/src/core/facade/lang';


// TODO(jeffbcross): properly implement body accessors
/**
* Creates `Request` instances from provided values.
Expand Down Expand Up @@ -77,7 +77,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
Expand Down
23 changes: 23 additions & 0 deletions modules/angular2/test/http/http_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MockConnection>(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', () => {
Expand Down

0 comments on commit 34518f0

Please sign in to comment.