add response.charset accessor and ctx.charset alias

This commit is contained in:
TJ Holowaychuk 2014-02-14 09:33:10 -08:00
parent c0e0c7c6aa
commit 94413b1bd4
5 changed files with 85 additions and 2 deletions

View file

@ -149,10 +149,12 @@ throw err;
- `ctx.body=` - `ctx.body=`
- `ctx.status` - `ctx.status`
- `ctx.status=` - `ctx.status=`
- `ctx.length=`
- `ctx.length` - `ctx.length`
- `ctx.type=` - `ctx.length=`
- `ctx.type` - `ctx.type`
- `ctx.type=`
- `ctx.charset`
- `ctx.charset=`
- `ctx.headerSent` - `ctx.headerSent`
- `ctx.redirect()` - `ctx.redirect()`
- `ctx.attachment()` - `ctx.attachment()`

View file

@ -186,6 +186,20 @@ this.type = 'png';
when explicitly defined in full as `res.type = 'text/html'` when explicitly defined in full as `res.type = 'text/html'`
no charset is assigned. 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]) ### res.redirect(url, [alt])
Perform a [302] redirect to `url`. Perform a [302] redirect to `url`.

View file

@ -151,6 +151,7 @@ delegate(proto, 'response')
.access('body') .access('body')
.access('length') .access('length')
.access('type') .access('type')
.access('charset')
.getter('headerSent') .getter('headerSent')
.setter('lastModified') .setter('lastModified')
.setter('etag'); .setter('etag');

View file

@ -17,6 +17,31 @@ var extname = path.extname;
module.exports = { 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. * Return the request socket.
* *

41
test/response/charset.js Normal file
View file

@ -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');
})
})
})