Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/dispatcher/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ class ProxyAgent extends DispatcherBase {
}

/**
* @param {string[] | Record<string, string>} headers
* @returns {Record<string, string>}
* @param {string[] | Record<string, string> | Iterable<[string, string | string[] | undefined]>} headers
* @returns {Record<string, string | string[] | undefined>}
*/
function buildHeaders (headers) {
// When using undici.fetch, the headers list is stored
Expand Down Expand Up @@ -355,7 +355,20 @@ function buildHeaders (headers) {
const headersPair = {}

for (const [key, value] of headers) {
headersPair[key] = value
if (!Object.hasOwn(headersPair, key)) {
headersPair[key] = value
continue
}

const previous = headersPair[key]
const values = []
if (previous !== undefined) {
values.push(...(Array.isArray(previous) ? previous : [previous]))
}
if (value !== undefined) {
values.push(...(Array.isArray(value) ? value : [value]))
}
headersPair[key] = values.length > 1 ? values : values[0]
}

return headersPair
Expand Down
28 changes: 28 additions & 0 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,34 @@ test('use proxy-agent with custom headers with tunneling enabled', async (t) =>
proxyAgent.close()
})

test('use proxy-agent with repeated iterable headers', async (t) => {
t = tspl(t, { plan: 1 })
const server = await buildServer()
const proxy = await buildProxy()

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new ProxyAgent(proxyUrl)

server.on('request', (req, res) => {
t.strictEqual(req.headers['x-duplicate'], 'first, second')
res.end()
})

const headers = {
* [Symbol.iterator] () {
yield ['x-duplicate', 'first']
yield ['x-duplicate', 'second']
}
}

await request(serverUrl, { dispatcher: proxyAgent, headers })

server.close()
proxy.close()
proxyAgent.close()
})

test('sending proxy-authorization in request headers should throw', async (t) => {
t = tspl(t, { plan: 5 })
const server = await buildServer()
Expand Down
Loading