Skip to content

Commit

Permalink
Fix 648 and 650
Browse files Browse the repository at this point in the history
  • Loading branch information
berndfuhrmann committed Nov 18, 2024
2 parents cec94c3 + 4f05b21 commit 5b1dca4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions sdk_contrib/fetch/lib/fetch_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function transferDispatcherNodeJSBuggyUndici(request, requestClone) {
function getTransferDispatcherNodeJSBuggyUndici(requestClass) {
try {
const { Agent } = require('http');
const request = new requestClass({ dispatcher: new Agent });
const request = new requestClass('http://example.org', { dispatcher: new Agent });
const requestClone = request.clone();
const dispatcherSymbol = Object.getOwnPropertySymbols(request).find(symbol => symbol.description === 'dispatcher');
if (request[dispatcherSymbol] && !requestClone[dispatcherSymbol]) {
Expand Down Expand Up @@ -92,7 +92,7 @@ function enableCapture(baseFetchFunction, requestClass, downstreamXRayEnabled, s
const thisDownstreamXRayEnabled = !!downstreamXRayEnabled;
const thisSubsegmentCallback = subsegmentCallback;
// Standardize request information
const request = typeof args[0] === 'object' ?
const request = args[0] instanceof requestClass ?
args[0] :
new requestClass(...args);

Expand Down
20 changes: 20 additions & 0 deletions sdk_contrib/fetch/test/integration/fetch_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ describe('Integration tests', function () {
stubClose.should.have.been.calledOnce;
});

it('works with stringifyable objects', async function () {
const spyCallback = sandbox.spy();
const fetch = captureFetchGlobal(true, spyCallback);
const response = await fetch(new URL(goodUrl), {
headers: {
'foo': 'bar'
}
});
response.status.should.equal(200);
receivedHeaders.should.to.have.property('x-amzn-trace-id');
receivedHeaders.should.to.have.property('foo', 'bar');
(await response.text()).should.contain('Example');
stubIsAutomaticMode.should.have.been.called;
stubAddNewSubsegment.should.have.been.calledOnce;
stubResolveSegment.should.have.been.calledOnce;
stubAddFetchRequestData.should.have.been.calledOnce;
stubAddErrorFlag.should.not.have.been.calledOnce;
stubClose.should.have.been.calledOnce;
});

it('sets error flag on failed fetch when global fetch exists', async function () {
const spyCallback = sandbox.spy();
const fetch = captureFetchGlobal(true, spyCallback);
Expand Down
6 changes: 3 additions & 3 deletions sdk_contrib/fetch/test/unit/fetch_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ describe('Unit tests', function () {

it('short circuits if headers include trace ID', async function () {
const activeFetch = captureFetch(true);
const request = new fetchModule.Request('https://www.foo.com', {
const request = new Request('https://www.foo.com', {
headers: {
'X-Amzn-Trace-Id': '12345'
}
});
await activeFetch(request);
stubFetch.should.have.been.calledOnceWith(request);
stubFetch.should.have.been.calledOnceWith(sinon.match({ url: 'https://www.foo.com/'}));
stubResolveSegment.should.not.have.been.called;
});

Expand All @@ -185,7 +185,7 @@ describe('Unit tests', function () {
});
const dispatcherSymbol = Object.getOwnPropertySymbols(dummyRequest).find(symbol => symbol.description === 'dispatcher');
if (dispatcherSymbol) {
stubFetch.should.have.been.calledOnceWith(sinon.match({ [dispatcherSymbol]: agent }));
stubFetch.should.have.been.calledOnceWith(sinon.match({ [dispatcherSymbol]: agent }));
}
});

Expand Down

0 comments on commit 5b1dca4

Please sign in to comment.