From 8e10f12d38d01bbc13307927fae3ec55a5eff357 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sun, 24 Nov 2013 05:06:35 -0800 Subject: [PATCH] add ctx.remove() and res.remove(). Closes #100 --- docs/api/context.md | 1 + docs/api/response.md | 4 ++++ lib/context.js | 8 ++++++++ lib/request.js | 8 ++++---- lib/response.js | 17 ++++++++++++++--- test/context.js | 1 + test/response/remove.js | 11 +++++++++++ 7 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 test/response/remove.js diff --git a/docs/api/context.md b/docs/api/context.md index b83b061..c0175fd 100644 --- a/docs/api/context.md +++ b/docs/api/context.md @@ -62,6 +62,7 @@ - `ctx.redirect()` - `ctx.attachment()` - `ctx.set()` + - `ctx.remove()` - `ctx.lastModified=` - `ctx.etag=` diff --git a/docs/api/response.md b/docs/api/response.md index 450ea4b..f36e1e6 100644 --- a/docs/api/response.md +++ b/docs/api/response.md @@ -159,6 +159,10 @@ this.set({ }); ``` +### res.remove(field) + + Remove header `field`. + ### res.type Get response `Content-Type` void of parameters such as "charset". diff --git a/lib/context.js b/lib/context.js index 721763a..4711bfd 100644 --- a/lib/context.js +++ b/lib/context.js @@ -395,6 +395,14 @@ module.exports = { this.response.etag = val; }, + /** + * Delegate to Request#remove(). + */ + + remove: function() { + return this.response.remove.apply(this.response, arguments); + }, + /** * Delegate to Request#accepts(). */ diff --git a/lib/request.js b/lib/request.js index 7e87603..c8c65a7 100644 --- a/lib/request.js +++ b/lib/request.js @@ -561,19 +561,19 @@ module.exports = { * this.get('Something'); * // => undefined * - * @param {String} name + * @param {String} field * @return {String} * @api public */ - get: function(name){ + get: function(field){ var req = this.req; - switch (name = name.toLowerCase()) { + switch (field = field.toLowerCase()) { case 'referer': case 'referrer': return req.headers.referrer || req.headers.referer; default: - return req.headers[name]; + return req.headers[field]; } }, diff --git a/lib/response.js b/lib/response.js index ded63d7..30611a1 100644 --- a/lib/response.js +++ b/lib/response.js @@ -347,13 +347,13 @@ module.exports = { * this.get('content-type'); * // => "text/plain" * - * @param {String} name + * @param {String} field * @return {String} * @api public */ - get: function(name){ - return this.header[name.toLowerCase()]; + get: function(field){ + 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`. * diff --git a/test/context.js b/test/context.js index 27e2d59..66164f4 100644 --- a/test/context.js +++ b/test/context.js @@ -8,6 +8,7 @@ exports = module.exports = function(req, res){ req = req || { headers: {} }; res = res || { _headers: {} }; 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); } diff --git a/test/response/remove.js b/test/response/remove.js new file mode 100644 index 0000000..ab5ca7f --- /dev/null +++ b/test/response/remove.js @@ -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({}); + }) +})