Skip to content

Commit

Permalink
fix(mock-agent): split set-cookie (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored Jan 17, 2024
1 parent 7bcb320 commit f97b38e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})

0 comments on commit f97b38e

Please sign in to comment.