mirror of https://github.com/lukechilds/node.git
Browse Source
This PR simplifies Server.prototype.listen, removing some redundancy and inconsistency. Because listen and connect have a similar function signature, normalizeConnectArgs can be reused for listen. listenAfterLookup renamed to lookupAndListen for consistency with lookupAndConnect, and moved out of Server.prototype.listen. PR-URL: https://github.com/nodejs/node/pull/4039 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Glen Keane <glenkeane.94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>v7.x
Jan Schär
8 years ago
committed by
Matteo Collina
3 changed files with 98 additions and 92 deletions
@ -1,28 +1,45 @@ |
|||
'use strict'; |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
var net = require('net'); |
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const net = require('net'); |
|||
|
|||
function close() { this.close(); } |
|||
net.Server().listen({ port: undefined }, close); |
|||
net.Server().listen({ port: '' + common.PORT }, close); |
|||
|
|||
[ 'nan', |
|||
-1, |
|||
123.456, |
|||
0x10000, |
|||
1 / 0, |
|||
-1 / 0, |
|||
'+Infinity', |
|||
'-Infinity' |
|||
].forEach(function(port) { |
|||
assert.throws(function() { |
|||
net.Server().listen({ port: port }, common.fail); |
|||
}, /"port" argument must be >= 0 and < 65536/i); |
|||
}); |
|||
// From lib/net.js
|
|||
function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; } |
|||
|
|||
function isPipeName(s) { |
|||
return typeof s === 'string' && toNumber(s) === false; |
|||
} |
|||
|
|||
const listenVariants = [ |
|||
(port, cb) => net.Server().listen({port}, cb), |
|||
(port, cb) => net.Server().listen(port, cb) |
|||
]; |
|||
|
|||
listenVariants.forEach((listenVariant, i) => { |
|||
listenVariant(undefined, common.mustCall(close)); |
|||
listenVariant('0', common.mustCall(close)); |
|||
|
|||
[ |
|||
'nan', |
|||
-1, |
|||
123.456, |
|||
0x10000, |
|||
1 / 0, |
|||
-1 / 0, |
|||
'+Infinity', |
|||
'-Infinity' |
|||
].forEach((port) => { |
|||
if (i === 1 && isPipeName(port)) { |
|||
// skip this, because listen(port) can also be listen(path)
|
|||
return; |
|||
} |
|||
assert.throws(() => listenVariant(port, common.fail), |
|||
/"port" argument must be >= 0 and < 65536/i); |
|||
}); |
|||
|
|||
[null, true, false].forEach(function(port) { |
|||
assert.throws(function() { |
|||
net.Server().listen({ port: port }, common.fail); |
|||
}, /invalid listen argument/i); |
|||
[null, true, false].forEach((port) => |
|||
assert.throws(() => listenVariant(port, common.fail), |
|||
/invalid listen argument/i)); |
|||
}); |
|||
|
Loading…
Reference in new issue