parent
a251df9df4
commit
34518f0f2d
|
@ -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 {Headers} from './headers';
|
||||||
import {RequestMethods} from './enums';
|
import {RequestMethods} from './enums';
|
||||||
import {RequestOptionsArgs} from './interfaces';
|
import {RequestOptionsArgs} from './interfaces';
|
||||||
import {Injectable} from 'angular2/src/core/di';
|
import {Injectable} from 'angular2/src/core/di';
|
||||||
import {URLSearchParams} from './url_search_params';
|
import {URLSearchParams} from './url_search_params';
|
||||||
|
import {normalizeMethodName} from './http_utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a request options object to be optionally provided when instantiating a
|
* Creates a request options object to be optionally provided when instantiating a
|
||||||
|
@ -34,7 +35,7 @@ export class RequestOptions {
|
||||||
* Http method with which to execute a {@link Request}.
|
* Http method with which to execute a {@link Request}.
|
||||||
* Acceptable methods are defined in the {@link RequestMethods} enum.
|
* Acceptable methods are defined in the {@link RequestMethods} enum.
|
||||||
*/
|
*/
|
||||||
method: RequestMethods;
|
method: RequestMethods | string;
|
||||||
/**
|
/**
|
||||||
* {@link Headers} to be attached to a {@link Request}.
|
* {@link Headers} to be attached to a {@link Request}.
|
||||||
*/
|
*/
|
||||||
|
@ -53,7 +54,7 @@ export class RequestOptions {
|
||||||
*/
|
*/
|
||||||
search: URLSearchParams;
|
search: URLSearchParams;
|
||||||
constructor({method, headers, body, url, search}: RequestOptionsArgs = {}) {
|
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.headers = isPresent(headers) ? headers : null;
|
||||||
this.body = isPresent(body) ? body : null;
|
this.body = isPresent(body) ? body : null;
|
||||||
this.url = isPresent(url) ? url : null;
|
this.url = isPresent(url) ? url : null;
|
||||||
|
|
|
@ -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';
|
export {isJsObject} from 'angular2/src/core/facade/lang';
|
||||||
|
|
|
@ -31,7 +31,7 @@ export abstract class Connection {
|
||||||
*/
|
*/
|
||||||
export type RequestOptionsArgs = {
|
export type RequestOptionsArgs = {
|
||||||
url?: string;
|
url?: string;
|
||||||
method?: RequestMethods;
|
method?: string | RequestMethods;
|
||||||
search?: string | URLSearchParams;
|
search?: string | URLSearchParams;
|
||||||
headers?: Headers;
|
headers?: Headers;
|
||||||
// TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData
|
// TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import {RequestMethods} from './enums';
|
import {RequestMethods} from './enums';
|
||||||
import {RequestOptions} from './base_request_options';
|
import {RequestOptions} from './base_request_options';
|
||||||
import {Headers} from './headers';
|
import {Headers} from './headers';
|
||||||
|
import {normalizeMethodName} from './http_utils';
|
||||||
import {
|
import {
|
||||||
RegExpWrapper,
|
RegExpWrapper,
|
||||||
CONST_EXPR,
|
CONST_EXPR,
|
||||||
|
@ -9,7 +10,6 @@ import {
|
||||||
StringWrapper
|
StringWrapper
|
||||||
} from 'angular2/src/core/facade/lang';
|
} from 'angular2/src/core/facade/lang';
|
||||||
|
|
||||||
|
|
||||||
// TODO(jeffbcross): properly implement body accessors
|
// TODO(jeffbcross): properly implement body accessors
|
||||||
/**
|
/**
|
||||||
* Creates `Request` instances from provided values.
|
* Creates `Request` instances from provided values.
|
||||||
|
@ -77,7 +77,7 @@ export class Request {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this._body = requestOptions.body;
|
this._body = requestOptions.body;
|
||||||
this.method = requestOptions.method;
|
this.method = normalizeMethodName(requestOptions.method);
|
||||||
// TODO(jeffbcross): implement behavior
|
// TODO(jeffbcross): implement behavior
|
||||||
// Defaults to 'omit', consistent with browser
|
// Defaults to 'omit', consistent with browser
|
||||||
// TODO(jeffbcross): implement behavior
|
// TODO(jeffbcross): implement behavior
|
||||||
|
|
|
@ -319,6 +319,29 @@ export function main() {
|
||||||
res => {});
|
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', () => {
|
describe('Jsonp', () => {
|
||||||
|
|
Loading…
Reference in New Issue