Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't coerce header value to string on set #1394

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 8 additions & 9 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,22 +450,21 @@ module.exports = {
* this.set('Accept', 'application/json');
* this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
*
* @param {String|Object|Array} field
* @param {String} val
* @param {String|{ [k: string]: any }} field
* @param {any} [val]
* @api public
*/

set(field, val) {
if (this.headerSent) return;
if (this.headerSent || !field) return;

if (2 === arguments.length) {
if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
else if (typeof val !== 'string') val = String(val);
if (typeof field === 'string') {
this.res.setHeader(field, val);
} else {
for (const key in field) {
this.set(key, field[key]);
}
// it must be an object with header: value
Object.keys(field).forEach(
header => this.res.setHeader(header, field[header])
);
}
},

Expand Down
2 changes: 1 addition & 1 deletion test/response/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('res.inspect()', () => {
message: 'OK',
header: {
'content-type': 'text/plain; charset=utf-8',
'content-length': '5'
'content-length': 5
},
body: 'hello'
};
Expand Down
14 changes: 7 additions & 7 deletions test/response/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ describe('ctx.set(name, val)', () => {
it('should set a field value', () => {
const ctx = context();
ctx.set('x-foo', 'bar');
assert.equal(ctx.response.header['x-foo'], 'bar');
assert.equal(ctx.response.get('X-foo'), 'bar');
});

it('should coerce number to string', () => {
it('should not coerce number to string', () => {
const ctx = context();
ctx.set('x-foo', 5);
assert.equal(ctx.response.header['x-foo'], '5');
assert.strictEqual(ctx.response.header['x-foo'], 5);
});

it('should coerce undefined to string', () => {
it('should not coerce undefined to string', () => {
const ctx = context();
ctx.set('x-foo', undefined);
assert.equal(ctx.response.header['x-foo'], 'undefined');
assert.strictEqual(ctx.response.header['x-foo'], undefined);
});

it('should set a field value of array', () => {
Expand All @@ -35,11 +35,11 @@ describe('ctx.set(object)', () => {
const ctx = context();

ctx.set({
foo: '1',
foo: 0,
bar: '2'
});

assert.equal(ctx.response.header.foo, '1');
assert.strictEqual(ctx.response.header.foo, 0);
assert.equal(ctx.response.header.bar, '2');
});
});