Skip to content

Commit

Permalink
[fix] Use null as default value for the host option
Browse files Browse the repository at this point in the history
Fixes #588
  • Loading branch information
lpinca committed Nov 16, 2016
1 parent 4056bde commit dcdc652
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
18 changes: 9 additions & 9 deletions lib/WebSocketServer.js
Expand Up @@ -43,17 +43,17 @@ function WebSocketServer (options, callback) {
EventEmitter.call(this);

options = Object.assign({
host: '0.0.0.0',
port: null,
server: null,
verifyClient: null,
maxPayload: 100 * 1024 * 1024,
perMessageDeflate: true,
handleProtocols: null,
path: null,
noServer: false,
clientTracking: true,
perMessageDeflate: true,
maxPayload: 100 * 1024 * 1024,
backlog: null // use default (511 as implemented in net.js)
verifyClient: null,
noServer: false,
backlog: null, // use default (511 as implemented in net.js)
server: null,
host: null,
path: null,
port: null
}, options);

if (options.port == null && !options.server && !options.noServer) {
Expand Down
36 changes: 23 additions & 13 deletions test/WebSocketServer.test.js
Expand Up @@ -31,19 +31,30 @@ describe('WebSocketServer', function () {
it('emits an error if http server bind fails', function (done) {
const wss1 = new WebSocketServer({ port: 50003 });
const wss2 = new WebSocketServer({ port: 50003 });
wss2.on('error', () => {
wss1.close();
done();
});

wss2.on('error', () => wss1.close(done));
});

it('starts a server on a given port', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const ws = new WebSocket(`ws://localhost:${port}`);
});
wss.on('connection', (client) => {
wss.close();
done();

wss.on('connection', (client) => wss.close(done));
});

it('binds the server on any IPv6 address when available', function (done) {
const wss = new WebSocketServer({ port: ++port }, () => {
const address = wss._server.address().address;

// The Trusty build environment does not have IPv6 connectivity.
if (process.env.TRAVIS) {
assert.strictEqual(address, '0.0.0.0');
} else {
assert.strictEqual(address, '::');
}

wss.close(done);
});
});

Expand All @@ -70,15 +81,14 @@ describe('WebSocketServer', function () {
res.on('data', (chunk) => { body += chunk; });
res.on('end', () => {
assert.strictEqual(body, http.STATUS_CODES[426]);
wss.close();
done();
wss.close(done);
});
});
});
});

// Don't test this on Windows. It throws errors for obvious reasons.
if (!/^win/i.test(process.platform)) {
if (process.platform !== 'win32') {
it('uses a precreated http server listening on unix socket', function (done) {
const server = http.createServer();
const sockPath = `/tmp/ws_socket_${new Date().getTime()}.${Math.floor(Math.random() * 1000)}`;
Expand Down Expand Up @@ -110,13 +120,13 @@ describe('WebSocketServer', function () {
});

it('will not crash when it receives an unhandled opcode', function (done) {
const wss = new WebSocketServer({ port: 8080 });
const wss = new WebSocketServer({ port: ++port });

wss.on('connection', (ws) => {
ws.onerror = () => done();
ws.onerror = () => wss.close(done);
});

const ws = new WebSocket('ws://localhost:8080/');
const ws = new WebSocket(`ws://localhost:${port}/`);

ws.onopen = () => {
ws._socket.write(new Buffer([5]));
Expand Down

0 comments on commit dcdc652

Please sign in to comment.