diff --git a/lib/response.js b/lib/response.js index c3df5e9..1415d07 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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(); } diff --git a/test/response/type.js b/test/response/type.js index 2fbceb5..b5c8c48 100644 --- a/test/response/type.js +++ b/test/response/type.js @@ -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(){