Skip to content

Commit

Permalink
fix(mock-agent): send stream body (#2425)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored and metcoder95 committed Nov 22, 2023
1 parent b0beb9d commit 6c82525
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ async function httpNetworkFetch (
path: url.pathname + url.search,
origin: url.origin,
method: request.method,
body: fetchParams.controller.dispatcher.isMockActive ? request.body && request.body.source : body,
body: fetchParams.controller.dispatcher.isMockActive ? request.body && (request.body.source || request.body.stream) : body,
headers: request.headersList.entries,
maxRedirections: 0,
upgrade: request.mode === 'websocket' ? 'websocket' : undefined
Expand Down
35 changes: 35 additions & 0 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2600,3 +2600,38 @@ test('MockAgent - headers should be array of strings', async (t) => {
'baz=qux'
])
})

// https://github.com/nodejs/undici/issues/2418
test('MockAgent - Sending ReadableStream body', { skip: nodeMajor < 16 }, async (t) => {
t.plan(1)
const { fetch } = require('..')
const ReadableStream = globalThis.ReadableStream || require('stream/web').ReadableStream

const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const server = createServer((req, res) => {
res.setHeader('content-type', 'text/plain')
req.pipe(res)
})

t.teardown(mockAgent.close.bind(mockAgent))
t.teardown(server.close.bind(server))

await promisify(server.listen.bind(server))(0)

const url = `http://localhost:${server.address().port}`

const response = await fetch(url, {
method: 'POST',
body: new ReadableStream({
start (controller) {
controller.enqueue('test')
controller.close()
}
}),
duplex: 'half'
})

t.same(await response.text(), 'test')
})

0 comments on commit 6c82525

Please sign in to comment.