add ctx.remove() and res.remove(). Closes #100

This commit is contained in:
TJ Holowaychuk 2013-11-24 05:06:35 -08:00
parent e2cd1f02ca
commit 8e10f12d38
7 changed files with 43 additions and 7 deletions

View file

@ -62,6 +62,7 @@
- `ctx.redirect()` - `ctx.redirect()`
- `ctx.attachment()` - `ctx.attachment()`
- `ctx.set()` - `ctx.set()`
- `ctx.remove()`
- `ctx.lastModified=` - `ctx.lastModified=`
- `ctx.etag=` - `ctx.etag=`

View file

@ -159,6 +159,10 @@ this.set({
}); });
``` ```
### res.remove(field)
Remove header `field`.
### res.type ### res.type
Get response `Content-Type` void of parameters such as "charset". Get response `Content-Type` void of parameters such as "charset".

View file

@ -395,6 +395,14 @@ module.exports = {
this.response.etag = val; this.response.etag = val;
}, },
/**
* Delegate to Request#remove().
*/
remove: function() {
return this.response.remove.apply(this.response, arguments);
},
/** /**
* Delegate to Request#accepts(). * Delegate to Request#accepts().
*/ */

View file

@ -561,19 +561,19 @@ module.exports = {
* this.get('Something'); * this.get('Something');
* // => undefined * // => undefined
* *
* @param {String} name * @param {String} field
* @return {String} * @return {String}
* @api public * @api public
*/ */
get: function(name){ get: function(field){
var req = this.req; var req = this.req;
switch (name = name.toLowerCase()) { switch (field = field.toLowerCase()) {
case 'referer': case 'referer':
case 'referrer': case 'referrer':
return req.headers.referrer || req.headers.referer; return req.headers.referrer || req.headers.referer;
default: default:
return req.headers[name]; return req.headers[field];
} }
}, },

View file

@ -347,13 +347,13 @@ module.exports = {
* this.get('content-type'); * this.get('content-type');
* // => "text/plain" * // => "text/plain"
* *
* @param {String} name * @param {String} field
* @return {String} * @return {String}
* @api public * @api public
*/ */
get: function(name){ get: function(field){
return this.header[name.toLowerCase()]; return this.header[field.toLowerCase()];
}, },
/** /**
@ -383,6 +383,17 @@ module.exports = {
} }
}, },
/**
* Remove header `field`.
*
* @param {String} name
* @api public
*/
remove: function(field){
this.res.removeHeader(field);
},
/** /**
* Append `val` to header `field`. * Append `val` to header `field`.
* *

View file

@ -8,6 +8,7 @@ exports = module.exports = function(req, res){
req = req || { headers: {} }; req = req || { headers: {} };
res = res || { _headers: {} }; res = res || { _headers: {} };
res.setHeader = function(k, v){ res._headers[k.toLowerCase()] = v }; res.setHeader = function(k, v){ res._headers[k.toLowerCase()] = v };
res.removeHeader = function(k, v){ delete res._headers[k.toLowerCase()] };
return koa().createContext(req, res); return koa().createContext(req, res);
} }

11
test/response/remove.js Normal file
View file

@ -0,0 +1,11 @@
var context = require('../context');
describe('ctx.remove(name)', function(){
it('should remove a field', function(){
var ctx = context();
ctx.set('x-foo', 'bar');
ctx.remove('x-foo');
ctx.response.header.should.eql({});
})
})