From 94413b1bd4f20b8c4b183c84166fdfb71087aff6 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 14 Feb 2014 09:33:10 -0800 Subject: [PATCH] add response.charset accessor and ctx.charset alias --- docs/api/context.md | 6 ++++-- docs/api/response.md | 14 ++++++++++++++ lib/context.js | 1 + lib/response.js | 25 ++++++++++++++++++++++++ test/response/charset.js | 41 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 test/response/charset.js diff --git a/docs/api/context.md b/docs/api/context.md index d9c0aab..ce0fee9 100644 --- a/docs/api/context.md +++ b/docs/api/context.md @@ -149,10 +149,12 @@ throw err; - `ctx.body=` - `ctx.status` - `ctx.status=` - - `ctx.length=` - `ctx.length` - - `ctx.type=` + - `ctx.length=` - `ctx.type` + - `ctx.type=` + - `ctx.charset` + - `ctx.charset=` - `ctx.headerSent` - `ctx.redirect()` - `ctx.attachment()` diff --git a/docs/api/response.md b/docs/api/response.md index 86458f6..f1aa379 100644 --- a/docs/api/response.md +++ b/docs/api/response.md @@ -186,6 +186,20 @@ this.type = 'png'; when explicitly defined in full as `res.type = 'text/html'` no charset is assigned. +### res.charset + + Get response charset when present, or `undefined`: + +```js +this.type = 'text/plain; charset=utf-8'; +this.charset +// => "utf-8" +``` + +### res.charset= + + Set the response charset, overriding if present. + ### res.redirect(url, [alt]) Perform a [302] redirect to `url`. diff --git a/lib/context.js b/lib/context.js index 1873a89..c339774 100644 --- a/lib/context.js +++ b/lib/context.js @@ -151,6 +151,7 @@ delegate(proto, 'response') .access('body') .access('length') .access('type') + .access('charset') .getter('headerSent') .setter('lastModified') .setter('etag'); diff --git a/lib/response.js b/lib/response.js index 10e9dec..bae33d3 100644 --- a/lib/response.js +++ b/lib/response.js @@ -17,6 +17,31 @@ var extname = path.extname; module.exports = { + /** + * Set the charset. + * + * @param {String} val + * @api public + */ + + set charset(val) { + this.set('Content-Type', this.type + '; charset=' + val); + }, + + /** + * Get the charset when present or undefined. + * + * @return {String} + * @api public + */ + + get charset() { + // TODO: lame, we could have a generic param parsing lib + var type = this.get('Content-Type'); + var m = type.match(/charset *= *(\S+)/); + if (m) return m[1]; + }, + /** * Return the request socket. * diff --git a/test/response/charset.js b/test/response/charset.js new file mode 100644 index 0000000..83d3c10 --- /dev/null +++ b/test/response/charset.js @@ -0,0 +1,41 @@ + +var context = require('../context'); +var assert = require('assert'); + +describe('ctx.charset=', function(){ + describe('with no charset present', function(){ + it('should set it', function(){ + var ctx = context(); + ctx.type = 'text/plain'; + ctx.charset = 'utf8'; + ctx.response.get('Content-Type').should.equal('text/plain; charset=utf8'); + }) + }) + + describe('with a charset', function(){ + it('should set it', function(){ + var ctx = context(); + ctx.type = 'text/plain; charset=hey'; + ctx.charset = 'utf8'; + ctx.response.get('Content-Type').should.equal('text/plain; charset=utf8'); + }) + }) +}) + +describe('ctx.charset', function(){ + describe('with no charset present', function(){ + it('should return null', function(){ + var ctx = context(); + ctx.type = 'text/plain'; + assert(null == ctx.charset); + }) + }) + + describe('with a charset', function(){ + it('should return the charset', function(){ + var ctx = context(); + ctx.type = 'text'; + ctx.charset.should.equal('utf-8'); + }) + }) +}) \ No newline at end of file