Skip to content

Commit

Permalink
Review changes + tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnj committed Dec 1, 2023
1 parent ab22847 commit a535cc3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 31 deletions.
28 changes: 28 additions & 0 deletions core/test/lib/url-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,32 @@ describe('UrlUtils', () => {
}).toThrow('INVALID_URL');
});
});

describe('getRootDomain', () => {
it('returns the correct rootDomain from a string from PSL', () => {
assert.equal(UrlUtils.getRootDomain('https://www.example.com/index.html'), 'example.com');
assert.equal(UrlUtils.getRootDomain('https://example.com'), 'example.com');
assert.equal(UrlUtils.getRootDomain('https://www.example.co.uk'), 'example.co.uk');
assert.equal(UrlUtils.getRootDomain('https://example.com.br/app/'), 'example.com.br');
assert.equal(UrlUtils.getRootDomain('https://example.tokyo.jp'), 'example.tokyo.jp');
assert.equal(UrlUtils.getRootDomain('https://sub.example.com'), 'example.com');
assert.equal(UrlUtils.getRootDomain('https://sub.example.tokyo.jp'), 'example.tokyo.jp');
assert.equal(UrlUtils.getRootDomain('http://localhost'), 'localhost');
assert.equal(UrlUtils.getRootDomain('http://localhost:8080'), 'localhost');
assert.equal(UrlUtils.getRootDomain('https://www.hydro.mb.ca'), 'hydro.mb.ca');
});

it('returns the correct rootDomain from an URL object', () => {
assert.equal(UrlUtils.getRootDomain(new URL('https://www.example.com/index.html')), 'example.com');
assert.equal(UrlUtils.getRootDomain(new URL('https://example.com')), 'example.com');
assert.equal(UrlUtils.getRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk');
assert.equal(UrlUtils.getRootDomain(new URL('https://example.com.br/app/')), 'example.com.br');
assert.equal(UrlUtils.getRootDomain(new URL('https://example.tokyo.jp')), 'example.tokyo.jp');
assert.equal(UrlUtils.getRootDomain(new URL('https://sub.example.com')), 'example.com');
assert.equal(UrlUtils.getRootDomain(new URL('https://sub.example.tokyo.jp')), 'example.tokyo.jp');
assert.equal(UrlUtils.getRootDomain(new URL('http://localhost')), 'localhost');
assert.equal(UrlUtils.getRootDomain(new URL('http://localhost:8080')), 'localhost');
assert.equal(UrlUtils.getRootDomain(new URL('https://www.hydro.mb.ca')), 'hydro.mb.ca');
});
});
});
48 changes: 24 additions & 24 deletions shared/test/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ import assert from 'assert/strict';
import {Util} from '../util.js';

describe('util helpers', () => {
describe('getTld', () => {
describe('getPseudoTld', () => {
it('returns the correct tld', () => {
assert.equal(Util.getTld('example.com'), '.com');
assert.equal(Util.getTld('example.co.uk'), '.co.uk');
assert.equal(Util.getTld('example.com.br'), '.com.br');
assert.equal(Util.getTld('example.tokyo.jp'), '.jp');
assert.equal(Util.getPseudoTld('example.com'), '.com');
assert.equal(Util.getPseudoTld('example.co.uk'), '.co.uk');
assert.equal(Util.getPseudoTld('example.com.br'), '.com.br');
assert.equal(Util.getPseudoTld('example.tokyo.jp'), '.jp');
});
});

describe('getLegacyRootDomain', () => {
describe('getPseudoRootDomain', () => {
it('returns the correct rootDomain from a string', () => {
assert.equal(Util.getLegacyRootDomain('https://www.example.com/index.html'), 'example.com');
assert.equal(Util.getLegacyRootDomain('https://example.com'), 'example.com');
assert.equal(Util.getLegacyRootDomain('https://www.example.co.uk'), 'example.co.uk');
assert.equal(Util.getLegacyRootDomain('https://example.com.br/app/'), 'example.com.br');
assert.equal(Util.getLegacyRootDomain('https://example.tokyo.jp'), 'tokyo.jp');
assert.equal(Util.getLegacyRootDomain('https://sub.example.com'), 'example.com');
assert.equal(Util.getLegacyRootDomain('https://sub.example.tokyo.jp'), 'tokyo.jp');
assert.equal(Util.getLegacyRootDomain('http://localhost'), 'localhost');
assert.equal(Util.getLegacyRootDomain('http://localhost:8080'), 'localhost');
assert.equal(Util.getPseudoRootDomain('https://www.example.com/index.html'), 'example.com');
assert.equal(Util.getPseudoRootDomain('https://example.com'), 'example.com');
assert.equal(Util.getPseudoRootDomain('https://www.example.co.uk'), 'example.co.uk');
assert.equal(Util.getPseudoRootDomain('https://example.com.br/app/'), 'example.com.br');
assert.equal(Util.getPseudoRootDomain('https://example.tokyo.jp'), 'tokyo.jp');
assert.equal(Util.getPseudoRootDomain('https://sub.example.com'), 'example.com');
assert.equal(Util.getPseudoRootDomain('https://sub.example.tokyo.jp'), 'tokyo.jp');
assert.equal(Util.getPseudoRootDomain('http://localhost'), 'localhost');
assert.equal(Util.getPseudoRootDomain('http://localhost:8080'), 'localhost');
});

it('returns the correct rootDomain from an URL object', () => {
assert.equal(Util.getLegacyRootDomain(new URL('https://www.example.com/index.html')), 'example.com');
assert.equal(Util.getLegacyRootDomain(new URL('https://example.com')), 'example.com');
assert.equal(Util.getLegacyRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk');
assert.equal(Util.getLegacyRootDomain(new URL('https://example.com.br/app/')), 'example.com.br');
assert.equal(Util.getLegacyRootDomain(new URL('https://example.tokyo.jp')), 'tokyo.jp');
assert.equal(Util.getLegacyRootDomain(new URL('https://sub.example.com')), 'example.com');
assert.equal(Util.getLegacyRootDomain(new URL('https://sub.example.tokyo.jp')), 'tokyo.jp');
assert.equal(Util.getLegacyRootDomain(new URL('http://localhost')), 'localhost');
assert.equal(Util.getLegacyRootDomain(new URL('http://localhost:8080')), 'localhost');
assert.equal(Util.getPseudoRootDomain(new URL('https://www.example.com/index.html')), 'example.com');
assert.equal(Util.getPseudoRootDomain(new URL('https://example.com')), 'example.com');
assert.equal(Util.getPseudoRootDomain(new URL('https://www.example.co.uk')), 'example.co.uk');
assert.equal(Util.getPseudoRootDomain(new URL('https://example.com.br/app/')), 'example.com.br');
assert.equal(Util.getPseudoRootDomain(new URL('https://example.tokyo.jp')), 'tokyo.jp');
assert.equal(Util.getPseudoRootDomain(new URL('https://sub.example.com')), 'example.com');
assert.equal(Util.getPseudoRootDomain(new URL('https://sub.example.tokyo.jp')), 'tokyo.jp');
assert.equal(Util.getPseudoRootDomain(new URL('http://localhost')), 'localhost');
assert.equal(Util.getPseudoRootDomain(new URL('http://localhost:8080')), 'localhost');
});
});

Expand Down
15 changes: 8 additions & 7 deletions shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ class Util {
static getEntityFromUrl(url, entities) {
// If it's a pre-v10 LHR, we don't have entities, so match against the root-ish domain
if (!entities) {
return Util.getLegacyRootDomain(url);
return Util.getPseudoRootDomain(url);
}

const entity = entities.find(e => e.origins.find(origin => url.startsWith(origin)));
// This fallback case would be unexpected, but leaving for safety.
return entity || Util.getLegacyRootDomain(url);
// This fallback case would be unexpected, but leaving for safety.
return entity || Util.getPseudoRootDomain(url);
}

/**
Expand Down Expand Up @@ -314,7 +314,7 @@ class Util {
* @param {string} hostname
* @return {string} tld
*/
static getLegacyTld(hostname) {
static getPseudoTld(hostname) {
const tlds = hostname.split('.').slice(-2);

if (!listOfTlds.includes(tlds[0])) {
Expand All @@ -328,13 +328,14 @@ class Util {
* Returns a primary domain for provided hostname (e.g. www.example.com -> example.com).
* As it doesn't consult the Public Suffix List, it can sometimes lose detail.
* See the `listOfTlds` comment above for more.
* This function is used only while rendering pre-10.0 LHRs.
* This function is used only while rendering pre-10.0 LHRs. See UrlUtils.getRootDomain
* for the current method that makes use of PSL.
* @param {string|URL} url hostname or URL object
* @return {string}
*/
static getLegacyRootDomain(url) {
static getPseudoRootDomain(url) {
const hostname = Util.createOrReturnURL(url).hostname;
const tld = Util.getLegacyTld(hostname);
const tld = Util.getPseudoTld(hostname);

// tld is .com or .co.uk which means we means that length is 1 to big
// .com => 2 & .co.uk => 3
Expand Down

0 comments on commit a535cc3

Please sign in to comment.