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

master
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);
},
/**
* 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`
* defaulting to 500. Note that these are user-level

View File

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

View File

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