Skip to content

Commit

Permalink
fix: Use port from endpoint url to create HttpRequestOptions
Browse files Browse the repository at this point in the history
Co-authored-by: sandeep poonia <poonias@amazon.nl>
  • Loading branch information
sandeeppoonia and sandeep poonia authored Jan 6, 2025
1 parent 032a9f5 commit b2d08c3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dispatch/DataPlaneClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
90 changes: 90 additions & 0 deletions src/dispatch/__tests__/DataPlaneClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}`);
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit b2d08c3

Please sign in to comment.