From 5a6e781293f88cfff3134722c0cc2a47f27e8c0a Mon Sep 17 00:00:00 2001 From: Patrick Mooney Date: Fri, 6 Jun 2014 12:21:05 -0500 Subject: [PATCH] Handle string ports properly in server.listen Parse port as an integer when possible in server.listen. In addition to fixing server.url output, this avoids some weird socket reuse behavior when mixing string and integer types passed to net.listen. Fix mcavage/node-ldapjs#196 --- lib/server.js | 4 ++++ test/server.test.js | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index 7190917..2fe3cfd 100644 --- a/lib/server.js +++ b/lib/server.js @@ -665,6 +665,10 @@ Server.prototype.listen = function (port, host, callback) { callback = host; host = '0.0.0.0'; } + if (typeof (port) === 'string' && /^[0-9]+$/.test(port)) { + // Disambiguate between string ports and file paths + port = parseInt(port, 10); + } var self = this; function _callback() { diff --git a/test/server.test.js b/test/server.test.js index 62263e3..1e6b336 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -14,6 +14,8 @@ var BIND_PW = 'secret'; var SUFFIX = 'dc=test'; +var SERVER_PORT = process.env.SERVER_PORT || 1389; + var ldap; var Attribute; var Change; @@ -44,23 +46,34 @@ test('basic create', function (t) { }); test('listen on unix/named socket', { timeout: 1000 }, function (t) { - t.plan(1); server = ldap.createServer(); sock = getSock(); server.listen(sock, function () { t.ok(true); server.close(); + t.end(); + }); +}); + +test('listen on static port', { timeout: 1000 }, function (t) { + server = ldap.createServer(); + server.listen(SERVER_PORT, '127.0.0.1', function () { + var addr = server.address(); + t.equal(addr.port, parseInt(SERVER_PORT, 10)); + t.equals(server.url, 'ldap://127.0.0.1:' + SERVER_PORT); + server.close(); + t.end(); }); }); test('listen on ephemeral port', { timeout: 1000 }, function (t) { - t.plan(2); server = ldap.createServer(); server.listen(0, 'localhost', function () { var addr = server.address(); t.ok(addr.port > 0); t.ok(addr.port < 65535); server.close(); + t.end(); }); });