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=
This commit is contained in:
TJ Holowaychuk 2013-10-10 12:48:14 -07:00
parent c4d194488c
commit 3b2c55b68b
2 changed files with 14 additions and 2 deletions

View file

@ -187,7 +187,7 @@ module.exports = {
} }
// json // json
if (setType) this.type = 'json'; this.type = 'json';
}, },
/** /**

View file

@ -30,11 +30,23 @@ describe('ctx.body=', function(){
assert('image/png' == ctx.responseHeader['content-type']); 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 = '<em>hey</em>';
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(){ it('should override length', function(){
var ctx = context(); var ctx = context();
ctx.type = 'html'; ctx.type = 'html';
ctx.body = 'something'; ctx.body = 'something';
assert.equal(ctx.responseLength, 9); ctx.responseLength.should.equal(9);
}) })
}) })