From cfaf852f064c40f8ef760a591d9e998ed64261da Mon Sep 17 00:00:00 2001 From: Gogs Date: Sat, 10 Jun 2017 10:47:53 -0700 Subject: [PATCH] Fix crash when port is not provided for bind. --- lib/socket/conn.js | 8 ++++++++ test/default.js | 2 +- test/eio.test.js | 4 ++++ test/tcp.test.js | 4 ++++ test/tls.test.js | 4 ++++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/socket/conn.js b/lib/socket/conn.js index 51e14e2..7e2f70f 100644 --- a/lib/socket/conn.js +++ b/lib/socket/conn.js @@ -27,6 +27,14 @@ exports.parseConnectionUri = function(uri) { throw new Error(err) } + if (!target.port) + target.port = 0 + + if (!target.host) { + target.hostname = '127.0.0.1' + target.host = '127.0.0.1' + (target.port ? ':' + target.port : '') + } + return { target: target, protocol: protocol, diff --git a/test/default.js b/test/default.js index 44d916b..7c4e337 100644 --- a/test/default.js +++ b/test/default.js @@ -3,7 +3,7 @@ var socketmq = require('../index') module.exports = function(name, T, smqServer, smqClient1, smqClient2, endpoint, options) { // 4 were in the tcp/tls/eio transport tests - T.plan(24) + T.plan(25) var serverStream1 var serverStream2 diff --git a/test/eio.test.js b/test/eio.test.js index 7598248..2248dfe 100644 --- a/test/eio.test.js +++ b/test/eio.test.js @@ -21,6 +21,10 @@ module.exports = function() { t.ok(event.stream, 'eio has stream instance in error event') }) + var noport = 'eio://' + socketmq.bind(noport) + t.pass('Bind should not crash when no port is not provided.') + testDefault('eio', t, smqServer, smqClient1, smqClient2, endpoint) }) } diff --git a/test/tcp.test.js b/test/tcp.test.js index 1460cf4..1177397 100644 --- a/test/tcp.test.js +++ b/test/tcp.test.js @@ -21,6 +21,10 @@ module.exports = function() { t.ok(event.stream, 'tcp has stream instance in error event') }) + var noport = 'tcp://' + socketmq.bind(noport) + t.pass('Bind should not crash when no port is not provided.') + testDefault('tcp', t, smqServer, smqClient1, smqClient2, endpoint) }) } diff --git a/test/tls.test.js b/test/tls.test.js index c3da4db..e5cf38f 100644 --- a/test/tls.test.js +++ b/test/tls.test.js @@ -36,6 +36,10 @@ module.exports = function() { t.ok(event.stream, 'tls has stream instance in error event') }) + var noport = 'tls://' + socketmq.bind(noport) + t.pass('Bind should not crash when no port is not provided.') + testDefault('tls', t, smqServer, smqClient1, smqClient2, endpoint, clientOptions) }) }