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.
This commit is contained in:
parent
628d06c17c
commit
654ff6115a
|
@ -46,7 +46,7 @@ export class Headers {
|
||||||
_headersMap: Map<string, string[]>;
|
_headersMap: Map<string, string[]>;
|
||||||
constructor(headers?: Headers|{[key: string]: any}) {
|
constructor(headers?: Headers|{[key: string]: any}) {
|
||||||
if (headers instanceof Headers) {
|
if (headers instanceof Headers) {
|
||||||
this._headersMap = (<Headers>headers)._headersMap;
|
this._headersMap = new Map<string, string[]>((<Headers>headers)._headersMap);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,14 @@ export function main() {
|
||||||
expect(headers.get('foo')).toBe('bar');
|
expect(headers.get('foo')).toBe('bar');
|
||||||
expect(headers.getAll('foo')).toEqual(['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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue