fix(Headers): serializable toJSON

fixes #6073

Closes #6714
This commit is contained in:
gdi2290 2016-01-26 23:24:50 -08:00 committed by PatrickJS
parent 5e9daed2e8
commit b55f1764b5
2 changed files with 49 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import {
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
import {
isListLikeIterable,
iterateListLike,
Map,
MapWrapper,
StringMapWrapper,
@ -131,7 +132,17 @@ export class Headers {
/**
* Returns string of all headers.
*/
toJSON(): string { return Json.stringify(this.values()); }
toJSON(): {[key: string]: any} {
let serializableHeaders = {};
this._headersMap.forEach((values: string[], name: string) => {
let list = [];
iterateListLike(values, val => list = ListWrapper.concat(list, val.split(',')));
serializableHeaders[name] = list;
});
return serializableHeaders;
}
/**
* Returns list of header values for a given name.

View File

@ -1,4 +1,5 @@
import {Headers} from 'angular2/src/http/headers';
import {Json} from 'angular2/src/facade/lang';
import {Map, StringMapWrapper} from 'angular2/src/facade/collection';
import {
AsyncTestCompleter,
@ -62,6 +63,42 @@ export function main() {
expect(/bar, ?baz/g.test(headers.getAll('foo')[0])).toBe(true);
});
});
describe('.toJSON()', () => {
let headers = null;
let inputArr = null;
let obj = null;
beforeEach(() => {
headers = new Headers();
inputArr = ['application/jeisen', 'application/jason', 'application/patrickjs'];
obj = {'Accept': inputArr};
headers.set('Accept', inputArr);
});
it('should be serializable with toJSON', () => {
let stringifed = Json.stringify(obj);
let serializedHeaders = Json.stringify(headers);
expect(serializedHeaders).toEqual(stringifed);
});
it('should be able to parse serialized header', () => {
let stringifed = Json.stringify(obj);
let serializedHeaders = Json.stringify(headers);
expect(Json.parse(serializedHeaders)).toEqual(Json.parse(stringifed));
});
it('should be able to recreate serializedHeaders', () => {
let serializedHeaders = Json.stringify(headers);
let parsedHeaders = Json.parse(serializedHeaders);
let recreatedHeaders = new Headers(parsedHeaders);
expect(Json.stringify(parsedHeaders)).toEqual(Json.stringify(recreatedHeaders));
});
});
});
describe('.fromResponseHeaderString()', () => {