Skip to content

Commit

Permalink
feat: Allow using the HEAD verb to show document info only
Browse files Browse the repository at this point in the history
Use the -I command-line parameter, like curl.
  • Loading branch information
prantlf committed Nov 6, 2017
1 parent e5e1954 commit abd440e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ TLS Handshake 0.133s 0.079s
First Byte 0.174s 0.041s
Content Transfer 0.176s 0.002s
Socket Close 0.177s 0.001s

Status Code: 302
-----------------------------------
Status Code: OK (200)
```

Running `nettime` without any parameters prints usage instructions:
Expand All @@ -35,12 +35,13 @@ Options:
-e, --ignore-certificate ignore certificate errors
-f, --format <format> set output format: text, json
-H, --header <header> send specific HTTP header
-I, --head use HEAD verb to show document info only
-u, --unit <unit> set time unit: ms, s+ns
-U, --user <credentials> credentials for Basic Authentication
-h, --help output usage information
The default output format is "text" and time unit "ms".
Options -H and -U are the same as -H and -u for curl.
Options -H, -I and -U are the same as -H, -I and -u for curl.
Timings are printed to the standard output.
```

Expand Down Expand Up @@ -75,6 +76,7 @@ The input object can contain:
* `url`: string with a URL to make the request with.
* `credentials`: object with `username` and `password` string properties to be used for formatting of the Basic Authentication HTTP header.
* `headers`: object with header names as string keys and header values as string values.
* `method`: HTTP verb to use in the HTTP request: `GET` (default) or `HEAD`.
* `rejectUnauthorized`: boolean to refuse finishing the HTTPS request, is set to `true` (the default), if validation of the web site certificate fails; setting it to `false` makes the request ignore certificate errors.

The result object contains:
Expand Down
4 changes: 3 additions & 1 deletion bin/nettime
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ commander.version(pkg.version)
.option('-e, --ignore-certificate', 'ignore certificate errors')
.option('-f, --format <format>', 'set output format: text, json')
.option('-H, --header <header>', 'send specific HTTP header', collect, [])
.option('-I, --head', 'use HEAD verb to show document info only')
.option('-u, --unit <unit>', 'set time unit: ms, s+ns')
.option('-U, --user <credentials>', 'credentials for Basic Authentication')

commander.on('--help', function () {
console.log()
console.log(' The default output format is "text" and time unit "ms".')
console.log(' Options -H and -U are the same as -H and -u for curl.')
console.log(' Options -H, -I and -U are the same as -H, -I and -u for curl.')
console.log(' Timings are printed to the standard output.')
console.log()
console.log(' Examples:')
Expand Down Expand Up @@ -69,6 +70,7 @@ if (credentials) {

nettime({
url: url,
method: commander.head ? 'HEAD' : 'GET',
headers: headers,
credentials: credentials,
rejectUnauthorized: !commander.ignoreCertificate
Expand Down
1 change: 1 addition & 0 deletions lib/nettime.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function nettime (options) {
headers['authorization'] = 'Basic ' + Buffer.from(
credentials.username + ':' + credentials.password).toString('base64')
}
parameters.method = options.method
parameters.agent = false
return parameters
}
Expand Down
51 changes: 26 additions & 25 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ipAddress = '127.0.0.1'
const unsecurePort = 8899
const securePort = 9988
const servers = []
let lastHeaders
let lastHeaders, lastMethod

function createServer (protocol, port, options) {
return new Promise((resolve, reject) => {
Expand All @@ -35,6 +35,7 @@ function serve (request, response) {
const statusCode = url === '/' ? 204 : url === '/data' ? 200 : 404

lastHeaders = request.headers
lastMethod = request.method
response.writeHead(statusCode)
if (statusCode === 200) {
response.write('data')
Expand All @@ -58,24 +59,25 @@ function stopServers () {
}
}

function makeRequest (protocol, host, port, path, headers) {
function makeRequest (protocol, host, port, path, options) {
const https = protocol === 'https'
const url = protocol + '://' + host + ':' + port + (path || '')
let credentials
if (headers && headers.username) {
credentials = headers
headers = undefined
let credentials, headers, method
if (options) {
if (options.username) {
credentials = options
} else if (options.method) {
method = options.method
} else {
headers = options
}
}
return nettime(https ? {
return nettime(https || options ? {
url: url,
credentials: credentials,
headers: headers,
method: method,
rejectUnauthorized: false
} : headers ? {
url: url,
headers: headers
} : credentials ? {
url: url,
credentials: credentials
} : url)
.then(checkRequest)
}
Expand Down Expand Up @@ -203,12 +205,6 @@ test.test('test with custom headers', function (test) {
TestHeader: 'Test value'
})
.then(result => {
const timings = result.timings
test.equal(result.statusCode, 200)
test.equal(result.statusMessage, 'OK')
test.equal(Object.keys(timings).length, 5)
checkNull(timings.dnsLookup)
checkNull(timings.tlsHandshake)
test.ok(lastHeaders)
test.equal(Object.keys(lastHeaders).length, 3)
test.equal(lastHeaders.connection, 'close')
Expand All @@ -225,12 +221,6 @@ test.test('test with credentials', function (test) {
password: 'secret'
})
.then(result => {
const timings = result.timings
test.equal(result.statusCode, 200)
test.equal(result.statusMessage, 'OK')
test.equal(Object.keys(timings).length, 5)
checkNull(timings.dnsLookup)
checkNull(timings.tlsHandshake)
test.ok(lastHeaders)
test.equal(Object.keys(lastHeaders).length, 3)
test.equal(lastHeaders.connection, 'close')
Expand All @@ -241,6 +231,17 @@ test.test('test with credentials', function (test) {
.then(test.end)
})

test.test('test with the HEAD verb', function (test) {
return makeRequest('http', ipAddress, unsecurePort, '/data', {
method: 'HEAD'
})
.then(result => {
test.equal(lastMethod, 'HEAD')
})
.catch(test.threw)
.then(test.end)
})

test.test('stop testing servers', function (test) {
stopServers()
test.end()
Expand Down

0 comments on commit abd440e

Please sign in to comment.