Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array headers #1598

Merged
merged 3 commits into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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