From 83fd83aaf89918390852bee7e0669adb5f1f5621 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sun, 15 Sep 2013 08:51:56 -0700 Subject: [PATCH] fix ctx.body= content-type override check. Thanks @jonathanong --- lib/context.js | 5 +++-- test/context.js | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) 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();