Skip to content

Commit f97b38e

Browse files
authored
fix(mock-agent): split set-cookie (#2619)
1 parent 7bcb320 commit f97b38e

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

lib/mock/mock-utils.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,21 @@ function buildKey (opts) {
188188
}
189189

190190
function generateKeyValues (data) {
191-
return Object.entries(data).reduce((keyValuePairs, [key, value]) => [
192-
...keyValuePairs,
193-
Buffer.from(`${key}`),
194-
Array.isArray(value) ? value.map(x => Buffer.from(`${x}`)) : Buffer.from(`${value}`)
195-
], [])
191+
const keys = Object.keys(data)
192+
const result = []
193+
for (let i = 0; i < keys.length; ++i) {
194+
const key = keys[i]
195+
const value = data[key]
196+
const name = Buffer.from(`${key}`)
197+
if (Array.isArray(value)) {
198+
for (let j = 0; j < value.length; ++j) {
199+
result.push(name, Buffer.from(`${value[j]}`))
200+
}
201+
} else {
202+
result.push(name, Buffer.from(`${value}`))
203+
}
204+
}
205+
return result
196206
}
197207

198208
/**

test/mock-agent.js

+30
Original file line numberDiff line numberDiff line change
@@ -2624,3 +2624,33 @@ test('MockAgent - Sending ReadableStream body', async (t) => {
26242624

26252625
t.same(await response.text(), 'test')
26262626
})
2627+
2628+
// https://github.com/nodejs/undici/issues/2616
2629+
test('MockAgent - headers should be array of strings (fetch)', async (t) => {
2630+
t.plan(1)
2631+
2632+
const mockAgent = new MockAgent()
2633+
mockAgent.disableNetConnect()
2634+
setGlobalDispatcher(mockAgent)
2635+
2636+
t.teardown(mockAgent.close.bind(mockAgent))
2637+
2638+
const mockPool = mockAgent.get('http://localhost:3000')
2639+
2640+
mockPool
2641+
.intercept({
2642+
path: '/foo',
2643+
method: 'GET'
2644+
})
2645+
.reply(200, 'foo', {
2646+
headers: {
2647+
'set-cookie': ['foo=bar', 'bar=baz', 'baz=qux']
2648+
}
2649+
})
2650+
2651+
const response = await fetch('http://localhost:3000/foo', {
2652+
method: 'GET'
2653+
})
2654+
2655+
t.same(response.headers.getSetCookie(), ['foo=bar', 'bar=baz', 'baz=qux'])
2656+
})

0 commit comments

Comments
 (0)