Skip to content

Commit

Permalink
fix: make mock headers case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Jul 30, 2022
1 parent 1822ee6 commit 57ec203
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mock/mock-utils.js
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
30 changes: 30 additions & 0 deletions test/mock-agent.js
Expand Up @@ -2463,3 +2463,33 @@ 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()
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 57ec203

Please sign in to comment.