fix(http): fix Headers initialization from Headers and Object (#12106)

This commit is contained in:
Victor Berchet 2016-10-05 17:05:50 -07:00 committed by Tobias Bosch
parent a67c06708d
commit f4566f8128
2 changed files with 30 additions and 11 deletions

View File

@ -49,19 +49,16 @@ export class Headers {
}
if (headers instanceof Headers) {
headers._headers.forEach((value: string[], name: string) => {
const lcName = name.toLowerCase();
this._headers.set(lcName, value);
this.mayBeSetNormalizedName(name);
headers._headers.forEach((values: string[], name: string) => {
values.forEach(value => this.append(name, value));
});
return;
}
Object.keys(headers).forEach((name: string) => {
const value = headers[name];
const lcName = name.toLowerCase();
this._headers.set(lcName, Array.isArray(value) ? value : [value]);
this.mayBeSetNormalizedName(name);
const values: string[] = Array.isArray(headers[name]) ? headers[name] : [headers[name]];
this.delete(name);
values.forEach(value => this.append(name, value));
});
}
@ -137,8 +134,13 @@ export class Headers {
* Sets or overrides header value for given name.
*/
set(name: string, value: string|string[]): void {
const strValue = Array.isArray(value) ? value.join(',') : value;
this._headers.set(name.toLowerCase(), [strValue]);
if (Array.isArray(value)) {
if (value.length) {
this._headers.set(name.toLowerCase(), [value.join(',')]);
}
} else {
this._headers.set(name.toLowerCase(), [value]);
}
this.mayBeSetNormalizedName(name);
}

View File

@ -37,6 +37,24 @@ export function main() {
secondHeaders.append('Content-Type', 'image/jpeg');
expect(firstHeaders.has('Content-Type')).toEqual(false);
});
it('should preserve the list of values', () => {
const src = new Headers();
src.append('foo', 'a');
src.append('foo', 'b');
src.append('foo', 'c');
const dst = new Headers(src);
expect(dst.getAll('foo')).toEqual(src.getAll('foo'));
});
it('should keep the last value when initialized from an object', () => {
const headers = new Headers({
'foo': 'first',
'fOo': 'second',
});
expect(headers.getAll('foo')).toEqual(['second']);
});
});
describe('.set()', () => {
@ -140,7 +158,6 @@ export function main() {
ref = {'Accept': values};
});
it('should be serializable with toJSON',
() => { expect(JSON.stringify(headers)).toEqual(JSON.stringify(ref)); });