Skip to content

Commit a166af4

Browse files
dcharbonnierlpinca
authored andcommitted
[test] Skip family test if localhost doesn't resolve to ::1 (#1246)
1 parent 009d05c commit a166af4

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

test/WebSocket.test.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ const safeBuffer = require('safe-buffer');
66
const assert = require('assert');
77
const crypto = require('crypto');
88
const https = require('https');
9+
const dns = require('dns');
910
const http = require('http');
1011
const net = require('net');
1112
const fs = require('fs');
13+
const os = require('os');
1214

1315
const constants = require('../lib/Constants');
1416
const WebSocket = require('..');
@@ -100,25 +102,38 @@ describe('WebSocket', function () {
100102
});
101103

102104
it('accepts the family option', function (done) {
103-
const wss = new WebSocket.Server({ host: '::1', port: 0 }, () => {
104-
const port = wss._server.address().port;
105-
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
105+
const re = process.platform === 'win32' ? /Loopback Pseudo-Interface/ : /lo/;
106+
const ifaces = os.networkInterfaces();
107+
const hasIPv6 = Object.keys(ifaces).some((name) => {
108+
return re.test(name) && ifaces[name].some((info) => info.family === 'IPv6');
106109
});
107110

108-
wss.on('error', (err) => {
109-
wss.close(() => {
110-
//
111-
// Skip this test on machines where IPv6 is not supported.
112-
//
113-
if (err.code === 'EADDRNOTAVAIL') return this.skip();
111+
//
112+
// Skip this test on machines where IPv6 is not supported.
113+
//
114+
if (!hasIPv6) return this.skip();
115+
116+
dns.lookup('localhost', { family: 6, all: true }, (err, addresses) => {
117+
//
118+
// Skip this test if localhost does not resolve to ::1.
119+
//
120+
if (err) {
121+
return err.code === 'ENOTFOUND' || err.code === 'EAI_AGAIN'
122+
? this.skip()
123+
: done(err);
124+
}
125+
126+
if (!addresses.some((val) => val.address === '::1')) return this.skip();
114127

115-
done(err);
128+
const wss = new WebSocket.Server({ host: '::1', port: 0 }, () => {
129+
const port = wss._server.address().port;
130+
const ws = new WebSocket(`ws://localhost:${port}`, { family: 6 });
116131
});
117-
});
118132

119-
wss.on('connection', (ws, req) => {
120-
assert.strictEqual(req.connection.remoteAddress, '::1');
121-
wss.close(done);
133+
wss.on('connection', (ws, req) => {
134+
assert.strictEqual(req.connection.remoteAddress, '::1');
135+
wss.close(done);
136+
});
122137
});
123138
});
124139
});

0 commit comments

Comments
 (0)