Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/mock/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class MockAgent extends Dispatcher {
dispatchOpts.path = `${path}?${normalizedSearchParams}`
}

if (dispatchOpts.allowH2 === false) {
Comment thread
Megashubham marked this conversation as resolved.
Outdated
delete dispatchOpts.allowH2
}

return this[kAgent].dispatch(dispatchOpts, handler)
}

Expand Down
30 changes: 30 additions & 0 deletions test/issue-5036.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { MockAgent, setGlobalDispatcher } = require('../index.js')

test('MockAgent works with native Node.js fetch', async (t) => {
// Only run if native fetch is available
if (typeof globalThis.fetch !== 'function') {
return
}

const agent = new MockAgent()
agent.disableNetConnect()

setGlobalDispatcher(agent)

agent.get('https://example.com')
.intercept({
method: 'GET',
path: '/v1/test'
})
.reply(200, { test: 123 })

const req = await globalThis.fetch('https://example.com/v1/test')
assert.strictEqual(req.status, 200)

const data = await req.json()
assert.deepStrictEqual(data, { test: 123 })
})
Loading