add {request,response,context}#toJSON()

This commit is contained in:
TJ Holowaychuk 2013-11-14 11:30:56 -08:00
parent d9940c1931
commit 35a0c1d2bb
4 changed files with 63 additions and 8 deletions

View file

@ -345,6 +345,31 @@ module.exports = {
return this.response.attachment.apply(this.response, arguments); return this.response.attachment.apply(this.response, arguments);
}, },
/**
* Inspect implementation.
*
* @return {Object}
* @api public
*/
inspect: function(){
return this.toJSON();
},
/**
* Return JSON representation.
*
* @return {Object}
* @api public
*/
toJSON: function(){
return {
request: this.request.toJSON(),
response: this.response.toJSON()
}
},
/** /**
* Throw an error with `msg` and optional `status` * Throw an error with `msg` and optional `status`
* defaulting to 500. Note that these are user-level * defaulting to 500. Note that these are user-level

View file

@ -554,8 +554,6 @@ module.exports = {
/** /**
* Inspect implementation. * Inspect implementation.
* *
* TODO: add tests
*
* @return {Object} * @return {Object}
* @api public * @api public
*/ */
@ -574,6 +572,7 @@ module.exports = {
toJSON: function(){ toJSON: function(){
return { return {
method: this.method, method: this.method,
url: this.url,
header: this.header header: this.header
} }
} }

View file

@ -408,17 +408,12 @@ module.exports = {
/** /**
* Inspect implementation. * Inspect implementation.
* *
* TODO: add tests
*
* @return {Object} * @return {Object}
* @api public * @api public
*/ */
inspect: function(){ inspect: function(){
var o = this.toJSON(); return this.toJSON();
o.body = this.body;
o.statusString = this.statusString;
return o;
}, },
/** /**
@ -431,6 +426,7 @@ module.exports = {
toJSON: function(){ toJSON: function(){
return { return {
status: this.status, status: this.status,
string: this.statusString,
header: this.header header: this.header
} }
} }

35
test/context/toJSON.js Normal file
View file

@ -0,0 +1,35 @@
var context = require('../context');
describe('ctx.toJSON()', function(){
it('should return a json representation', function(){
var ctx = context();
ctx.req.method = 'POST';
ctx.req.url = '/items';
ctx.req.headers['content-type'] = 'text/plain';
ctx.status = 200;
ctx.body = '<p>Hey</p>';
var obj = ctx.toJSON();
var req = obj.request;
var res = obj.response;
req.should.eql({
method: 'POST',
url: '/items',
header: {
'content-type': 'text/plain'
}
});
res.should.eql({
status: 200,
string: 'OK',
header: {
'content-type': 'text/html; charset=utf-8',
'content-length': '10'
}
});
})
})