2016-06-23 12:47:54 -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
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Headers} from '../src/headers';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('Headers', () => {
|
|
|
|
|
|
|
|
describe('initialization', () => {
|
2016-10-03 18:27:56 -04:00
|
|
|
it('should conform to spec', () => {
|
|
|
|
const httpHeaders = {
|
|
|
|
'Content-Type': 'image/jpeg',
|
|
|
|
'Accept-Charset': 'utf-8',
|
|
|
|
'X-My-Custom-Header': 'Zeke are cool',
|
|
|
|
};
|
|
|
|
const secondHeaders = new Headers(httpHeaders);
|
|
|
|
const secondHeadersObj = new Headers(secondHeaders);
|
|
|
|
expect(secondHeadersObj.get('Content-Type')).toEqual('image/jpeg');
|
|
|
|
});
|
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
it('should merge values in provided dictionary', () => {
|
2016-09-18 18:39:26 -04:00
|
|
|
const headers = new Headers({'foo': 'bar'});
|
2016-10-03 18:27:56 -04:00
|
|
|
expect(headers.get('foo')).toEqual('bar');
|
2015-04-29 02:07:55 -04:00
|
|
|
expect(headers.getAll('foo')).toEqual(['bar']);
|
|
|
|
});
|
2016-10-03 18:27:56 -04:00
|
|
|
|
2016-08-18 18:00:44 -04:00
|
|
|
it('should not alter the values of a provided header template', () => {
|
|
|
|
// Spec at https://fetch.spec.whatwg.org/#concept-headers-fill
|
|
|
|
// test for https://github.com/angular/angular/issues/6845
|
|
|
|
const firstHeaders = new Headers();
|
|
|
|
const secondHeaders = new Headers(firstHeaders);
|
|
|
|
secondHeaders.append('Content-Type', 'image/jpeg');
|
2016-10-03 18:27:56 -04:00
|
|
|
expect(firstHeaders.has('Content-Type')).toEqual(false);
|
2016-08-18 18:00:44 -04:00
|
|
|
});
|
2015-04-29 02:07:55 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('.set()', () => {
|
|
|
|
it('should clear all values and re-set for the provided key', () => {
|
2016-09-18 18:39:26 -04:00
|
|
|
const headers = new Headers({'foo': 'bar'});
|
2016-10-03 18:27:56 -04:00
|
|
|
expect(headers.get('foo')).toEqual('bar');
|
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
headers.set('foo', 'baz');
|
2016-10-03 18:27:56 -04:00
|
|
|
expect(headers.get('foo')).toEqual('baz');
|
|
|
|
|
|
|
|
headers.set('fOO', 'bat');
|
|
|
|
expect(headers.get('foo')).toEqual('bat');
|
2015-04-29 02:07:55 -04:00
|
|
|
});
|
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
it('should preserve the case of the first call', () => {
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('fOo', 'baz');
|
|
|
|
headers.set('foo', 'bat');
|
|
|
|
expect(JSON.stringify(headers)).toEqual('{"fOo":["bat"]}');
|
|
|
|
});
|
2015-04-29 02:07:55 -04:00
|
|
|
|
|
|
|
it('should convert input array to string', () => {
|
2016-10-03 18:27:56 -04:00
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('foo', ['bar', 'baz']);
|
|
|
|
expect(headers.get('foo')).toEqual('bar,baz');
|
|
|
|
expect(headers.getAll('foo')).toEqual(['bar,baz']);
|
2015-04-29 02:07:55 -04:00
|
|
|
});
|
|
|
|
});
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
describe('.get()', () => {
|
|
|
|
it('should be case insensitive', () => {
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set('foo', 'baz');
|
|
|
|
expect(headers.get('foo')).toEqual('baz');
|
|
|
|
expect(headers.get('FOO')).toEqual('baz');
|
|
|
|
});
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
it('should return null if the header is not present', () => {
|
|
|
|
const headers = new Headers({bar: []});
|
|
|
|
expect(headers.get('bar')).toEqual(null);
|
|
|
|
expect(headers.get('foo')).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
describe('.getAll()', () => {
|
|
|
|
it('should be case insensitive', () => {
|
|
|
|
const headers = new Headers({foo: ['bar', 'baz']});
|
|
|
|
expect(headers.getAll('foo')).toEqual(['bar', 'baz']);
|
|
|
|
expect(headers.getAll('FOO')).toEqual(['bar', 'baz']);
|
2016-01-27 02:24:50 -05:00
|
|
|
});
|
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
it('should return null if the header is not present', () => {
|
|
|
|
const headers = new Headers();
|
|
|
|
expect(headers.getAll('foo')).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
describe('.delete', () => {
|
|
|
|
it('should be case insensitive', () => {
|
|
|
|
const headers = new Headers();
|
|
|
|
|
|
|
|
headers.set('foo', 'baz');
|
|
|
|
expect(headers.has('foo')).toEqual(true);
|
|
|
|
headers.delete('foo');
|
|
|
|
expect(headers.has('foo')).toEqual(false);
|
|
|
|
|
|
|
|
headers.set('foo', 'baz');
|
|
|
|
expect(headers.has('foo')).toEqual(true);
|
|
|
|
headers.delete('FOO');
|
|
|
|
expect(headers.has('foo')).toEqual(false);
|
2016-01-27 02:24:50 -05:00
|
|
|
});
|
2016-10-03 18:27:56 -04:00
|
|
|
});
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
describe('.append', () => {
|
|
|
|
it('should preserve the case of the first call', () => {
|
|
|
|
const headers = new Headers();
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
headers.append('FOO', 'bar');
|
|
|
|
headers.append('foo', 'baz');
|
|
|
|
expect(JSON.stringify(headers)).toEqual('{"FOO":["bar","baz"]}');
|
2016-01-27 02:24:50 -05:00
|
|
|
});
|
2016-10-03 18:27:56 -04:00
|
|
|
});
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-10-03 18:27:56 -04:00
|
|
|
describe('.toJSON()', () => {
|
|
|
|
let headers: Headers;
|
|
|
|
let values: string[];
|
|
|
|
let ref: {[name: string]: string[]};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
values = ['application/jeisen', 'application/jason', 'application/patrickjs'];
|
|
|
|
headers = new Headers();
|
|
|
|
headers.set('Accept', values);
|
|
|
|
ref = {'Accept': values};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should be serializable with toJSON',
|
|
|
|
() => { expect(JSON.stringify(headers)).toEqual(JSON.stringify(ref)); });
|
2016-01-27 02:24:50 -05:00
|
|
|
|
|
|
|
it('should be able to recreate serializedHeaders', () => {
|
2016-10-03 18:27:56 -04:00
|
|
|
const parsedHeaders = JSON.parse(JSON.stringify(headers));
|
|
|
|
const recreatedHeaders = new Headers(parsedHeaders);
|
|
|
|
expect(JSON.stringify(parsedHeaders)).toEqual(JSON.stringify(recreatedHeaders));
|
2016-01-27 02:24:50 -05:00
|
|
|
});
|
|
|
|
});
|
2015-11-19 20:51:00 -05:00
|
|
|
|
2016-10-05 19:36:11 -04:00
|
|
|
describe('.fromResponseHeaderString()', () => {
|
|
|
|
it('should parse a response header string', () => {
|
|
|
|
const response = `Date: Fri, 20 Nov 2015 01:45:26 GMT\n` +
|
|
|
|
`Content-Type: application/json; charset=utf-8\n` +
|
|
|
|
`Transfer-Encoding: chunked\n` +
|
|
|
|
`Connection: keep-alive`;
|
|
|
|
const headers = Headers.fromResponseHeaderString(response);
|
|
|
|
expect(headers.get('Date')).toEqual('Fri, 20 Nov 2015 01:45:26 GMT');
|
|
|
|
expect(headers.get('Content-Type')).toEqual('application/json; charset=utf-8');
|
|
|
|
expect(headers.get('Transfer-Encoding')).toEqual('chunked');
|
|
|
|
expect(headers.get('Connection')).toEqual('keep-alive');
|
|
|
|
});
|
2015-11-19 20:51:00 -05:00
|
|
|
});
|
|
|
|
});
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|