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: make mock headers case-insensitive #1585

Merged
merged 2 commits into from Jul 31, 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
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
31 changes: 31 additions & 0 deletions test/mock-agent.js
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()
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for t.end() here, the test finishes once the asyn function promise resolve.

})