add charset support to ctx.type=

master
TJ Holowaychuk 2013-09-14 14:48:33 -07:00
parent 167530a9e6
commit 04f4d72692
3 changed files with 12 additions and 3 deletions

View File

@ -275,12 +275,16 @@ var ct = this.type;
Set response `Content-Type` via mime string or file extension.
```js
this.type = 'text/plain; charset=utf-8';
this.type = 'image/png';
this.type = '.png';
this.type = 'png';
```
__NOTE__: when `ctx.body` is an object the content-type is set for you.
Note: when appropriate a `charset` is selected for you, for
example `ctx.type = 'html'` will default to "utf-8", however
when explicitly defined in full as `ctx.type = 'text/html'`
no charset is assigned.
### ctx.url

View File

@ -743,7 +743,12 @@ module.exports = {
*/
set type(type){
if (!~type.indexOf('/')) type = mime.lookup(type);
if (!~type.indexOf('/')) {
type = mime.lookup(type);
var cs = mime.charsets.lookup(type);
if (cs) type += '; charset=' + cs.toLowerCase();
}
this.set('Content-Type', type);
},

View File

@ -177,7 +177,7 @@ describe('app.respond', function(){
request(server)
.get('/')
.expect('Content-Type', 'text/plain')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(404)
.end(done);
})