Skip to content

Commit 057cf7f

Browse files
jnizetdevversion
authored andcommitted
fix(http): preserve all headers from Headers object (#57802)
when initialized from a `Headers` object containing multiple values for the same header, `HttpHeaders` now contains all the header values instead of only having one of them. Fixes #57798 PR Close #57802
1 parent 9ce839f commit 057cf7f

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

packages/common/http/src/headers.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,15 @@ export class HttpHeaders {
5656
const index = line.indexOf(':');
5757
if (index > 0) {
5858
const name = line.slice(0, index);
59-
const key = name.toLowerCase();
6059
const value = line.slice(index + 1).trim();
61-
this.maybeSetNormalizedName(name, key);
62-
if (this.headers.has(key)) {
63-
this.headers.get(key)!.push(value);
64-
} else {
65-
this.headers.set(key, [value]);
66-
}
60+
this.addHeaderEntry(name, value);
6761
}
6862
});
6963
};
7064
} else if (typeof Headers !== 'undefined' && headers instanceof Headers) {
7165
this.headers = new Map<string, string[]>();
72-
headers.forEach((values: string, name: string) => {
73-
this.setHeaderEntries(name, values);
66+
headers.forEach((value: string, name: string) => {
67+
this.addHeaderEntry(name, value);
7468
});
7569
} else {
7670
this.lazyInit = () => {
@@ -249,6 +243,16 @@ export class HttpHeaders {
249243
}
250244
}
251245

246+
private addHeaderEntry(name: string, value: string) {
247+
const key = name.toLowerCase();
248+
this.maybeSetNormalizedName(name, key);
249+
if (this.headers.has(key)) {
250+
this.headers.get(key)!.push(value);
251+
} else {
252+
this.headers.set(key, [value]);
253+
}
254+
}
255+
252256
private setHeaderEntries(name: string, values: any) {
253257
const headerValues = (Array.isArray(values) ? values : [values]).map((value) =>
254258
value.toString(),

packages/common/http/test/headers_spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ describe('HttpHeaders', () => {
4646
expect(headers.getAll('foo')).toEqual(['second']);
4747
});
4848

49+
it('should keep all values when initialized from a Headers object with duplicate headers', () => {
50+
const standardHeaders = new Headers([
51+
['Set-Cookie', 'cookie1=foo'],
52+
['Set-Cookie', 'cookie2=bar'],
53+
]);
54+
const headers = new HttpHeaders(standardHeaders);
55+
56+
expect(headers.getAll('Set-Cookie')).toEqual(['cookie1=foo', 'cookie2=bar']);
57+
});
58+
4959
it('should throw an error when null is passed as header', () => {
5060
// Note: the `strictNullChecks` set to `false` in TS config would make `null`
5161
// valid value within the headers object, thus this test verifies this scenario.

0 commit comments

Comments
 (0)