Skip to content

Commit d48d88e

Browse files
tinovyatkindead-horse
authored andcommitted
feat: implement response.has (#1397)
1 parent 8be5626 commit d48d88e

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

docs/api/response.md

+9
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ app.use(async ctx => {
177177
const etag = ctx.response.get('ETag');
178178
```
179179

180+
### response.has(field)
181+
182+
Returns `true` if the header identified by name is currently set in the outgoing headers.
183+
The header name matching is case-insensitive.
184+
185+
```js
186+
const rateLimited = ctx.response.has('X-RateLimit-Limit');
187+
```
188+
180189
### response.set(field, value)
181190

182191
Set response header `field` to `value`:

lib/context.js

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ delegate(proto, 'response')
193193
.method('redirect')
194194
.method('remove')
195195
.method('vary')
196+
.method('has')
196197
.method('set')
197198
.method('append')
198199
.method('flushHeaders')

lib/response.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module.exports = {
149149
if (!this._explicitStatus) this.status = 200;
150150

151151
// set the content-type only if not yet set
152-
const setType = !this.header['content-type'];
152+
const setType = !this.has('Content-Type');
153153

154154
// string
155155
if ('string' == typeof val) {
@@ -419,6 +419,29 @@ module.exports = {
419419
return this.header[field.toLowerCase()] || '';
420420
},
421421

422+
/**
423+
* Returns true if the header identified by name is currently set in the outgoing headers.
424+
* The header name matching is case-insensitive.
425+
*
426+
* Examples:
427+
*
428+
* this.has('Content-Type');
429+
* // => true
430+
*
431+
* this.get('content-type');
432+
* // => true
433+
*
434+
* @param {String} field
435+
* @return {boolean}
436+
* @api public
437+
*/
438+
has(field) {
439+
return typeof this.res.hasHeader === 'function'
440+
? this.res.hasHeader(field)
441+
// Node < 7.7
442+
: field.toLowerCase() in this.headers;
443+
},
444+
422445
/**
423446
* Set header `field` to `val`, or pass
424447
* an object of header fields.

test/response/has.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
'use strict';
3+
4+
const assert = require('assert');
5+
const context = require('../helpers/context');
6+
7+
describe('ctx.response.has(name)', () => {
8+
it('should check a field value, case insensitive way', () => {
9+
const ctx = context();
10+
ctx.set('X-Foo', '');
11+
assert.ok(ctx.response.has('x-Foo'));
12+
assert.ok(ctx.has('x-foo'));
13+
});
14+
15+
it('should return false for non-existent header', () => {
16+
const ctx = context();
17+
assert.strictEqual(ctx.response.has('boo'), false);
18+
ctx.set('x-foo', 5);
19+
assert.strictEqual(ctx.has('x-boo'), false);
20+
});
21+
});

0 commit comments

Comments
 (0)