Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Target localhost by default if $PORT is set, closes #146. #147

Merged
merged 5 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions autocannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const minimist = require('minimist')
const fs = require('fs')
const path = require('path')
const URL = require('url').URL
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you throw an error if URL is not available?

const help = fs.readFileSync(path.join(__dirname, 'help.txt'), 'utf8')
const run = require('./lib/run')
const track = require('./lib/progressTracker')
Expand Down Expand Up @@ -64,6 +65,28 @@ function parseArguments (argvs) {

argv.url = argv._[0]

// if PORT is set (like by `0x`), target `localhost:PORT/path` by default.
// this allows doing:
// 0x --on-port 'autocannon /path' -- node server.js
if (process.env.PORT) {
argv.url = new URL(argv.url, `http://localhost:${process.env.PORT}`).href
}
// Add http:// if it's not there and this is not a /path
if (argv.url.indexOf('http') !== 0 && argv.url[0] !== '/') {
argv.url = `http://${argv.url}`
}

// check that the URL is valid.
try {
new URL(argv.url) // eslint-disable-line no-new
} catch (err) {
console.error(err.message)
console.error('')
console.error('When targeting a path without a hostname, the PORT environment variable must be available.')
console.error('Use a full URL or set the PORT variable.')
process.exit(1)
}

// support -n to disable the progress bar and results table
if (argv.n) {
argv.renderProgressBar = false
Expand Down
61 changes: 61 additions & 0 deletions test/envPort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

const t = require('tap')
const split = require('split2')
const path = require('path')
const childProcess = require('child_process')
const helper = require('./helper')

const lines = [
/Running 1s test @ .*$/,
/10 connections.*$/,
/$/,
/Stat.*Avg.*Stdev.*Max.*$/,
/Latency \(ms\).*$/,
/Req\/Sec.*$/,
/Bytes\/Sec.*$/,
/$/,
/.* requests in \d+s, .* read/
]

t.plan(lines.length * 2 + 2)

const server = helper.startServer()
const port = server.address().port
const url = '/path' // no hostname

const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..'), '-d', '1', url], {
cwd: __dirname,
env: Object.assign({}, process.env, {
PORT: port
}),
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
})

t.tearDown(() => {
child.kill()
})

child
.stderr
.pipe(split())
.on('data', (line) => {
const regexp = lines.shift()
t.ok(regexp, 'we are expecting this line')
t.ok(regexp.test(line), 'line matches ' + regexp)
})
.on('end', () => {
t.ok(server.autocannonConnects > 0, 'targeted the correct port')
})

const noPortChild = childProcess.spawn(process.execPath, [path.join(__dirname, '..'), url], {
cwd: __dirname,
env: process.env,
stdio: ['ignore', 'pipe', 'pipe'],
detached: false
})

noPortChild.on('exit', (code) => {
t.equal(code, 1, 'should exit with error when a hostless URL is passed and no PORT var is available')
})