diff --git a/goldens/public-api/common/http/index.md b/goldens/public-api/common/http/index.md index 1cc31ac6928..91875972d43 100644 --- a/goldens/public-api/common/http/index.md +++ b/goldens/public-api/common/http/index.md @@ -1782,7 +1782,7 @@ export class HttpHeaderResponse extends HttpResponseBase { // @public export class HttpHeaders { constructor(headers?: string | { - [name: string]: string | string[]; + [name: string]: string | number | (string | number)[]; }); append(name: string, value: string | string[]): HttpHeaders; delete(name: string, value?: string | string[]): HttpHeaders; diff --git a/packages/common/http/src/headers.ts b/packages/common/http/src/headers.ts old mode 100755 new mode 100644 index 16d604f8225..2f711c77817 --- a/packages/common/http/src/headers.ts +++ b/packages/common/http/src/headers.ts @@ -45,7 +45,7 @@ export class HttpHeaders { /** Constructs a new HTTP header object with the given values.*/ - constructor(headers?: string|{[name: string]: string | string[]}) { + constructor(headers?: string|{[name: string]: string | number | (string | number)[]}) { if (!headers) { this.headers = new Map(); } else if (typeof headers === 'string') { @@ -72,14 +72,20 @@ export class HttpHeaders { assertValidHeaders(headers); } this.headers = new Map(); - Object.keys(headers).forEach(name => { - let values: string|string[] = headers[name]; - const key = name.toLowerCase(); + Object.entries(headers).forEach(([name, values]) => { + let headerValues: string[]; + if (typeof values === 'string') { - values = [values]; + headerValues = [values]; + } else if (typeof values === 'number') { + headerValues = [values.toString()]; + } else { + headerValues = values.map((value) => value.toString()); } - if (values.length > 0) { - this.headers.set(key, values); + + if (headerValues.length > 0) { + const key = name.toLowerCase(); + this.headers.set(key, headerValues); this.maybeSetNormalizedName(name, key); } }); @@ -264,16 +270,16 @@ export class HttpHeaders { /** * Verifies that the headers object has the right shape: the values - * must be either strings or arrays. Throws an error if an invalid + * must be either strings, numbers or arrays. Throws an error if an invalid * header value is present. */ function assertValidHeaders(headers: Record): - asserts headers is Record { + asserts headers is Record { for (const [key, value] of Object.entries(headers)) { - if (typeof value !== 'string' && !Array.isArray(value)) { + if (!(typeof value === 'string' || typeof value === 'number') && !Array.isArray(value)) { throw new Error( `Unexpected value of the \`${key}\` header provided. ` + - `Expecting either a string or an array, but got: \`${value}\`.`); + `Expecting either a string, a number or an array, but got: \`${value}\`.`); } } } diff --git a/packages/common/http/test/client_spec.ts b/packages/common/http/test/client_spec.ts index b2dbe911a18..0eef8b80c5e 100644 --- a/packages/common/http/test/client_spec.ts +++ b/packages/common/http/test/client_spec.ts @@ -224,7 +224,7 @@ import {toArray} from 'rxjs/operators'; expect(() => backend.expectOne('/test').request.headers.has('random-header')) .toThrowError( 'Unexpected value of the `foo` header provided. ' + - 'Expecting either a string or an array, but got: `null`.'); + 'Expecting either a string, a number or an array, but got: `null`.'); }); }); }); diff --git a/packages/common/http/test/headers_spec.ts b/packages/common/http/test/headers_spec.ts index 182723bb1d4..5096624dcfa 100644 --- a/packages/common/http/test/headers_spec.ts +++ b/packages/common/http/test/headers_spec.ts @@ -54,7 +54,7 @@ import {HttpHeaders} from '@angular/common/http/src/headers'; expect(() => headers.get('foo')) .toThrowError( 'Unexpected value of the `foo` header provided. ' + - 'Expecting either a string or an array, but got: `null`.'); + 'Expecting either a string, a number or an array, but got: `null`.'); }); it('should throw an error when undefined is passed as header', () => { @@ -64,7 +64,25 @@ import {HttpHeaders} from '@angular/common/http/src/headers'; expect(() => headers.get('bar')) .toThrowError( 'Unexpected value of the `bar` header provided. ' + - 'Expecting either a string or an array, but got: `undefined`.'); + 'Expecting either a string, a number or an array, but got: `undefined`.'); + }); + + it('should not throw an error when a number is passed as header', () => { + const headers = new HttpHeaders({'Content-Length': 100}); + const value = headers.get('Content-Length'); + expect(value).toEqual('100'); + }); + + it('should not throw an error when a numerical array is passed as header', () => { + const headers = new HttpHeaders({'some-key': [123]}); + const value = headers.get('some-key'); + expect(value).toEqual('123'); + }); + + it('should not throw an error when an array of strings is passed as header', () => { + const headers = new HttpHeaders({'some-key': ['myValue']}); + const value = headers.get('some-key'); + expect(value).toEqual('myValue'); }); });