Skip to content

Commit

Permalink
fix: failing tests for domain parse util function
Browse files Browse the repository at this point in the history
This commit fixes the updated domain parse util function (that uses
tldts) to make sure all tests pass.
  • Loading branch information
deepjyoti30-st committed Oct 1, 2024
1 parent 5aeab6a commit b7e0f8e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
13 changes: 10 additions & 3 deletions lib/build/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions lib/ts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,19 @@ export function getTopLevelDomainForSameSiteResolution(url: string): string {
return "localhost";
}

// According to tests, if it is an ec2 subdomain, we want to return the subdomain
// as well. With `psl` library, this was not an issue because the library
// would return a `null` value for `domain` for ec2 urls.
//
// With tldts library, it parses the ec2 urls and returns the .amazonaws.com
// as the domain (instead of `null`) so we need to do the amazonaws check
// regardless of the domain being null.
if (hostname.endsWith(".amazonaws.com")) {
return hostname;
}

let parsedURL = parse(hostname);
if (!parsedURL.domain) {
if (hostname.endsWith(".amazonaws.com") && parsedURL.publicSuffix === hostname) {
return hostname;
}
// support for .local domain
if (hostname.endsWith(".local") && !parsedURL.publicSuffix) {
return hostname;
Expand Down
7 changes: 4 additions & 3 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ describe("getTopLevelDomainForSameSiteResolution test", () => {
assert.equal(getTopLevelDomainForSameSiteResolution("http://sub.domain.co.uk"), "domain.co.uk");
});

it("should handle .amazonaws.com domains correctly", () => {
assert.equal(getTopLevelDomainForSameSiteResolution("https://my-instance.amazonaws.com"), "amazonaws.com");
});
assert.strictEqual(
getTopLevelDomainForSameSiteResolution("https://ec2-xx-yyy-zzz-0.compute-1.amazonaws.com"),
"ec2-xx-yyy-zzz-0.compute-1.amazonaws.com"
);

it("should handle .local domains correctly", () => {
assert.equal(getTopLevelDomainForSameSiteResolution("http://myserver.local"), "myserver.local");
Expand Down

0 comments on commit b7e0f8e

Please sign in to comment.