File tree 4 files changed +55
-1
lines changed
4 files changed +55
-1
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,15 @@ app.use(async ctx => {
177
177
const etag = ctx .response .get (' ETag' );
178
178
```
179
179
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
+
180
189
### response.set(field, value)
181
190
182
191
Set response header ` field ` to ` value ` :
Original file line number Diff line number Diff line change @@ -193,6 +193,7 @@ delegate(proto, 'response')
193
193
. method ( 'redirect' )
194
194
. method ( 'remove' )
195
195
. method ( 'vary' )
196
+ . method ( 'has' )
196
197
. method ( 'set' )
197
198
. method ( 'append' )
198
199
. method ( 'flushHeaders' )
Original file line number Diff line number Diff line change @@ -149,7 +149,7 @@ module.exports = {
149
149
if ( ! this . _explicitStatus ) this . status = 200 ;
150
150
151
151
// set the content-type only if not yet set
152
- const setType = ! this . header [ 'content-type' ] ;
152
+ const setType = ! this . has ( 'Content-Type' ) ;
153
153
154
154
// string
155
155
if ( 'string' == typeof val ) {
@@ -419,6 +419,29 @@ module.exports = {
419
419
return this . header [ field . toLowerCase ( ) ] || '' ;
420
420
} ,
421
421
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
+
422
445
/**
423
446
* Set header `field` to `val`, or pass
424
447
* an object of header fields.
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments