From 654ff6115ab3cc39427cc219ec1db4c7b39fec1a Mon Sep 17 00:00:00 2001 From: Marcus Krahl Date: Fri, 19 Aug 2016 00:00:44 +0200 Subject: [PATCH] fix(http): deep copy for constructor using existing Headers (#10679) When creating a new Headers object using an existing Headers object the existing Headers map is copied by reference. Therefore adding a new Header value to the new Headers object also added this value to the existing Headers object which is not in accordance with the spec. This commit alters the constructor to create a deep copy of existing Headers maps and therefore unlink existing Headers from new Headers. Closes #6845 BREAKING CHANGE: any code which relies on the fact that a newly created Headers object is referencing an existing Headers map is now broken, but that should normally not be the case since this behavior is not documented and not in accordance with the spec. --- modules/@angular/http/src/headers.ts | 2 +- modules/@angular/http/test/headers_spec.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/@angular/http/src/headers.ts b/modules/@angular/http/src/headers.ts index 9d1f5982a8..553fb34f86 100644 --- a/modules/@angular/http/src/headers.ts +++ b/modules/@angular/http/src/headers.ts @@ -46,7 +46,7 @@ export class Headers { _headersMap: Map; constructor(headers?: Headers|{[key: string]: any}) { if (headers instanceof Headers) { - this._headersMap = (headers)._headersMap; + this._headersMap = new Map((headers)._headersMap); return; } diff --git a/modules/@angular/http/test/headers_spec.ts b/modules/@angular/http/test/headers_spec.ts index 9b79275ee2..ae8593a1ef 100644 --- a/modules/@angular/http/test/headers_spec.ts +++ b/modules/@angular/http/test/headers_spec.ts @@ -42,6 +42,14 @@ export function main() { expect(headers.get('foo')).toBe('bar'); expect(headers.getAll('foo')).toEqual(['bar']); }); + 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'); + expect(firstHeaders.has('Content-Type')).toBeFalsy(); + }); });