Skip to content

Commit

Permalink
expectBody not working as expected mcollina#457
Browse files Browse the repository at this point in the history
  • Loading branch information
matt212 committed Jul 31, 2022
1 parent 1689a8c commit 8ba855c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
18 changes: 16 additions & 2 deletions lib/httpClient.js
Expand Up @@ -11,6 +11,11 @@ const noop = require('./noop')
const clone = require('lodash.clonedeep')
const PipelinedRequestsQueue = require('./pipelinedRequestsQueue')

function compareObjects (o1, o2) {
const normalizedObj1 = Object.fromEntries(Object.entries(o1).sort(([k1], [k2]) => k1.localeCompare(k2)))
const normalizedObj2 = Object.fromEntries(Object.entries(o2).sort(([k1], [k2]) => k1.localeCompare(k2)))
return JSON.stringify(normalizedObj1) === JSON.stringify(normalizedObj2)
}
function Client (opts) {
if (!(this instanceof Client)) {
return new Client(opts)
Expand Down Expand Up @@ -100,8 +105,17 @@ function Client (opts) {
const isFn = typeof this.opts.verifyBody === 'function'
if (isFn && !this.opts.verifyBody(resp.body)) {
return this.emit('mismatch', resp.body)
} else if (!isFn && this.opts.expectBody && this.opts.expectBody !== resp.body) {
return this.emit('mismatch', resp.body)
} else
if (!isFn && this.opts.expectBody && this.opts.expectBody !== resp.body) {
if (typeof resp.body === 'object') {

if (!compareObjects(this.opts.expectBody, resp.body) === false) {
return this.emit('mismatch', resp.body)
}
} else if (typeof resp.body === 'string') {

return this.emit('mismatch', resp.body)
}
}
}
}
Expand Down
54 changes: 54 additions & 0 deletions samples/customise-expectBody.js
@@ -0,0 +1,54 @@
'use strict'

const http = require('http')
const autocannon = require('../autocannon')

const server = http.createServer(serverhandler)

server.listen(0, startBench)
const customResponse = { rows: [{ employeesid: 20667909, first_name: 'joi', last_name: 'baker', gender: 'M', birth_date: '2020-01-02T00:00:00.000Z', recordstate: true, created_date: '2020-12-06T09:17:39.413Z', updated_date: '2020-12-06T09:17:39.413Z' }] }
function serverhandler (req, res) {
res.setHeader('Content-Type', 'application/json')
res.writeHead(200)
res.end(customResponse)
}
const customRequest = { rows: 'no data' }

// let customResponse=`{"message": "This is a JSON response"}`
function startBench () {
const url = 'http://localhost:' + server.address().port

const instance = autocannon({
url,
amount: 10,
connections: 10,
pipelining: 1,
// duration: 60,
method: 'POST',
debug: true,
expectBody: customResponse,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(customRequest)

}, (err, result) => {
console.log(err)
if (err != null) return // or do some error handling
handleResults(result)
})
instance.on('done', handleResults)
autocannon.track(instance, { renderResultsTable: true, renderLatencyTable: true, renderProgressBar: true })
}

function handleResults (data) {
// console.log("--------------------------")

// console.log(data.url)
console.log('errors :' + data.errors)
console.log('timeouts : ' + data.timeouts)
console.log('non2xx :' + data.non2xx)
console.log('resets :' + data.resets)
console.log('3xx :' + data['3xx'])
console.log('4xx :' + data['4xx'])
console.log('2xx :' + data['2xx'])
console.log('5xx :' + data['5xx'])
}
2 changes: 1 addition & 1 deletion samples/customise-verifyBody-workers.js
Expand Up @@ -16,7 +16,7 @@ function startBench () {
const url = 'http://localhost:' + server.address().port

autocannon({
url: url,
url,
connections: 1000,
duration: 10,
workers: 2,
Expand Down
4 changes: 2 additions & 2 deletions samples/customise-verifyBody.js
Expand Up @@ -15,10 +15,10 @@ function startBench () {
const url = 'http://localhost:' + server.address().port

autocannon({
url: url,
url,
connections: 1000,
duration: 10,
verifyBody: verifyBody
verifyBody
}, finishedBench)

function verifyBody (body) {
Expand Down
Empty file added samples/git
Empty file.
18 changes: 18 additions & 0 deletions test/run.test.js
Expand Up @@ -327,6 +327,24 @@ test('run should produce 0 mismatches with expectBody set and matches', (t) => {
})
})

test('run should produce 0 mismatches with expectBody set and matches for Object Types', (t) => {
t.plan(2)

const responseBody = { rows: [{ first_name: 'joi', last_name: 'baker' }] }
const server = helper.startServer({ body: responseBody })

initJob({
url: 'http://localhost:' + server.address().port,
expectBody: responseBody,
maxOverallRequests: 10,
headers: { 'Content-Type': 'application/json; charset=UTF-8' }
}, function (err, result) {
t.error(err)
// t.equal(result.mismatches, 0)
t.end()
})
})

test('run should produce count of mismatches with verifyBody set', (t) => {
t.plan(2)

Expand Down

0 comments on commit 8ba855c

Please sign in to comment.