Skip to content

Commit

Permalink
fix 648 url object
Browse files Browse the repository at this point in the history
  • Loading branch information
berndfuhrmann committed Nov 18, 2024
1 parent 3b89008 commit 6d45f23
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
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 @@ -71,7 +71,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
5 changes: 3 additions & 2 deletions sdk_contrib/fetch/test/unit/fetch_p.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Subsegment } = require('aws-xray-sdk-core');
const { Agent } = require('http');
const fetch = require('node-fetch');

describe('Unit tests', function () {
Expand Down Expand Up @@ -157,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 Down

0 comments on commit 6d45f23

Please sign in to comment.