Skip to content

Commit

Permalink
fix: make mock headers case-insensitive (nodejs#1585)
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 authored and crysmags committed Feb 27, 2024
1 parent 3126a03 commit b31f799
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function lowerCaseEntries (headers) {
function getHeaderByName (headers, key) {
if (Array.isArray(headers)) {
for (let i = 0; i < headers.length; i += 2) {
if (headers[i] === key) {
if (headers[i].toLocaleLowerCase() === key.toLocaleLowerCase()) {
return headers[i + 1]
}
}
Expand All @@ -47,7 +47,7 @@ function getHeaderByName (headers, key) {
} else if (typeof headers.get === 'function') {
return headers.get(key)
} else {
return headers[key]
return lowerCaseEntries(headers)[key.toLocaleLowerCase()]
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2463,3 +2463,34 @@ test('MockAgent - using fetch yields a headers object in the reply callback', {

t.end()
})

// https://github.com/nodejs/undici/issues/1579
test('MockAgent - headers in mock dispatcher intercept should be case-insensitive', { skip: nodeMajor < 16 }, async (t) => {
const { fetch } = require('..')

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
setGlobalDispatcher(mockAgent)
t.teardown(mockAgent.close.bind(mockAgent))

const mockPool = mockAgent.get('https://example.com')

mockPool
.intercept({
path: '/',
headers: {
authorization: 'Bearer 12345',
'USER-agent': 'undici'
}
})
.reply(200)

await fetch('https://example.com', {
headers: {
Authorization: 'Bearer 12345',
'user-AGENT': 'undici'
}
})

t.end()
})

0 comments on commit b31f799

Please sign in to comment.