2016-07-22 11:37:32 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-03-02 15:12:46 -05:00
|
|
|
import {describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
import {RequestOptions} from '../src/base_request_options';
|
|
|
|
import {ContentType} from '../src/enums';
|
|
|
|
import {Headers} from '../src/headers';
|
2017-01-18 19:01:02 -05:00
|
|
|
import {ArrayBuffer, Request} from '../src/static_request';
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('Request', () => {
|
|
|
|
describe('detectContentType', () => {
|
|
|
|
it('should return ContentType.NONE', () => {
|
2017-04-17 14:12:53 -04:00
|
|
|
const req =
|
|
|
|
new Request(new RequestOptions({url: 'test', method: 'GET', body: null}) as any);
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.NONE);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return ContentType.JSON', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({'content-type': 'application/json'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.JSON);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return ContentType.FORM', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({'content-type': 'application/x-www-form-urlencoded'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.FORM);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return ContentType.FORM_DATA', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({'content-type': 'multipart/form-data'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.FORM_DATA);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return ContentType.TEXT', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({'content-type': 'text/plain'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.TEXT);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return ContentType.BLOB', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({'content-type': 'application/octet-stream'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2016-07-22 11:37:32 -04:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.BLOB);
|
|
|
|
});
|
2017-01-18 19:01:02 -05:00
|
|
|
|
|
|
|
it('should not create a blob out of ArrayBuffer', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: new ArrayBuffer(1),
|
|
|
|
headers: new Headers({'content-type': 'application/octet-stream'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2017-01-18 19:01:02 -05:00
|
|
|
|
|
|
|
expect(req.detectContentType()).toEqual(ContentType.ARRAY_BUFFER);
|
|
|
|
});
|
2016-07-22 11:37:32 -04:00
|
|
|
});
|
2016-08-12 00:40:18 -04:00
|
|
|
|
|
|
|
it('should return empty string if no body is present', () => {
|
|
|
|
const req = new Request(new RequestOptions({
|
|
|
|
url: 'test',
|
|
|
|
method: 'GET',
|
|
|
|
body: null,
|
|
|
|
headers: new Headers({'content-type': 'application/json'})
|
2017-04-17 14:12:53 -04:00
|
|
|
}) as any);
|
2016-08-12 00:40:18 -04:00
|
|
|
|
|
|
|
expect(req.text()).toEqual('');
|
|
|
|
});
|
2016-12-09 18:39:39 -05:00
|
|
|
|
|
|
|
it('should return empty string if body is undefined', () => {
|
|
|
|
const reqOptions = new RequestOptions(
|
|
|
|
{url: 'test', method: 'GET', headers: new Headers({'content-type': 'application/json'})});
|
|
|
|
delete reqOptions.body;
|
2017-04-17 14:12:53 -04:00
|
|
|
const req = new Request(reqOptions as any);
|
2016-12-09 18:39:39 -05:00
|
|
|
|
|
|
|
expect(req.text()).toEqual('');
|
|
|
|
});
|
2016-07-22 11:37:32 -04:00
|
|
|
});
|
|
|
|
}
|