Skip to content

Commit

Permalink
Fix codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed May 19, 2018
1 parent 6c8c56d commit 61f7038
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lighthouse-core/lib/url-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,24 @@ class URLShim extends URL {
* @param {string} urlB
*/
static rootDomainsMatch(urlA, urlB) {
const urlAInfo = new URL(urlA);
const urlBInfo = new URL(urlB);
let urlAInfo;
let urlBInfo;
try {
urlAInfo = new URL(urlA);
urlBInfo = new URL(urlB);
} catch (err) {
return false;
}

if (!urlAInfo.hostname || !urlBInfo.hostname) {
return false;
}

const urlARootDomain = urlAInfo.hostname.split('.').slice(-2).join('.');
const urlBRootDomain = urlBInfo.hostname.split('.').slice(-2).join('.');
const isTldA = isTldPlusDomain(urlAInfo.hostname);
const isTldB = isTldPlusDomain(urlBInfo.hostname);

const urlARootDomain = urlAInfo.hostname.split('.').slice(isTldA ? -3 : -2).join('.');
const urlBRootDomain = urlBInfo.hostname.split('.').slice(isTldB ? -3 : -2).join('.');

return urlARootDomain === urlBRootDomain;
}
Expand Down
31 changes: 31 additions & 0 deletions lighthouse-core/test/lib/url-shim-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,37 @@ describe('URL Shim', () => {
assert.equal(URL.getOrigin(urlD), null);
});

describe('rootDomainsMatch', () => {
it('matches a subdomain and a root domain', () => {

const urlA = 'http://example.com/js/test.js';
const urlB = 'http://example.com/';
const urlC = 'http://sub.example.com/js/test.js';
const urlD = 'http://sub.otherdomain.com/js/test.js';

assert.ok(URL.rootDomainsMatch(urlA, urlB));
assert.ok(URL.rootDomainsMatch(urlA, urlC));
assert.ok(!URL.rootDomainsMatch(urlA, urlD));
assert.ok(!URL.rootDomainsMatch(urlB, urlD));
});

it(`doesn't break on urls without a valid host`, () => {
const urlA = 'http://example.com/js/test.js';
const urlB = 'data:image/jpeg;base64,foobar';
const urlC = 'anonymous:90';
const urlD = '!!garbage';
const urlE = 'file:///opt/lighthouse/index.js';

assert.ok(!URL.rootDomainsMatch(urlA, urlB));
assert.ok(!URL.rootDomainsMatch(urlA, urlC));
assert.ok(!URL.rootDomainsMatch(urlA, urlD));
assert.ok(!URL.rootDomainsMatch(urlA, urlE));
assert.ok(!URL.rootDomainsMatch(urlB, urlC));
assert.ok(!URL.rootDomainsMatch(urlB, urlD));
assert.ok(!URL.rootDomainsMatch(urlB, urlE));
});
});

describe('getURLDisplayName', () => {
it('respects numPathParts option', () => {
const url = 'http://example.com/a/deep/nested/file.css';
Expand Down

0 comments on commit 61f7038

Please sign in to comment.