Skip to content

Commit e0d32ca

Browse files
KhafraDevronag
authored andcommitted
fix: mock node fetch with options.body fn
1 parent 84c8a02 commit e0d32ca

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

lib/fetch/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const EE = require('events')
5151
const { Readable, pipeline } = require('stream')
5252
const { isErrored, isReadable } = require('../core/util')
5353
const { dataURLProcessor } = require('./dataURL')
54-
const { kIsMockActive } = require('../mock/mock-symbols')
5554
const { TransformStream } = require('stream/web')
5655

5756
/** @type {import('buffer').resolveObjectURL} */
@@ -1912,7 +1911,7 @@ async function httpNetworkFetch (
19121911
path: url.pathname + url.search,
19131912
origin: url.origin,
19141913
method: request.method,
1915-
body: fetchParams.controller.dispatcher[kIsMockActive] ? request.body && request.body.source : body,
1914+
body: fetchParams.controller.dispatcher.isActive ? request.body && request.body.source : body,
19161915
headers: [...request.headersList].flat(),
19171916
maxRedirections: 0,
19181917
bodyTimeout: 300_000,

lib/mock/mock-agent.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ class MockAgent extends Dispatcher {
9696
this[kNetConnect] = false
9797
}
9898

99+
// This is required to bypass issues caused by using global symbols - see:
100+
// https://github.com/nodejs/undici/issues/1447
101+
get isActive () {
102+
return this[kIsMockActive]
103+
}
104+
99105
[kMockAgentSet] (origin, dispatcher) {
100106
this[kClients].set(origin, new FakeWeakRef(dispatcher))
101107
}

lib/mock/mock-utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
kMockAgent,
77
kOriginalDispatch,
88
kOrigin,
9-
kIsMockActive,
109
kGetNetConnect
1110
} = require('./mock-symbols')
1211

@@ -302,7 +301,7 @@ function buildMockDispatch () {
302301
const originalDispatch = this[kOriginalDispatch]
303302

304303
return function dispatch (opts, handler) {
305-
if (agent[kIsMockActive]) {
304+
if (agent.isActive) {
306305
try {
307306
mockDispatch.call(this, opts, handler)
308307
} catch (error) {

0 commit comments

Comments
 (0)