From 71f61538ea16ef56b48d7359ef442ddc512abd43 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Sat, 19 May 2018 08:49:09 +0200 Subject: [PATCH] Fix codecov --- lighthouse-core/lib/url-shim.js | 17 ++++++++++--- lighthouse-core/test/lib/url-shim-test.js | 31 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lighthouse-core/lib/url-shim.js b/lighthouse-core/lib/url-shim.js index d1bbb270a172..b3f625aa8504 100644 --- a/lighthouse-core/lib/url-shim.js +++ b/lighthouse-core/lib/url-shim.js @@ -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; } diff --git a/lighthouse-core/test/lib/url-shim-test.js b/lighthouse-core/test/lib/url-shim-test.js index 8eba0d943f0e..3e7d5ebe6adc 100644 --- a/lighthouse-core/test/lib/url-shim-test.js +++ b/lighthouse-core/test/lib/url-shim-test.js @@ -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';