Skip to content

Commit

Permalink
ipv4 for tests, fix up cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 11, 2019
1 parent ea0ffb4 commit 280538f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
9 changes: 4 additions & 5 deletions test/cli/basic-test.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
31 changes: 15 additions & 16 deletions test/cli/host-test.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
}
Expand All @@ -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) => {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/dot-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 280538f

Please sign in to comment.