From 6d45f230e6293084a2198b7efd6b5e0a10e136e7 Mon Sep 17 00:00:00 2001 From: Bernd Fuhrmann Date: Mon, 18 Nov 2024 10:51:37 +0100 Subject: [PATCH 1/4] fix 648 url object --- sdk_contrib/fetch/lib/fetch_p.js | 2 +- .../fetch/test/integration/fetch_p.test.js | 20 +++++++++++++++++++ sdk_contrib/fetch/test/unit/fetch_p.test.js | 5 +++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/sdk_contrib/fetch/lib/fetch_p.js b/sdk_contrib/fetch/lib/fetch_p.js index dd7c4f2c..dc8b1dca 100644 --- a/sdk_contrib/fetch/lib/fetch_p.js +++ b/sdk_contrib/fetch/lib/fetch_p.js @@ -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); diff --git a/sdk_contrib/fetch/test/integration/fetch_p.test.js b/sdk_contrib/fetch/test/integration/fetch_p.test.js index 3d633e38..25ffccc2 100644 --- a/sdk_contrib/fetch/test/integration/fetch_p.test.js +++ b/sdk_contrib/fetch/test/integration/fetch_p.test.js @@ -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); diff --git a/sdk_contrib/fetch/test/unit/fetch_p.test.js b/sdk_contrib/fetch/test/unit/fetch_p.test.js index 4dde8d52..0d008036 100644 --- a/sdk_contrib/fetch/test/unit/fetch_p.test.js +++ b/sdk_contrib/fetch/test/unit/fetch_p.test.js @@ -1,4 +1,5 @@ const { Subsegment } = require('aws-xray-sdk-core'); +const { Agent } = require('http'); const fetch = require('node-fetch'); describe('Unit tests', function () { @@ -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; }); From 4f05b21044bc9cf220a2d6eaba9608a01220576c Mon Sep 17 00:00:00 2001 From: Bernd Fuhrmann Date: Mon, 18 Nov 2024 11:19:09 +0100 Subject: [PATCH 2/4] fix linter warning --- sdk_contrib/fetch/test/unit/fetch_p.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk_contrib/fetch/test/unit/fetch_p.test.js b/sdk_contrib/fetch/test/unit/fetch_p.test.js index 0d008036..858689f8 100644 --- a/sdk_contrib/fetch/test/unit/fetch_p.test.js +++ b/sdk_contrib/fetch/test/unit/fetch_p.test.js @@ -1,5 +1,4 @@ const { Subsegment } = require('aws-xray-sdk-core'); -const { Agent } = require('http'); const fetch = require('node-fetch'); describe('Unit tests', function () { From e7566963c5552b5d094dea760f72da6933d89cd4 Mon Sep 17 00:00:00 2001 From: Bernd Fuhrmann Date: Mon, 18 Nov 2024 14:03:27 +0100 Subject: [PATCH 3/4] fix --- sdk_contrib/fetch/lib/fetch_p.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk_contrib/fetch/lib/fetch_p.js b/sdk_contrib/fetch/lib/fetch_p.js index dc8b1dca..20596184 100644 --- a/sdk_contrib/fetch/lib/fetch_p.js +++ b/sdk_contrib/fetch/lib/fetch_p.js @@ -71,7 +71,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); From 96043e309e9920c6b515c711c30a5264c3253746 Mon Sep 17 00:00:00 2001 From: Bernd Fuhrmann Date: Fri, 22 Nov 2024 08:46:34 +0100 Subject: [PATCH 4/4] fix test --- sdk_contrib/fetch/test/unit/fetch_p.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk_contrib/fetch/test/unit/fetch_p.test.js b/sdk_contrib/fetch/test/unit/fetch_p.test.js index 858689f8..7f68c336 100644 --- a/sdk_contrib/fetch/test/unit/fetch_p.test.js +++ b/sdk_contrib/fetch/test/unit/fetch_p.test.js @@ -157,7 +157,7 @@ describe('Unit tests', function () { it('short circuits if headers include trace ID', async function () { const activeFetch = captureFetch(true); - const request = new Request('https://www.foo.com', { + const request = new FetchRequest('https://www.foo.com', { headers: { 'X-Amzn-Trace-Id': '12345' }