From 7bf5cd14dcd4babcdd653296eb26f1c7e8a2ed3c Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:28:43 +0900 Subject: [PATCH] fix(mock-agent): split set-cookie (#2619) --- lib/mock/mock-utils.js | 20 +++++++++++++++----- test/mock-agent.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index 42ea185cc0e..d6b00943564 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -188,11 +188,21 @@ function buildKey (opts) { } function generateKeyValues (data) { - return Object.entries(data).reduce((keyValuePairs, [key, value]) => [ - ...keyValuePairs, - Buffer.from(`${key}`), - Array.isArray(value) ? value.map(x => Buffer.from(`${x}`)) : Buffer.from(`${value}`) - ], []) + const keys = Object.keys(data) + const result = [] + for (let i = 0; i < keys.length; ++i) { + const key = keys[i] + const value = data[key] + const name = Buffer.from(`${key}`) + if (Array.isArray(value)) { + for (let j = 0; j < value.length; ++j) { + result.push(name, Buffer.from(`${value[j]}`)) + } + } else { + result.push(name, Buffer.from(`${value}`)) + } + } + return result } /** diff --git a/test/mock-agent.js b/test/mock-agent.js index 7c1ff660622..66c583da4c5 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -2624,3 +2624,33 @@ test('MockAgent - Sending ReadableStream body', async (t) => { t.same(await response.text(), 'test') }) + +// https://github.com/nodejs/undici/issues/2616 +test('MockAgent - headers should be array of strings (fetch)', async (t) => { + t.plan(1) + + const mockAgent = new MockAgent() + mockAgent.disableNetConnect() + setGlobalDispatcher(mockAgent) + + t.teardown(mockAgent.close.bind(mockAgent)) + + const mockPool = mockAgent.get('http://localhost:3000') + + mockPool + .intercept({ + path: '/foo', + method: 'GET' + }) + .reply(200, 'foo', { + headers: { + 'set-cookie': ['foo=bar', 'bar=baz', 'baz=qux'] + } + }) + + const response = await fetch('http://localhost:3000/foo', { + method: 'GET' + }) + + t.same(response.headers.getSetCookie(), ['foo=bar', 'bar=baz', 'baz=qux']) +})