Skip to content

Commit

Permalink
add ServerResponse#toJSON()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Oct 18, 2012
1 parent 3761aa7 commit b8492f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/http.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -922,6 +922,13 @@ exports.ServerResponse = ServerResponse;


ServerResponse.prototype.statusCode = 200; ServerResponse.prototype.statusCode = 200;


ServerResponse.prototype.toJSON = function(){
return {
statusCode: this.statusCode,
headers: this._headers
}
};

function onServerResponseClose() { function onServerResponseClose() {
this._httpMessage.emit('close'); this._httpMessage.emit('close');
} }
Expand Down
29 changes: 29 additions & 0 deletions test/simple/test-http-response-tojson.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@

var common = require('../common');
var assert = require('assert');
var http = require('http');

var server = http.createServer(function(req, res){
res.statusCode = 400;
res.setHeader('Content-Length', '5');
res.setHeader('Content-Type', 'text/plain');

var obj = JSON.parse(JSON.stringify(res));

assert.deepEqual(obj, {
statusCode: 400,
headers: {
'content-length': '5',
'content-type': 'text/plain'
}
});

process.exit(0);
});

server.listen(common.PORT);

http.get({
port: common.PORT,
path: '/something'
});

0 comments on commit b8492f6

Please sign in to comment.