change res.type= to always default charset. Closes #252

This commit is contained in:
TJ Holowaychuk 2014-04-09 09:34:50 -07:00
parent dfe8e95be4
commit 3d8ab61fa1
2 changed files with 25 additions and 4 deletions

View file

@ -243,7 +243,7 @@ module.exports = {
/**
* Set Content-Type response header with `type` through `mime.lookup()`
* when it does not contain "/", or set the Content-Type to `type` otherwise.
* when it does not contain a charset.
*
* Examples:
*
@ -258,8 +258,11 @@ module.exports = {
*/
set type(type) {
if (!~type.indexOf('/')) {
type = mime.lookup(type);
// mime
if (!~type.indexOf('/')) type = mime.lookup(type);
// charset
if (!~type.indexOf('charset')) {
var cs = mime.charsets.lookup(type);
if (cs) type += '; charset=' + cs.toLowerCase();
}

View file

@ -8,7 +8,7 @@ describe('ctx.type=', function(){
var ctx = context();
ctx.type = 'text/plain';
ctx.type.should.equal('text/plain');
ctx.response.header['content-type'].should.equal('text/plain');
ctx.response.header['content-type'].should.equal('text/plain; charset=utf-8');
})
})
@ -20,6 +20,24 @@ describe('ctx.type=', function(){
ctx.response.header['content-type'].should.equal('application/json');
})
})
describe('without a charset', function(){
it('should default the charset', function(){
var ctx = context();
ctx.type = 'text/html';
ctx.type.should.equal('text/html');
ctx.response.header['content-type'].should.equal('text/html; charset=utf-8');
})
})
describe('with a charset', function(){
it('should not default the charset', function(){
var ctx = context();
ctx.type = 'text/html; charset=foo';
ctx.type.should.equal('text/html');
ctx.response.header['content-type'].should.equal('text/html; charset=foo');
})
})
})
describe('ctx.type', function(){