From 0d1419ce8980e8c0f289e876448bf1e6eb755a8d Mon Sep 17 00:00:00 2001 From: Mateo Nunez Date: Thu, 11 Aug 2022 12:07:59 +0200 Subject: [PATCH] Fix array headers (#1598) * perf(util): handle header array values * test(*): add array headers tests * chore(mock-agent): remove skip in headers test --- lib/core/util.js | 6 +++++- test/mock-agent.js | 27 +++++++++++++++++++++++++++ test/util.js | 3 ++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/core/util.js b/lib/core/util.js index 5b0c5d1ef39..e9a8384ced8 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -244,7 +244,11 @@ function parseHeaders (headers, obj = {}) { const key = headers[i].toString().toLowerCase() let val = obj[key] if (!val) { - obj[key] = headers[i + 1].toString() + if (Array.isArray(headers[i + 1])) { + obj[key] = headers[i + 1] + } else { + obj[key] = headers[i + 1].toString() + } } else { if (!Array.isArray(val)) { val = [val] diff --git a/test/mock-agent.js b/test/mock-agent.js index 049cc3ed6b9..a6d6192bda4 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -2494,3 +2494,30 @@ test('MockAgent - headers in mock dispatcher intercept should be case-insensitiv t.end() }) + +test('MockAgent - headers should be array of strings', async (t) => { + const mockAgent = new MockAgent() + mockAgent.disableNetConnect() + setGlobalDispatcher(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 { headers } = await request('http://localhost:3000/foo', { + method: 'GET' + }) + + t.equal(headers['set-cookie'].length, 3) +}) diff --git a/test/util.js b/test/util.js index c47818fc7d1..2b9dd703b1f 100644 --- a/test/util.js +++ b/test/util.js @@ -83,11 +83,12 @@ test('validateHandler', (t) => { }) test('parseHeaders', (t) => { - t.plan(4) + t.plan(5) t.same(util.parseHeaders(['key', 'value']), { key: 'value' }) t.same(util.parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' }) t.same(util.parseHeaders(['Key', 'Value']), { key: 'Value' }) t.same(util.parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] }) + t.same(util.parseHeaders(['key', ['value1', 'value2', 'value3']]), { key: ['value1', 'value2', 'value3'] }) }) test('parseRawHeaders', (t) => {