diff --git a/modules/@angular/http/src/headers.ts b/modules/@angular/http/src/headers.ts index 0e54e80689..3e582aa239 100644 --- a/modules/@angular/http/src/headers.ts +++ b/modules/@angular/http/src/headers.ts @@ -49,19 +49,16 @@ export class Headers { } if (headers instanceof Headers) { - headers._headers.forEach((value: string[], name: string) => { - const lcName = name.toLowerCase(); - this._headers.set(lcName, value); - this.mayBeSetNormalizedName(name); + headers._headers.forEach((values: string[], name: string) => { + values.forEach(value => this.append(name, value)); }); return; } Object.keys(headers).forEach((name: string) => { - const value = headers[name]; - const lcName = name.toLowerCase(); - this._headers.set(lcName, Array.isArray(value) ? value : [value]); - this.mayBeSetNormalizedName(name); + const values: string[] = Array.isArray(headers[name]) ? headers[name] : [headers[name]]; + this.delete(name); + values.forEach(value => this.append(name, value)); }); } @@ -137,8 +134,13 @@ export class Headers { * Sets or overrides header value for given name. */ set(name: string, value: string|string[]): void { - const strValue = Array.isArray(value) ? value.join(',') : value; - this._headers.set(name.toLowerCase(), [strValue]); + if (Array.isArray(value)) { + if (value.length) { + this._headers.set(name.toLowerCase(), [value.join(',')]); + } + } else { + this._headers.set(name.toLowerCase(), [value]); + } this.mayBeSetNormalizedName(name); } diff --git a/modules/@angular/http/test/headers_spec.ts b/modules/@angular/http/test/headers_spec.ts index 0325f0c5ab..d1090b82ee 100644 --- a/modules/@angular/http/test/headers_spec.ts +++ b/modules/@angular/http/test/headers_spec.ts @@ -37,6 +37,24 @@ export function main() { secondHeaders.append('Content-Type', 'image/jpeg'); expect(firstHeaders.has('Content-Type')).toEqual(false); }); + + it('should preserve the list of values', () => { + const src = new Headers(); + src.append('foo', 'a'); + src.append('foo', 'b'); + src.append('foo', 'c'); + const dst = new Headers(src); + expect(dst.getAll('foo')).toEqual(src.getAll('foo')); + }); + + it('should keep the last value when initialized from an object', () => { + const headers = new Headers({ + 'foo': 'first', + 'fOo': 'second', + }); + + expect(headers.getAll('foo')).toEqual(['second']); + }); }); describe('.set()', () => { @@ -140,7 +158,6 @@ export function main() { ref = {'Accept': values}; }); - it('should be serializable with toJSON', () => { expect(JSON.stringify(headers)).toEqual(JSON.stringify(ref)); });