diff --git a/lib/context.js b/lib/context.js index 592aadd..caf905f 100644 --- a/lib/context.js +++ b/lib/context.js @@ -153,8 +153,6 @@ module.exports = { set body(val) { this._body = val; - if (this.type) return; - // no content if (null == val) { var s = this.status; @@ -165,6 +163,9 @@ module.exports = { return; } + // content-type already set + if (this.responseHeader['content-type']) return; + // string if ('string' == typeof val) { this.type = ~val.indexOf('<') ? 'html' : 'text'; diff --git a/test/context.js b/test/context.js index aff3021..01a7fa1 100644 --- a/test/context.js +++ b/test/context.js @@ -22,6 +22,15 @@ function context(req, res) { } 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(){ it('should default to text', function(){ var ctx = context();