Skip to content

Commit

Permalink
ctx.response.get - use .getHeader (#1392)
Browse files Browse the repository at this point in the history
* use .getHeader

* add tests

Co-authored-by: jongleberry <[email protected]>
  • Loading branch information
tinovyatkin and jonathanong authored Aug 18, 2021
1 parent 6cf6a95 commit 5b076c5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,12 @@ module.exports = {
* // => "text/plain"
*
* @param {String} field
* @return {String}
* @return {any}
* @api public
*/

get (field) {
return this.header[field.toLowerCase()] || ''
get(field) {
return this.res.getHeader(field);
},

/**
Expand Down
37 changes: 37 additions & 0 deletions test/response/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

'use strict';

const assert = require('assert');
const context = require('../helpers/context');

describe('ctx.get(name)', () => {
it('should get a field value, case insensitive', () => {
const ctx = context();
ctx.set('X-Foo', 'bar');
assert.strictEqual(ctx.response.get('x-FOO'), 'bar');
});

it('should have the same behavior as ctx.res.getHeader on undefined and null values', () => {
const ctx = context();
ctx.res.setHeader('X-Foo', undefined);
ctx.response.header['x-boo'] = null;
assert.strictEqual(ctx.response.get('x-FOO'), ctx.res.getHeader('X-FOO'));
assert.strictEqual(ctx.response.get('x-bOO'), ctx.res.getHeader('X-BOO'));
});

it('should not convert header value type', () => {
const ctx = context();
ctx.res.setHeader('Foo-date', new Date());
ctx.response.header['foo-map'] = new Map();
ctx.res.setHeader('Foo-empty-string', '');
ctx.res.setHeader('Foo-number', 0);
ctx.res.setHeader('Foo-null', null);
ctx.res.setHeader('Foo-undefined', undefined);
assert.ok(ctx.response.get('foo-Date') instanceof Date);
assert.ok(ctx.response.get('foo-Map') instanceof Map);
assert.strictEqual(ctx.response.get('Foo-empty-String'), '');
assert.strictEqual(ctx.response.get('Foo-Number'), 0);
assert.ok(ctx.response.get('foo-NULL') === null);
assert.ok(typeof ctx.response.get('FOO-undefined') === 'undefined');
});
});

0 comments on commit 5b076c5

Please sign in to comment.