From c69b0d10333d833798a449e265e1dfad1debbf76 Mon Sep 17 00:00:00 2001 From: sandeep poonia Date: Tue, 17 Dec 2024 00:03:32 +0100 Subject: [PATCH 1/3] Use port from endpoint url to create HttpRequestOptions: fix for 594 --- src/dispatch/DataPlaneClient.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dispatch/DataPlaneClient.ts b/src/dispatch/DataPlaneClient.ts index e4db7a75..f14233ed 100644 --- a/src/dispatch/DataPlaneClient.ts +++ b/src/dispatch/DataPlaneClient.ts @@ -114,6 +114,7 @@ export class DataPlaneClient { const options = { method: METHOD, protocol: this.config.endpoint.protocol, + port: this.config.endpoint.port, headers: { 'content-type': contentType, host: this.config.endpoint.host From 797e689e28214dfba61af677355fffa59684315c Mon Sep 17 00:00:00 2001 From: sandeep poonia Date: Tue, 17 Dec 2024 01:10:57 +0100 Subject: [PATCH 2/3] convert endpoint port to number and add unit tests --- src/dispatch/DataPlaneClient.ts | 6 ++- .../__tests__/DataPlaneClient.test.ts | 45 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/dispatch/DataPlaneClient.ts b/src/dispatch/DataPlaneClient.ts index f14233ed..2c31d457 100644 --- a/src/dispatch/DataPlaneClient.ts +++ b/src/dispatch/DataPlaneClient.ts @@ -114,7 +114,11 @@ export class DataPlaneClient { const options = { method: METHOD, protocol: this.config.endpoint.protocol, - port: this.config.endpoint.port, + port: + !this.config.endpoint.port || + isNaN(parseInt(this.config.endpoint.port, 10)) + ? undefined + : parseInt(this.config.endpoint.port, 10), headers: { 'content-type': contentType, host: this.config.endpoint.host diff --git a/src/dispatch/__tests__/DataPlaneClient.test.ts b/src/dispatch/__tests__/DataPlaneClient.test.ts index bf6f7815..4382ea11 100644 --- a/src/dispatch/__tests__/DataPlaneClient.test.ts +++ b/src/dispatch/__tests__/DataPlaneClient.test.ts @@ -171,6 +171,28 @@ describe('DataPlaneClient tests', () => { ); }); + test('when the endpoint contains port then the fetch request url also contains the same port', async () => { + // Init + const endpoint = new URL('https://localhost:8080'); + const client: DataPlaneClient = createDataPlaneClient({ + ...defaultConfig, + endpoint + }); + + // Run + await client.sendFetch(Utils.PUT_RUM_EVENTS_REQUEST); + + // Assert + const signedRequest: HttpRequest = ( + fetchHandler.mock.calls[0] as any + )[0]; + expect(signedRequest.port).toEqual(8080); + expect(signedRequest.hostname).toEqual('localhost'); + expect(signedRequest.path).toEqual( + `${endpoint.pathname.replace(/\/$/, '')}/appmonitors/application123` + ); + }); + test('when the endpoint contains a path then the beacon request url contains the path prefix', async () => { // Init const endpoint = new URL(`${Utils.AWS_RUM_ENDPOINT}${'prod'}`); @@ -213,6 +235,29 @@ describe('DataPlaneClient tests', () => { ); }); + test('when the endpoint contains port then the beacon request url also contains the same port', async () => { + // Init + const endpoint = new URL('https://localhost:8080'); + const client: DataPlaneClient = createDataPlaneClient({ + ...defaultConfig, + endpoint + }); + + // Run + await client.sendBeacon(Utils.PUT_RUM_EVENTS_REQUEST); + + // Assert + const signedRequest: HttpRequest = ( + beaconHandler.mock.calls[0] as any + )[0]; + + expect(signedRequest.port).toEqual(8080); + expect(signedRequest.hostname).toEqual('localhost'); + expect(signedRequest.path).toEqual( + `${endpoint.pathname.replace(/\/$/, '')}/appmonitors/application123` + ); + }); + test('when signing is disabled then sendFetch does not sign the request', async () => { // Init const client: DataPlaneClient = createDataPlaneClient({ From ae312b04d745351ad2ac8dc8a2fc540c34b93866 Mon Sep 17 00:00:00 2001 From: sandeep poonia Date: Wed, 18 Dec 2024 22:31:19 +0100 Subject: [PATCH 3/3] simplify the port assignment and add test when port is not set --- src/dispatch/DataPlaneClient.ts | 6 +-- .../__tests__/DataPlaneClient.test.ts | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/dispatch/DataPlaneClient.ts b/src/dispatch/DataPlaneClient.ts index 2c31d457..559557c4 100644 --- a/src/dispatch/DataPlaneClient.ts +++ b/src/dispatch/DataPlaneClient.ts @@ -114,11 +114,7 @@ export class DataPlaneClient { const options = { method: METHOD, protocol: this.config.endpoint.protocol, - port: - !this.config.endpoint.port || - isNaN(parseInt(this.config.endpoint.port, 10)) - ? undefined - : parseInt(this.config.endpoint.port, 10), + port: Number(this.config.endpoint.port) || undefined, headers: { 'content-type': contentType, host: this.config.endpoint.host diff --git a/src/dispatch/__tests__/DataPlaneClient.test.ts b/src/dispatch/__tests__/DataPlaneClient.test.ts index 4382ea11..0b4b4777 100644 --- a/src/dispatch/__tests__/DataPlaneClient.test.ts +++ b/src/dispatch/__tests__/DataPlaneClient.test.ts @@ -193,6 +193,28 @@ describe('DataPlaneClient tests', () => { ); }); + test('when the endpoint does not contain port then the fetch request url also contains no port', async () => { + // Init + const endpoint = Utils.AWS_RUM_ENDPOINT; + const client: DataPlaneClient = createDataPlaneClient({ + ...defaultConfig, + endpoint + }); + + // Run + await client.sendFetch(Utils.PUT_RUM_EVENTS_REQUEST); + + // Assert + const signedRequest: HttpRequest = ( + fetchHandler.mock.calls[0] as any + )[0]; + expect(signedRequest.port).toBeUndefined(); + expect(signedRequest.hostname).toEqual(Utils.AWS_RUM_ENDPOINT.hostname); + expect(signedRequest.path).toEqual( + `${endpoint.pathname.replace(/\/$/, '')}/appmonitors/application123` + ); + }); + test('when the endpoint contains a path then the beacon request url contains the path prefix', async () => { // Init const endpoint = new URL(`${Utils.AWS_RUM_ENDPOINT}${'prod'}`); @@ -258,6 +280,29 @@ describe('DataPlaneClient tests', () => { ); }); + test('when the endpoint does not contain port then the beacon request url also does not contains port', async () => { + // Init + const endpoint = Utils.AWS_RUM_ENDPOINT; + const client: DataPlaneClient = createDataPlaneClient({ + ...defaultConfig, + endpoint + }); + + // Run + await client.sendBeacon(Utils.PUT_RUM_EVENTS_REQUEST); + + // Assert + const signedRequest: HttpRequest = ( + beaconHandler.mock.calls[0] as any + )[0]; + + expect(signedRequest.port).toBeUndefined(); + expect(signedRequest.hostname).toEqual(Utils.AWS_RUM_ENDPOINT.hostname); + expect(signedRequest.path).toEqual( + `${endpoint.pathname.replace(/\/$/, '')}/appmonitors/application123` + ); + }); + test('when signing is disabled then sendFetch does not sign the request', async () => { // Init const client: DataPlaneClient = createDataPlaneClient({