Skip to content

Commit

Permalink
feat: Allow sending data with the POST verb
Browse files Browse the repository at this point in the history
Use the -d command-line parameter, like curl.
  • Loading branch information
prantlf committed Nov 6, 2017
1 parent 499fcc0 commit 8a5975b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Usage: nettime [options] <URL>
Options:
-V, --version output the version number
-d, --data <data> data to be sent using the POST verb
-e, --ignore-certificate ignore certificate errors
-f, --format <format> set output format: text, json
-H, --header <header> send specific HTTP header
Expand All @@ -44,7 +45,7 @@ Options:
-h, --help output usage information
The default output format is "text" and time unit "ms".
Options "HiIXoU" are the same as "HiIXou" for curl.
Options "HiIXdoU" are the same as "HiIXdou" for curl.
Timings are printed to the standard output.
```

Expand Down Expand Up @@ -78,8 +79,9 @@ 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.
* `data`: string or Buffer to send to the server using the HTTP verb `POST` and the content type `application/x-www-form-urlencoded` by default.
* `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`.
* `method`: HTTP verb to use in the HTTP request; `GET` is the default, unless `-i` or `-d` options are not set.
* `outputFile`: file path to write the received data to.
* `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.
* `returnResponse`: includes property `response` (`Buffer`) with the received data in the promised result object.
Expand Down
7 changes: 5 additions & 2 deletions bin/nettime
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ commander.version(pkg.version)
.description(pkg.description)
.usage('[options] <URL>')
.option('-e, --ignore-certificate', 'ignore certificate errors')
.option('-d, --data <data>', 'data to be sent using the POST verb')
.option('-f, --format <format>', 'set output format: text, json')
.option('-H, --header <header>', 'send specific HTTP header', collect, [])
.option('-i, --include', 'include response headers in the output file')
Expand All @@ -29,7 +30,7 @@ commander.version(pkg.version)
commander.on('--help', function () {
console.log()
console.log(' The default output format is "text" and time unit "ms".')
console.log(' Options "HiIXoU" are the same as "HiIXou" for curl.')
console.log(' Options "HiIXdoU" are the same as "HiIXdou" for curl.')
console.log(' Timings are printed to the standard output.')
console.log()
console.log(' Examples:')
Expand Down Expand Up @@ -73,7 +74,9 @@ if (credentials) {

nettime({
url: url,
method: commander.head ? 'HEAD' : commander.request || 'GET',
data: commander.data,
method: commander.request || (commander.head ? 'HEAD'
: commander.data ? 'POST' : 'GET'),
headers: headers,
credentials: credentials,
outputFile: commander.output,
Expand Down
17 changes: 15 additions & 2 deletions lib/nettime.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function nettime (options) {
}

start = process.hrtime()
protocol.get(parameters, localResponse => {
const request = protocol.request(parameters, localResponse => {
let firstByte
function checkFirstByte () {
if (!firstByte) {
Expand Down Expand Up @@ -102,6 +102,12 @@ function nettime (options) {
})
})
.on('error', reject)

const inputData = options.data
if (inputData) {
request.write(inputData)
}
request.end()
})

function getParameters () {
Expand All @@ -122,7 +128,14 @@ function nettime (options) {
headers['authorization'] = 'Basic ' + Buffer.from(
credentials.username + ':' + credentials.password).toString('base64')
}
parameters.method = options.method
const data = options.data
if (data) {
if (!headers['content-type']) {
headers['content-type'] = 'application/x-www-form-urlencoded'
}
headers['content-length'] = Buffer.byteLength(data)
}
parameters.method = options.method || (data ? 'POST' : 'GET')
parameters.agent = false
return parameters
}
Expand Down
46 changes: 37 additions & 9 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, lastMethod
let lastHeaders, lastMethod, lastData

function createServer (protocol, port, options) {
return new Promise((resolve, reject) => {
Expand All @@ -32,17 +32,30 @@ function readCertificate (name) {
function serve (request, response) {
setTimeout(() => {
const url = request.url
const statusCode = url === '/' ? 204 : url === '/data' ? 200 : 404
const upload = url === '/upload'
const statusCode = url === '/' ? 204 : url === '/data' || upload ? 200 : 404

function sendResponse () {
response.writeHead(statusCode, {
test: 'ok'
})
if (statusCode === 200) {
response.write('data')
}
response.end()
}

lastHeaders = request.headers
lastMethod = request.method
response.writeHead(statusCode, {
test: 'ok'
})
if (statusCode === 200) {
response.write('data')
if (upload) {
lastData = ''
request.on('data', function (data) {
lastData += data
})
.on('end', sendResponse)
} else {
sendResponse()
}
response.end()
}, 1)
}

Expand All @@ -65,7 +78,7 @@ function makeRequest (protocol, host, port, path, options) {
const https = protocol === 'https'
const url = protocol + '://' + host + ':' + port + (path || '')
let credentials, headers, method, outputFile, returnResponse
let includeHeaders
let includeHeaders, data
if (options) {
if (options.username) {
credentials = options
Expand All @@ -77,13 +90,16 @@ function makeRequest (protocol, host, port, path, options) {
} else if (options.returnResponse) {
returnResponse = true
includeHeaders = options.includeHeaders
} else if (options.data) {
data = options.data
} else {
headers = options
}
}
return nettime(https || options ? {
url: url,
credentials: credentials,
data: data,
headers: headers,
includeHeaders: includeHeaders,
method: method,
Expand Down Expand Up @@ -327,6 +343,18 @@ test.test('test writing an output file with headers', function (test) {
.then(test.end)
})

test.test('test posting data', function (test) {
return makeRequest('http', ipAddress, unsecurePort, '/upload', {
data: 'test=ok'
})
.then(result => {
test.equal(lastMethod, 'POST')
test.equal(lastData, 'test=ok')
})
.catch(test.threw)
.then(test.end)
})

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

0 comments on commit 8a5975b

Please # to comment.