fix ctx.body= content-type override check. Thanks @jonathanong

This commit is contained in:
TJ Holowaychuk 2013-09-15 08:51:56 -07:00
parent 11913f5e4e
commit 83fd83aaf8
2 changed files with 12 additions and 2 deletions

View file

@ -153,8 +153,6 @@ module.exports = {
set body(val) { set body(val) {
this._body = val; this._body = val;
if (this.type) return;
// no content // no content
if (null == val) { if (null == val) {
var s = this.status; var s = this.status;
@ -165,6 +163,9 @@ module.exports = {
return; return;
} }
// content-type already set
if (this.responseHeader['content-type']) return;
// string // string
if ('string' == typeof val) { if ('string' == typeof val) {
this.type = ~val.indexOf('<') ? 'html' : 'text'; this.type = ~val.indexOf('<') ? 'html' : 'text';

View file

@ -22,6 +22,15 @@ function context(req, res) {
} }
describe('ctx.body=', function(){ describe('ctx.body=', function(){
describe('when Content-Type is set', function(){
it('should not override', function(){
var ctx = context();
ctx.type = 'png';
ctx.body = new Buffer('something');
assert('image/png' == ctx.responseHeader['content-type']);
})
})
describe('when a string is given', function(){ describe('when a string is given', function(){
it('should default to text', function(){ it('should default to text', function(){
var ctx = context(); var ctx = context();