From 280538f84d49d327228699e7a2b59abd3e861575 Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Tue, 12 Nov 2019 09:32:48 +1100 Subject: [PATCH] ipv4 for tests, fix up cli tests --- test/cli/basic-test.js | 9 ++++----- test/cli/host-test.js | 31 +++++++++++++++---------------- test/common.js | 2 +- test/dot-common.js | 2 +- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/test/cli/basic-test.js b/test/cli/basic-test.js index ee05b33..56f99fb 100644 --- a/test/cli/basic-test.js +++ b/test/cli/basic-test.js @@ -1,13 +1,12 @@ -const test = require('tap').test -const common = require('./common') -const serve = common.serve +const { test } = require('tap') +const { serve, stExpect } = require('./common') test('Basic cli operation', (t) => { serve([], (req) => { req('/st.js', (er, res, body) => { t.ifError(er) && t.equal(res.statusCode, 200) && - t.equal(body.toString(), common.stExpect) + t.equal(body.toString(), stExpect) }) }, (er, stdout, stderr) => { t.ifError(er) @@ -22,7 +21,7 @@ test('Listening on localhost only', (t) => { req('/st.js', (er, res, body) => { t.ifError(er) && t.equal(res.statusCode, 200) && - t.equal(body.toString(), common.stExpect) + t.equal(body.toString(), stExpect) }) }, (er, stdout, stderr) => { t.ifError(er) diff --git a/test/cli/host-test.js b/test/cli/host-test.js index 706b2c9..d3998a6 100644 --- a/test/cli/host-test.js +++ b/test/cli/host-test.js @@ -1,15 +1,14 @@ -var os = require('os') -var tap = require('tap') -var test = tap.test -var common = require('./common') -var serve = common.serve +const os = require('os') +let { test, fail, comment } = require('tap') +const { serve } = require('./common') +const port = 1338 -var otherAddress = (() => { - var ifaces = os.networkInterfaces() - for (var iface in ifaces) { - var addrs = ifaces[iface] - for (var i = 0; i < addrs.length; ++i) { - var addr = addrs[i].address +const otherAddress = (() => { + const ifaces = os.networkInterfaces() + for (const iface in ifaces) { + const addrs = ifaces[iface] + for (let i = 0; i < addrs.length; ++i) { + const addr = addrs[i].address if (/^127\./.test(addr) || /^::1$/.test(addr)) { // loopback device continue } @@ -22,17 +21,17 @@ var otherAddress = (() => { return null })() if (!otherAddress) { - tap.fail('No non-loopback network address found', { skip: true }) + fail('No non-loopback network address found', { skip: true }) test = () => {} } else { - tap.comment('Using ' + otherAddress + ' as non-localhost address') + comment('Using ' + otherAddress + ' as non-localhost address') } function addr2url (addr, path) { if (/:/.test(addr)) { addr = '[' + addr + ']' } - addr = 'http://' + addr + ':' + common.port + addr = 'http://' + addr + ':' + port if (path) { addr += path } @@ -41,7 +40,7 @@ function addr2url (addr, path) { function testServer (name, args, addr, canConnect, cannotConnect) { test(name, (t) => { - serve(args, (req) => { + serve(args.concat(['--port', port]), (req) => { canConnect.forEach(checkConnections(t, req, true)) cannotConnect.forEach(checkConnections(t, req, false)) }, (err, stdout, stderr) => { @@ -57,7 +56,7 @@ function testServer (name, args, addr, canConnect, cannotConnect) { function checkConnections (t, req, canConnect) { return (addr) => { - var url = addr2url(addr, '/st.js') + const url = addr2url(addr, '/st.js') req(url, (er, res, body) => { if (canConnect) { t.ifError(er, url) && t.equal(res.statusCode, 200, url) diff --git a/test/common.js b/test/common.js index f3d09af..9ee57ae 100644 --- a/test/common.js +++ b/test/common.js @@ -49,7 +49,7 @@ test('setup', (t) => { console.error(e) return res.end(`Internal error: ${e.message}`) } - }).listen(() => { + }).listen(0, '127.0.0.1', () => { t.pass('listening') address = server.address() t.end() diff --git a/test/dot-common.js b/test/dot-common.js index cb1feae..f221f63 100644 --- a/test/dot-common.js +++ b/test/dot-common.js @@ -30,7 +30,7 @@ test('setup', (t) => { res.statusCode = 404 return res.end(`Not a match: ${req.url}`) } - }).listen(() => { + }).listen(0, '127.0.0.1', () => { t.pass('listening') address = server.address() t.end()