Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use port from endpoint url to create HttpRequestOptions: fix for 594 #595

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/dispatch/DataPlaneClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export class DataPlaneClient {
const options = {
method: METHOD,
protocol: this.config.endpoint.protocol,
port:
!this.config.endpoint.port ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we can simplify lines 117-118. Otherwise, looks great! Thank you

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup correct! I was doing a null check but of course parseInt will return NaN in that case so its not required. In fact I will simply use Number instead of parseInt because URL class already does the checks for us and we should just rely on it to do the right thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there coverage for this line?

Not sure what the point of !this.config.endpoint.port is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answered above

isNaN(parseInt(this.config.endpoint.port, 10))
? undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there test coverage for when the port is not a number, and becomes undefined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost all the other tests are for the use-case where port is not set and I have added one where port is set. I cannot add a case for invalid port because URL class does not allow that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added couple of more tests to specifically test when port is not set and validated it against the code without my change.

: parseInt(this.config.endpoint.port, 10),
headers: {
'content-type': contentType,
host: this.config.endpoint.host
Expand Down
45 changes: 45 additions & 0 deletions src/dispatch/__tests__/DataPlaneClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}`);
Expand Down Expand Up @@ -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({
Expand Down