From 57ec203eced0b181aebeb3090d3510988bbc9922 Mon Sep 17 00:00:00 2001 From: cola119 Date: Sun, 31 Jul 2022 08:32:26 +0900 Subject: [PATCH 1/2] fix: make mock headers case-insensitive --- lib/mock/mock-utils.js | 4 ++-- test/mock-agent.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index 58bee2a5612..7e115f83b4d 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -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] } } @@ -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()] } } diff --git a/test/mock-agent.js b/test/mock-agent.js index dc1150058f6..db4bae9c3f5 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -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() +}) From 956023c9bc0e3b47f5f8b1f4fa30ff6e1b115a06 Mon Sep 17 00:00:00 2001 From: cola119 Date: Sun, 31 Jul 2022 09:13:50 +0900 Subject: [PATCH 2/2] fixup! fix: make mock headers case-insensitive --- test/mock-agent.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/mock-agent.js b/test/mock-agent.js index db4bae9c3f5..049cc3ed6b9 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -2469,6 +2469,7 @@ test('MockAgent - headers in mock dispatcher intercept should be case-insensitiv const { fetch } = require('..') const mockAgent = new MockAgent() + mockAgent.disableNetConnect() setGlobalDispatcher(mockAgent) t.teardown(mockAgent.close.bind(mockAgent))