X Tutup
Skip to content

Commit 34518f0

Browse files
mgechevjeffbcross
authored andcommitted
feat(http): Add support for strings as http method names
Closes #4331
1 parent a251df9 commit 34518f0

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

modules/angular2/src/http/base_request_options.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import {CONST_EXPR, CONST, isPresent, isString} from 'angular2/src/core/facade/lang';
1+
import {isPresent, isString} from 'angular2/src/core/facade/lang';
22
import {Headers} from './headers';
33
import {RequestMethods} from './enums';
44
import {RequestOptionsArgs} from './interfaces';
55
import {Injectable} from 'angular2/src/core/di';
66
import {URLSearchParams} from './url_search_params';
7+
import {normalizeMethodName} from './http_utils';
78

89
/**
910
* Creates a request options object to be optionally provided when instantiating a
@@ -34,7 +35,7 @@ export class RequestOptions {
3435
* Http method with which to execute a {@link Request}.
3536
* Acceptable methods are defined in the {@link RequestMethods} enum.
3637
*/
37-
method: RequestMethods;
38+
method: RequestMethods | string;
3839
/**
3940
* {@link Headers} to be attached to a {@link Request}.
4041
*/
@@ -53,7 +54,7 @@ export class RequestOptions {
5354
*/
5455
search: URLSearchParams;
5556
constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) {
56-
this.method = isPresent(method) ? method : null;
57+
this.method = isPresent(method) ? normalizeMethodName(method) : null;
5758
this.headers = isPresent(headers) ? headers : null;
5859
this.body = isPresent(body) ? body : null;
5960
this.url = isPresent(url) ? url : null;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
import {isString} from 'angular2/src/core/facade/lang';
2+
import {RequestMethods} from './enums';
3+
import {makeTypeError} from 'angular2/src/core/facade/exceptions';
4+
5+
export function normalizeMethodName(method): RequestMethods {
6+
if (isString(method)) {
7+
var originalMethod = method;
8+
method = method.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
9+
method = RequestMethods[method];
10+
if (typeof method !== 'number')
11+
throw makeTypeError(
12+
`Invalid request method. The method "${originalMethod}" is not supported.`);
13+
}
14+
return method;
15+
}
16+
117
export {isJsObject} from 'angular2/src/core/facade/lang';

modules/angular2/src/http/interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export abstract class Connection {
3131
*/
3232
export type RequestOptionsArgs = {
3333
url?: string;
34-
method?: RequestMethods;
34+
method?: string | RequestMethods;
3535
search?: string | URLSearchParams;
3636
headers?: Headers;
3737
// TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData

modules/angular2/src/http/static_request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {RequestMethods} from './enums';
22
import {RequestOptions} from './base_request_options';
33
import {Headers} from './headers';
4+
import {normalizeMethodName} from './http_utils';
45
import {
56
RegExpWrapper,
67
CONST_EXPR,
@@ -9,7 +10,6 @@ import {
910
StringWrapper
1011
} from 'angular2/src/core/facade/lang';
1112

12-
1313
// TODO(jeffbcross): properly implement body accessors
1414
/**
1515
* Creates `Request` instances from provided values.
@@ -77,7 +77,7 @@ export class Request {
7777
}
7878
}
7979
this._body = requestOptions.body;
80-
this.method = requestOptions.method;
80+
this.method = normalizeMethodName(requestOptions.method);
8181
// TODO(jeffbcross): implement behavior
8282
// Defaults to 'omit', consistent with browser
8383
// TODO(jeffbcross): implement behavior

modules/angular2/test/http/http_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,29 @@ export function main() {
319319
res => {});
320320
}));
321321
});
322+
323+
describe('string method names', () => {
324+
it('should allow case insensitive strings for method names', () => {
325+
inject([AsyncTestCompleter], (async) => {
326+
ObservableWrapper.subscribe<MockConnection>(backend.connections, c => {
327+
expect(c.request.method)
328+
.toBe(RequestMethods.Post)
329+
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
330+
async.done();
331+
});
332+
ObservableWrapper.subscribe(http.request(new Request(new RequestOptions(
333+
{url: 'https://google.com', method: 'PosT'}))),
334+
(res) => {});
335+
});
336+
});
337+
338+
it('should throw when invalid string parameter is passed for method name', () => {
339+
expect(() => {
340+
http.request(
341+
new Request(new RequestOptions({url: 'https://google.com', method: 'Invalid'})));
342+
}).toThrowError('Invalid request method. The method "Invalid" is not supported.');
343+
});
344+
});
322345
});
323346

324347
describe('Jsonp', () => {

0 commit comments

Comments
 (0)
X Tutup