Skip to content

Commit

Permalink
Merge pull request #4 from mcollina/opts-body
Browse files Browse the repository at this point in the history
--body is for an inlining. Added --input to specify the body from a file. body as strings.
  • Loading branch information
David Mark Clements committed Apr 7, 2016
2 parents 7b2fca3 + 759c18e commit d7926ff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
5 changes: 3 additions & 2 deletions autocannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function start () {
method: 'm',
headers: 'H',
body: 'b',
input: 'i',
help: 'h'
},
default: {
Expand All @@ -175,8 +176,8 @@ function start () {
process.exit(1)
}

if (argv.body) {
argv.body = fs.readFileSync(argv.body)
if (argv.input) {
argv.body = fs.readFileSync(argv.input)
}

if (argv.headers) {
Expand Down
3 changes: 2 additions & 1 deletion help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Available options:
-p/--pipelining NUM The number of pipelined requests to use
-d/--duration SEC The number of seconds to run the autocannnon
-m/--method METHOD The http method to use
-b/--body FILE The body of the request
-b/--body BODY The body of the request
-i/--input FILE The body of the request
-h/--headers K=V The request headers
-j/--json Print the output as json
-l/--latency Print all the latency data
Expand Down
18 changes: 14 additions & 4 deletions lib/myhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,18 @@ function Client (opts) {

opts.headers = opts.headers || {}

if (opts.body) {
opts.headers['Content-Length'] = '' + opts.body.length
var body

if (typeof opts.body === 'string') {
body = new Buffer(opts.body)
} else if (Buffer.isBuffer(opts.body)) {
body = opts.body
} else if (opts.body) {
throw new Error('body must be either a string or a buffer')
}

if (body) {
opts.headers['Content-Length'] = '' + body.length
}

this._req = Object.keys(opts.headers)
Expand All @@ -65,8 +75,8 @@ function Client (opts) {

this._req = new Buffer(this._req + '\r\n', 'utf8')

if (opts.body) {
this._req = Buffer.concat([this._req, opts.body, new Buffer('\r\n')])
if (body) {
this._req = Buffer.concat([this._req, body, new Buffer('\r\n')])
}

this._connect()
Expand Down
23 changes: 23 additions & 0 deletions test/myhttp.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,26 @@ test('client supports sending a body', (t) => {
client.destroy()
})
})

test('client supports sending a body which is a string', (t) => {
t.plan(4)

const opts = server.address()
opts.method = 'POST'
opts.body = 'hello world'

const client = new Client(opts)

server.once('request', (req, res) => {
req.pipe(bl((err, body) => {
t.error(err)
t.deepEqual(body.toString(), opts.body, 'body matches')
}))
})

client.on('response', (statusCode, length) => {
t.equal(statusCode, 200, 'status code matches')
t.ok(length > 'hello world'.length, 'length includes the headers')
client.destroy()
})
})

0 comments on commit d7926ff

Please sign in to comment.