Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packages/common/http/src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,15 @@ export class HttpHeaders {
const index = line.indexOf(':');
if (index > 0) {
const name = line.slice(0, index);
const key = name.toLowerCase();
const value = line.slice(index + 1).trim();
this.maybeSetNormalizedName(name, key);
if (this.headers.has(key)) {
this.headers.get(key)!.push(value);
} else {
this.headers.set(key, [value]);
}
this.addHeaderEntry(name, value);
}
});
};
} else if (typeof Headers !== 'undefined' && headers instanceof Headers) {
this.headers = new Map<string, string[]>();
headers.forEach((values: string, name: string) => {
this.setHeaderEntries(name, values);
headers.forEach((value: string, name: string) => {
this.addHeaderEntry(name, value);
});
} else {
this.lazyInit = () => {
Expand Down Expand Up @@ -249,6 +243,16 @@ export class HttpHeaders {
}
}

private addHeaderEntry(name: string, value: string) {
const key = name.toLowerCase();
this.maybeSetNormalizedName(name, key);
if (this.headers.has(key)) {
this.headers.get(key)!.push(value);
} else {
this.headers.set(key, [value]);
}
}

private setHeaderEntries(name: string, values: any) {
const headerValues = (Array.isArray(values) ? values : [values]).map((value) =>
value.toString(),
Expand Down
10 changes: 10 additions & 0 deletions packages/common/http/test/headers_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ describe('HttpHeaders', () => {
expect(headers.getAll('foo')).toEqual(['second']);
});

it('should keep all values when initialized from a Headers object with duplicate headers', () => {
const standardHeaders = new Headers([
['Set-Cookie', 'cookie1=foo'],
['Set-Cookie', 'cookie2=bar'],
]);
const headers = new HttpHeaders(standardHeaders);

expect(headers.getAll('Set-Cookie')).toEqual(['cookie1=foo', 'cookie2=bar']);
});

it('should throw an error when null is passed as header', () => {
// Note: the `strictNullChecks` set to `false` in TS config would make `null`
// valid value within the headers object, thus this test verifies this scenario.
Expand Down