Skip to content

Commit

Permalink
Fix array headers (nodejs#1598)
Browse files Browse the repository at this point in the history
* perf(util): handle header array values

* test(*): add array headers tests

* chore(mock-agent): remove skip in headers test
  • Loading branch information
mateonunez authored and crysmags committed Feb 27, 2024
1 parent 3c7f261 commit a7d20c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/core/util.js
Expand Up @@ -244,7 +244,11 @@ function parseHeaders (headers, obj = {}) {
const key = headers[i].toString().toLowerCase()
let val = obj[key]
if (!val) {
obj[key] = headers[i + 1].toString()
if (Array.isArray(headers[i + 1])) {
obj[key] = headers[i + 1]
} else {
obj[key] = headers[i + 1].toString()
}
} else {
if (!Array.isArray(val)) {
val = [val]
Expand Down
27 changes: 27 additions & 0 deletions test/mock-agent.js
Expand Up @@ -2494,3 +2494,30 @@ test('MockAgent - headers in mock dispatcher intercept should be case-insensitiv

t.end()
})

test('MockAgent - headers should be array of strings', async (t) => {
const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo', {
headers: {
'set-cookie': [
'foo=bar',
'bar=baz',
'baz=qux'
]
}
})

const { headers } = await request('http://localhost:3000/foo', {
method: 'GET'
})

t.equal(headers['set-cookie'].length, 3)
})
3 changes: 2 additions & 1 deletion test/util.js
Expand Up @@ -83,11 +83,12 @@ test('validateHandler', (t) => {
})

test('parseHeaders', (t) => {
t.plan(4)
t.plan(5)
t.same(util.parseHeaders(['key', 'value']), { key: 'value' })
t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' })
t.same(util.parseHeaders(['Key', 'Value']), { key: 'Value' })
t.same(util.parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] })
t.same(util.parseHeaders(['key', ['value1', 'value2', 'value3']]), { key: ['value1', 'value2', 'value3'] })
})

test('parseRawHeaders', (t) => {
Expand Down

0 comments on commit a7d20c9

Please sign in to comment.