diff --git a/modules/angular2/src/http/headers.ts b/modules/angular2/src/http/headers.ts index 42efd0e620..aa52de230b 100644 --- a/modules/angular2/src/http/headers.ts +++ b/modules/angular2/src/http/headers.ts @@ -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. diff --git a/modules/angular2/test/http/headers_spec.ts b/modules/angular2/test/http/headers_spec.ts index 0102164f4a..6b2d5216bf 100644 --- a/modules/angular2/test/http/headers_spec.ts +++ b/modules/angular2/test/http/headers_spec.ts @@ -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()', () => {