Permalink
Browse files

add IncomingMessage#toJSON()

  • Loading branch information...
1 parent c11c19b commit 3761aa7a97ffb45761e69c97c34bdbe4e73770f7 @tj committed Oct 18, 2012
Showing with 32 additions and 0 deletions.
  1. +8 −0 lib/http.js
  2. +24 −0 test/simple/test-http-request-tojson.js
View
8 lib/http.js
@@ -295,6 +295,14 @@ util.inherits(IncomingMessage, Stream);
exports.IncomingMessage = IncomingMessage;
+IncomingMessage.prototype.toJSON = function(){
+ return {
+ url: this.url,
+ method: this.method,
+ headers: this.headers,
+ httpVersion: this.httpVersion
+ }
+};
IncomingMessage.prototype.destroy = function(error) {
this.socket.destroy(error);
View
24 test/simple/test-http-request-tojson.js
@@ -0,0 +1,24 @@
+
+var common = require('../common');
+var assert = require('assert');
+var http = require('http');
+
+var server = http.createServer(function(req, res){
+ var obj = JSON.parse(JSON.stringify(req));
+
+ assert.deepEqual(obj, {
+ method: 'GET',
+ url: '/something',
+ headers: { host: 'localhost:' + common.PORT, connection: 'keep-alive' },
+ httpVersion: '1.1'
+ });
+
+ process.exit(0);
+});
+
+server.listen(common.PORT);
+
+http.get({
+ port: common.PORT,
+ path: '/something'
+});

0 comments on commit 3761aa7

Please sign in to comment.