Skip to content

Commit

Permalink
Add cli options to pass cert information (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
chiragpat committed Mar 24, 2022
1 parent a71a867 commit eec7e22
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ Available options:
Enabling this option will slow down the load testing.
--renderStatusCodes
Print status codes and their respective statistics.
--cert
Path to cert chain in pem format
--key
Path to private key for specified cert in pem format
--ca
Path to trusted ca certificates for the test. This argument accepts both a single file as well as a list of files
--debug
Print connection errors to stderr.
Print connection errors to stderr.
-v/--version
Print the version number.
-h/--help
Expand Down
32 changes: 32 additions & 0 deletions autocannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,38 @@ function parseArguments (argvs) {
}
}

argv.tlsOptions = {}

if (argv.cert) {
try {
argv.tlsOptions.cert = fs.readFileSync(argv.cert)
} catch (err) {
throw new Error(`Failed to load cert file: ${err.message}`)
}
}

if (argv.key) {
try {
argv.tlsOptions.key = fs.readFileSync(argv.key)
} catch (err) {
throw new Error(`Failed to load key file: ${err.message}`)
}
}

if (argv.ca) {
if (typeof argv.ca === 'string') {
argv.ca = [argv.ca]
} else if (Array.isArray(argv.ca._)) {
argv.ca = argv.ca._
}

try {
argv.tlsOptions.ca = argv.ca.map(caPath => fs.readFileSync(caPath))
} catch (err) {
throw new Error(`Failed to load ca file: ${err.message}`)
}
}

// This is to distinguish down the line whether it is
// run via command-line or programmatically
argv[Symbol.for('internal')] = true
Expand Down
43 changes: 43 additions & 0 deletions test/argumentParsing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,46 @@ test('parse argument with input file and multiple workers', (t) => {
t.equal(args.method, 'POST')
t.equal(args.body, fs.readFileSync(inputPath, 'utf8'))
})

test('parse argument with cert, key and multiple ca paths', (t) => {
t.plan(5)

const certPath = 'test/cert.pem'
const keyPath = 'test/key.pem'
const caPath1 = 'help.txt'
const caPath2 = 'package.json'
const args = Autocannon.parseArguments([
'-m', 'POST',
'--cert', certPath,
'--key', keyPath,
'--ca', '[', caPath1, caPath2, ']',
'http://localhost/foo/bar'
])

t.equal(args.url, 'http://localhost/foo/bar')
t.equal(args.method, 'POST')
t.same(args.tlsOptions.cert, fs.readFileSync(certPath))
t.same(args.tlsOptions.key, fs.readFileSync(keyPath))
t.same(args.tlsOptions.ca, [fs.readFileSync(caPath1), fs.readFileSync(caPath2)])
})

test('parse argument with cert, key and single ca path', (t) => {
t.plan(5)

const certPath = 'test/cert.pem'
const keyPath = 'test/key.pem'
const caPath = 'help.txt'
const args = Autocannon.parseArguments([
'-m', 'POST',
'--cert', certPath,
'--key', keyPath,
'--ca', caPath,
'http://localhost/foo/bar'
])

t.equal(args.url, 'http://localhost/foo/bar')
t.equal(args.method, 'POST')
t.same(args.tlsOptions.cert, fs.readFileSync(certPath))
t.same(args.tlsOptions.key, fs.readFileSync(keyPath))
t.same(args.tlsOptions.ca, [fs.readFileSync(caPath)])
})

0 comments on commit eec7e22

Please sign in to comment.