From 3b2c55b68b557164cf25beef1f61e29c742575c3 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Thu, 10 Oct 2013 12:48:14 -0700 Subject: [PATCH] add overriding to application/json on ctx.body=object since it cant be anything else, but if you have middleware that transforms the object to xml or something then you could set ctx.type= --- lib/context.js | 2 +- test/context.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/context.js b/lib/context.js index dbd796e..5085548 100644 --- a/lib/context.js +++ b/lib/context.js @@ -187,7 +187,7 @@ module.exports = { } // json - if (setType) this.type = 'json'; + this.type = 'json'; }, /** diff --git a/test/context.js b/test/context.js index ba916da..ab0604b 100644 --- a/test/context.js +++ b/test/context.js @@ -30,11 +30,23 @@ describe('ctx.body=', function(){ assert('image/png' == ctx.responseHeader['content-type']); }) + describe('when body is an object', function(){ + it('should override as json', function(){ + var ctx = context(); + + ctx.body = 'hey'; + assert('text/html; charset=utf-8' == ctx.responseHeader['content-type']); + + ctx.body = { foo: 'bar' }; + assert('application/json' == ctx.responseHeader['content-type']); + }) + }) + it('should override length', function(){ var ctx = context(); ctx.type = 'html'; ctx.body = 'something'; - assert.equal(ctx.responseLength, 9); + ctx.responseLength.should.equal(9); }) })