Skip to content

Commit

Permalink
fix case where Request and init was supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
berndfuhrmann committed Nov 18, 2024
1 parent 5b1dca4 commit c500852
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sdk_contrib/fetch/lib/fetch_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function enableCapture(baseFetchFunction, requestClass, downstreamXRayEnabled, s
const thisDownstreamXRayEnabled = !!downstreamXRayEnabled;
const thisSubsegmentCallback = subsegmentCallback;
// Standardize request information
const request = args[0] instanceof requestClass ?
const request = (args[0] instanceof requestClass && args.length === 1) ?
args[0] :
new requestClass(...args);

Expand Down
42 changes: 42 additions & 0 deletions sdk_contrib/fetch/test/unit/fetch_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,48 @@ describe('Unit tests', function () {
}
});

it('transfers dispatcher property for undici with Request object', async function () {
const activeFetch = captureFetch(true);
const agent = new Agent({
maxSockets: 1234
});
await activeFetch(new Request('https://www.foo.com'), {
dispatcher: agent
});
stubFetch.should.have.been.calledOnceWith(sinon.match({ url: 'https://www.foo.com/' }));
stubResolveSegment.should.have.been.called;

// check if dispatcher was transferred
const dummyRequest = new Request('https://www.foo.com', {
dispatcher: agent
});
const dispatcherSymbol = Object.getOwnPropertySymbols(dummyRequest).find(symbol => symbol.description === 'dispatcher');
if (dispatcherSymbol) {
stubFetch.should.have.been.calledOnceWith(sinon.match({ [dispatcherSymbol]: agent }));
}
});

it('transfers dispatcher property for undici with url and init', async function () {
const activeFetch = captureFetch(true);
const agent = new Agent({
maxSockets: 1234
});
await activeFetch('https://www.foo.com', {
dispatcher: agent
});
stubFetch.should.have.been.calledOnceWith(sinon.match({ url: 'https://www.foo.com/' }));
stubResolveSegment.should.have.been.called;

// check if dispatcher was transferred
const dummyRequest = new Request('https://www.foo.com', {
dispatcher: agent
});
const dispatcherSymbol = Object.getOwnPropertySymbols(dummyRequest).find(symbol => symbol.description === 'dispatcher');
if (dispatcherSymbol) {
stubFetch.should.have.been.calledOnceWith(sinon.match({ [dispatcherSymbol]: agent }));
}
});

it('calls base function when no parent and automatic mode', async function () {
const activeFetch = captureFetch(true);
stubResolveSegment.returns(null);
Expand Down

0 comments on commit c500852

Please sign in to comment.