From cff79eb1d464b7d31e9bcdda479fb474829b0646 Mon Sep 17 00:00:00 2001 From: Maximilian Prusch Date: Wed, 5 Jun 2024 23:53:31 +0200 Subject: [PATCH] Pass dispatcher options to global fetch --- sdk_contrib/fetch/lib/fetch_p.js | 6 +++++- sdk_contrib/fetch/test/unit/fetch_p.test.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/sdk_contrib/fetch/lib/fetch_p.js b/sdk_contrib/fetch/lib/fetch_p.js index d4ba1769..6e213097 100644 --- a/sdk_contrib/fetch/lib/fetch_p.js +++ b/sdk_contrib/fetch/lib/fetch_p.js @@ -63,6 +63,10 @@ const enableCapture = function enableCapture(baseFetchFunction, requestClass, do const request = typeof args[0] === 'object' ? args[0] : new requestClass(...args); + let fetchOptions = undefined; + if (args[1] && 'dispatcher' in args[1]) { + fetchOptions = { dispatcher: args[1].dispatcher }; + } // Facilitate the addition of Segment information via the request arguments const params = args.length > 1 ? args[1] : {}; @@ -116,7 +120,7 @@ const enableCapture = function enableCapture(baseFetchFunction, requestClass, do const requestClone = request.clone(); let response; try { - response = await baseFetchFunction(requestClone); + response = await baseFetchFunction(requestClone, fetchOptions); if (thisSubsegmentCallback) { thisSubsegmentCallback(subsegment, requestClone, response); diff --git a/sdk_contrib/fetch/test/unit/fetch_p.test.js b/sdk_contrib/fetch/test/unit/fetch_p.test.js index 9dbc5128..57c14897 100644 --- a/sdk_contrib/fetch/test/unit/fetch_p.test.js +++ b/sdk_contrib/fetch/test/unit/fetch_p.test.js @@ -352,6 +352,17 @@ describe('Unit tests', function () { response.should.equal(stubValidResponse); }); + it('resolves to response through proxy when fetch options are supplied', async function() { + const activeFetch = captureFetch(true); + const proxyStub = sinon.stub(); + const request = new FetchRequest('https://www.foo.com/test'); + const response = await activeFetch(request, { + dispatcher: proxyStub + }); + stubFetch.should.have.been.calledOnceWith(request, {dispatcher: proxyStub}); + response.should.equal(stubValidResponse); + }); + it('calls subsegmentCallback with error upon fetch throwing', async function () { const spyCallback = sandbox.spy(); const activeFetch = captureFetch(true, spyCallback);