diff --git a/src/dispatch/DataPlaneClient.ts b/src/dispatch/DataPlaneClient.ts index e4db7a75..559557c4 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: 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 bf6f7815..0b4b4777 100644 --- a/src/dispatch/__tests__/DataPlaneClient.test.ts +++ b/src/dispatch/__tests__/DataPlaneClient.test.ts @@ -171,6 +171,50 @@ 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 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'}`); @@ -213,6 +257,52 @@ 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 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({