diff --git a/lib/context.js b/lib/context.js index 1181bc5..3fc740b 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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 diff --git a/lib/request.js b/lib/request.js index 56ecd8e..c2f2550 100644 --- a/lib/request.js +++ b/lib/request.js @@ -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 } } diff --git a/lib/response.js b/lib/response.js index 20ab3bf..cf32281 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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 } } diff --git a/test/context/toJSON.js b/test/context/toJSON.js new file mode 100644 index 0000000..5a63cba --- /dev/null +++ b/test/context/toJSON.js @@ -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 = '

Hey

'; + + 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' + } + }); + }) +}) \ No newline at end of file