add req.toJSON() / res.toJSON() #4163

Closed
wants to merge 3 commits into from
View
15 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);
@@ -914,6 +922,13 @@ exports.ServerResponse = ServerResponse;
ServerResponse.prototype.statusCode = 200;
+ServerResponse.prototype.toJSON = function(){
+ return {
+ statusCode: this.statusCode,
+ headers: this._headers
+ }
+};
+
function onServerResponseClose() {
this._httpMessage.emit('close');
}
View
44 test/simple/test-http-request-tojson.js
@@ -0,0 +1,44 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+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);

Is .listen() synchronous now? Or should the http.get() call be moved into a listen callback function?

@tj
tj added a note Oct 19, 2012

forgot :D i'll fix this soonish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+http.get({
+ port: common.PORT,
+ path: '/something'
+});
View
49 test/simple/test-http-response-tojson.js
@@ -0,0 +1,49 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+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'
+});