Skip to content

Commit

Permalink
Add support for multi-value headers in onResponse (#423)
Browse files Browse the repository at this point in the history
Co-authored-by: Elias Serneels (miaa Guard) <elias.serneels@miaaguard.com>
  • Loading branch information
Elias-Serneels and Elias Serneels (miaa Guard) authored Jan 11, 2022
1 parent c8d2435 commit 38a99f6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lib/requestIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ const hyperid = require('hyperid')(true)
const inherits = require('util').inherits
const requestBuilder = require('./httpRequestBuilder')
const clone = require('lodash.clonedeep')

const toHeaderKeyValue = (headersArray) => {
const headersKeyValue = {}

for (let i = 0; i < headersArray.length - 1; ++i) {
headersKeyValue[headersArray[i]] = headersArray[i + 1]
}

return headersKeyValue
const chunk = require('lodash.chunk')
const flatten = require('lodash.flatten')

const toHeaderKeyValue = (rawHeaders) => {
const tupleHeaders = chunk(rawHeaders, 2)
const headers = {}
tupleHeaders.forEach((val) => {
const currentValue = headers[val[0]]
if (!currentValue) {
headers[val[0]] = val[1]
} else {
headers[val[0]] = flatten([currentValue, val[1]])
}
})
return headers
}

function RequestIterator (opts) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
"hdr-histogram-percentiles-obj": "^3.0.0",
"http-parser-js": "^0.5.2",
"hyperid": "^3.0.0",
"lodash.chunk": "^4.2.0",
"lodash.clonedeep": "^4.5.0",
"lodash.flatten": "^4.4.0",
"manage-path": "^2.0.0",
"on-net-listen": "^1.1.1",
"pretty-bytes": "^5.4.1",
Expand Down
23 changes: 23 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,3 +940,26 @@ test('should get headers passed from server onResponse callback', async t => {

t.end()
})

test('should get multi-value headers passed from server onResponse callback', async t => {
t.plan(3)
const server = helper.startServer({ responses: [{ statusCode: 200, body: 'ok', headers: { 'set-cookie': [123, 456, 789] } }] })

await initJob({
url: 'http://localhost:' + server.address().port,
connections: 1,
amount: 1,
requests: [
{
method: 'GET',
onResponse (status, body, context, headers) {
t.same(status, 200)
t.same(body, 'ok')
t.same(headers['set-cookie'], [123, 456, 789])
}
}
]
})

t.end()
})

0 comments on commit 38a99f6

Please sign in to comment.