perf: avoid stringify when set header (#1220)
This commit is contained in:
parent
c6b8782553
commit
0b930665a8
3 changed files with 11 additions and 4 deletions
|
@ -439,8 +439,8 @@ module.exports = {
|
||||||
if (this.headerSent) return;
|
if (this.headerSent) return;
|
||||||
|
|
||||||
if (2 == arguments.length) {
|
if (2 == arguments.length) {
|
||||||
if (Array.isArray(val)) val = val.map(String);
|
if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
|
||||||
else val = String(val);
|
else if (typeof val !== 'string') val = String(val);
|
||||||
this.res.setHeader(field, val);
|
this.res.setHeader(field, val);
|
||||||
} else {
|
} else {
|
||||||
for (const key in field) {
|
for (const key in field) {
|
||||||
|
|
|
@ -10,7 +10,8 @@ describe('res.header', () => {
|
||||||
it('should return the response header object', () => {
|
it('should return the response header object', () => {
|
||||||
const res = response();
|
const res = response();
|
||||||
res.set('X-Foo', 'bar');
|
res.set('X-Foo', 'bar');
|
||||||
assert.deepEqual(res.header, { 'x-foo': 'bar' });
|
res.set('X-Number', 200);
|
||||||
|
assert.deepEqual(res.header, { 'x-foo': 'bar', 'x-number': '200' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use res.getHeaders() accessor when available', () => {
|
it('should use res.getHeaders() accessor when available', () => {
|
||||||
|
|
|
@ -11,12 +11,18 @@ describe('ctx.set(name, val)', () => {
|
||||||
assert.equal(ctx.response.header['x-foo'], 'bar');
|
assert.equal(ctx.response.header['x-foo'], 'bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should coerce to a string', () => {
|
it('should coerce number to string', () => {
|
||||||
const ctx = context();
|
const ctx = context();
|
||||||
ctx.set('x-foo', 5);
|
ctx.set('x-foo', 5);
|
||||||
assert.equal(ctx.response.header['x-foo'], '5');
|
assert.equal(ctx.response.header['x-foo'], '5');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should coerce undefined to string', () => {
|
||||||
|
const ctx = context();
|
||||||
|
ctx.set('x-foo', undefined);
|
||||||
|
assert.equal(ctx.response.header['x-foo'], 'undefined');
|
||||||
|
});
|
||||||
|
|
||||||
it('should set a field value of array', () => {
|
it('should set a field value of array', () => {
|
||||||
const ctx = context();
|
const ctx = context();
|
||||||
ctx.set('x-foo', ['foo', 'bar']);
|
ctx.set('x-foo', ['foo', 'bar']);
|
||||||
|
|
Loading…
Reference in a new issue