Skip to content

Commit

Permalink
net: throw error if port/path does not exist in options
Browse files Browse the repository at this point in the history
Throw error ERR_INVALID_ARG_VALUE if the port and path properties
do not exist in the options object argument when calling
server.listen().

Refs: #16712

PR-URL: #22085
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
yanivfriedensohn authored and addaleax committed Sep 2, 2018
1 parent 7aac706 commit 4a0466f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const errors = require('internal/errors');
const {
ERR_INVALID_ADDRESS_FAMILY,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_FD_TYPE,
ERR_INVALID_IP_ADDRESS,
ERR_INVALID_OPT_VALUE,
Expand Down Expand Up @@ -1496,6 +1497,11 @@ Server.prototype.listen = function(...args) {
return this;
}

if (!(('port' in options) || ('path' in options))) {
throw new ERR_INVALID_ARG_VALUE('options', options,
'must have the property "port" or "path"');
}

throw new ERR_INVALID_OPT_VALUE('options', util.inspect(options));
};

Expand Down
30 changes: 23 additions & 7 deletions test/parallel/test-net-server-listen-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,23 @@ const listenOnPort = [
const block = () => {
net.createServer().listen(options, common.mustNotCall());
};
common.expectsError(block,
{
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: /^The value "{.*}" is invalid for option "options"$/
});

if (typeof options === 'object' &&
!(('port' in options) || ('path' in options))) {
common.expectsError(block,
{
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError,
message: /^The argument 'options' must have the property "port" or "path"\. Received .+$/,
});
} else {
common.expectsError(block,
{
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: /^The value "{.*}" is invalid for option "options"(?:\. .+)?$/,
});
}
}

shouldFailToListen(false, { port: false });
Expand All @@ -73,6 +84,11 @@ const listenOnPort = [
shouldFailToListen({ fd: -1 });
// Invalid path in listen(options)
shouldFailToListen({ path: -1 });
// Host without port

// Neither port or path are specified in options
shouldFailToListen({});
shouldFailToListen({ host: 'localhost' });
shouldFailToListen({ host: 'localhost:3000' });
shouldFailToListen({ host: { port: 3000 } });
shouldFailToListen({ exclusive: true });
}

0 comments on commit 4a0466f

Please sign in to comment.