From f506d906a3d65b55060f371cab4e24199dda027a Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Tue, 5 Mar 2019 11:00:48 -0800 Subject: [PATCH 1/3] Fix: DownloadReachability regex --- src/lib/DownloadReachability.js | 2 +- .../__tests__/DownloadReachability-test.js | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/lib/DownloadReachability.js b/src/lib/DownloadReachability.js index 82fc79876..500a95348 100644 --- a/src/lib/DownloadReachability.js +++ b/src/lib/DownloadReachability.js @@ -6,7 +6,7 @@ const PROD_CUSTOM_HOST_SUFFIX = 'boxcloud.com'; const DOWNLOAD_NOTIFICATION_SHOWN_KEY = 'download_host_notification_shown'; const DOWNLOAD_HOST_FALLBACK_KEY = 'download_host_fallback'; const NUMBERED_HOST_PREFIX_REGEX = /^https:\/\/dl\d+\./; -const CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/[A-Za-z0-9]+./; +const CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/.+?\./; let IS_STORAGE_AVAILABLE; diff --git a/src/lib/__tests__/DownloadReachability-test.js b/src/lib/__tests__/DownloadReachability-test.js index 3c13df311..f2a1837fb 100644 --- a/src/lib/__tests__/DownloadReachability-test.js +++ b/src/lib/__tests__/DownloadReachability-test.js @@ -41,10 +41,13 @@ describe('lib/DownloadReachability', () => { url = 'https://dl.user.inside-box.net'; expect(DownloadReachability.isCustomDownloadHost(url)).to.be.false; + + url = 'https://dl-hnl.user.inside-box.net'; + expect(DownloadReachability.isCustomDownloadHost(url)).to.be.false; }); }); - describe('replaceDownloadHostWithDefault()', () => { + describe('setDownloadHostNotificationShown()', () => { it('should add the given host to the array of shown hosts', () => { const blockedHost = 'https://dl3.boxcloud.com'; @@ -209,4 +212,36 @@ describe('lib/DownloadReachability', () => { expect(util.openUrlInsideIframe.getCall(0).args[1]).to.equal(defaultDownloadUrl); }); }); + + describe('replaceDownloadHostWithDefault()', () => { + const tests = [ + { + title: 'numbered host', + downloadUrl: 'https://dl3.boxcloud.com', + expectedResult: 'https://dl.boxcloud.com' + }, + { + title: 'two digit numbered host', + downloadUrl: 'https://dl34.boxcloud.com', + expectedResult: 'https://dl.boxcloud.com' + }, + { title: 'google', downloadUrl: 'https://www.google.com', expectedResult: 'https://dl.google.com' }, + { title: 'aws', downloadUrl: 'https://kld3lk.boxcloud.com', expectedResult: 'https://dl.boxcloud.com' }, + { + title: 'inside-box', + downloadUrl: 'https://dl3.user.inside-box.net', + expectedResult: 'https://dl.user.inside-box.net' + }, + { title: 'dl-las', downloadUrl: 'https://dl-las.boxcloud.com', expectedResult: 'https://dl.boxcloud.com' } + ]; + + tests.forEach((testData) => { + it(`should replace host with default: ${testData.title}`, () => { + expect( + DownloadReachability.replaceDownloadHostWithDefault(testData.downloadUrl), + testData.expectedResult + ); + }); + }); + }); }); From a9e5885ba50d4cc1dfe9a7729a1b99eb52224673 Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Tue, 5 Mar 2019 11:28:39 -0800 Subject: [PATCH 2/3] Chore: address PR comments --- src/lib/DownloadReachability.js | 10 ++---- .../__tests__/DownloadReachability-test.js | 36 ++++++++----------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/lib/DownloadReachability.js b/src/lib/DownloadReachability.js index 500a95348..bb807cf07 100644 --- a/src/lib/DownloadReachability.js +++ b/src/lib/DownloadReachability.js @@ -5,7 +5,7 @@ const DEFAULT_DOWNLOAD_HOST_PREFIX = 'https://dl.'; const PROD_CUSTOM_HOST_SUFFIX = 'boxcloud.com'; const DOWNLOAD_NOTIFICATION_SHOWN_KEY = 'download_host_notification_shown'; const DOWNLOAD_HOST_FALLBACK_KEY = 'download_host_fallback'; -const NUMBERED_HOST_PREFIX_REGEX = /^https:\/\/dl\d+\./; +const VALID_CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/dl\d+|^https:\/\/dl-las\./; const CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/.+?\./; let IS_STORAGE_AVAILABLE; @@ -55,7 +55,7 @@ class DownloadReachability { // 2. or starts with a custom prefix and ends with boxcloud.com return ( !downloadUrl.startsWith(DEFAULT_DOWNLOAD_HOST_PREFIX) && - (!!downloadUrl.match(NUMBERED_HOST_PREFIX_REGEX) || downloadUrl.indexOf(PROD_CUSTOM_HOST_SUFFIX) !== -1) + (!!downloadUrl.match(VALID_CUSTOM_HOST_PREFIX_REGEX) || downloadUrl.indexOf(PROD_CUSTOM_HOST_SUFFIX) !== -1) ); } @@ -67,12 +67,6 @@ class DownloadReachability { * @return {string} - The updated download URL */ static replaceDownloadHostWithDefault(downloadUrl) { - if (downloadUrl.match(NUMBERED_HOST_PREFIX_REGEX)) { - // First check to see if we can swap a numbered dl prefix for the default - return downloadUrl.replace(NUMBERED_HOST_PREFIX_REGEX, DEFAULT_DOWNLOAD_HOST_PREFIX); - } - - // Otherwise replace the custom prefix with the default return downloadUrl.replace(CUSTOM_HOST_PREFIX_REGEX, DEFAULT_DOWNLOAD_HOST_PREFIX); } diff --git a/src/lib/__tests__/DownloadReachability-test.js b/src/lib/__tests__/DownloadReachability-test.js index f2a1837fb..5ce2ec019 100644 --- a/src/lib/__tests__/DownloadReachability-test.js +++ b/src/lib/__tests__/DownloadReachability-test.js @@ -22,28 +22,22 @@ describe('lib/DownloadReachability', () => { }); describe('isCustomDownloadHost()', () => { - it('should be true if the url does not start with the default host prefix but is a dl host', () => { - let url = 'https://dl3.boxcloud.com/foo'; - const result = DownloadReachability.isCustomDownloadHost(url); - expect(result).to.be.true; - - url = 'https://dl.boxcloud.com/foo'; - expect(DownloadReachability.isCustomDownloadHost(url)).to.be.false; - - url = 'https://www.google.com'; - expect(DownloadReachability.isCustomDownloadHost(url)).to.be.false; - - url = 'https://kld3lk.boxcloud.com'; - expect(DownloadReachability.isCustomDownloadHost(url)).to.be.true; - - url = 'https://dl3.user.inside-box.net'; - expect(DownloadReachability.isCustomDownloadHost(url)).to.be.true; - - url = 'https://dl.user.inside-box.net'; - expect(DownloadReachability.isCustomDownloadHost(url)).to.be.false; + const tests = [ + { title: 'number host prefix', url: 'https://dl3.boxcloud.com/foo', expectedValue: true }, + { title: 'default prefix', url: 'https://dl.boxcloud.com/foo', expectedValue: false }, + { title: 'google', url: 'https://www.google.com', expectedValue: false }, + { title: 'has boxcloud domain', url: 'https://kld3lk.boxcloud.com', expectedValue: true }, + { title: 'number host prefix for inside-box', url: 'https://dl3.user.inside-box.net', expectedValue: true }, + { title: 'default prefix for inside-box', url: 'https://dl.user.inside-box.net', expectedValue: false }, + { title: 'dl-hnl for inside-box', url: 'https://dl-hnl.user.inside-box.net', expectedValue: false }, + { title: 'dl-las for inside-box', url: 'https://dl-las.user.inside-box.net', expectedValue: true } + ]; - url = 'https://dl-hnl.user.inside-box.net'; - expect(DownloadReachability.isCustomDownloadHost(url)).to.be.false; + tests.forEach((testData) => { + it(`should be ${testData.expectedValue} if the url is ${testData.title}`, () => { + const result = DownloadReachability.isCustomDownloadHost(testData.url); + expect(result).to.be[testData.expectedValue]; + }); }); }); From 16e0b2bd4dba775be42309b4307633ceb3055eff Mon Sep 17 00:00:00 2001 From: Conrad Chan Date: Tue, 5 Mar 2019 12:00:32 -0800 Subject: [PATCH 3/3] Chore: address PR comments --- src/lib/DownloadReachability.js | 10 ++++------ src/lib/__tests__/DownloadReachability-test.js | 8 ++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/lib/DownloadReachability.js b/src/lib/DownloadReachability.js index bb807cf07..03b0b2592 100644 --- a/src/lib/DownloadReachability.js +++ b/src/lib/DownloadReachability.js @@ -5,7 +5,6 @@ const DEFAULT_DOWNLOAD_HOST_PREFIX = 'https://dl.'; const PROD_CUSTOM_HOST_SUFFIX = 'boxcloud.com'; const DOWNLOAD_NOTIFICATION_SHOWN_KEY = 'download_host_notification_shown'; const DOWNLOAD_HOST_FALLBACK_KEY = 'download_host_fallback'; -const VALID_CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/dl\d+|^https:\/\/dl-las\./; const CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/.+?\./; let IS_STORAGE_AVAILABLE; @@ -50,12 +49,11 @@ class DownloadReachability { * @return {boolean} - HTTP response */ static isCustomDownloadHost(downloadUrl) { - // A custom download host either - // 1. begins with a numbered dl hostname - // 2. or starts with a custom prefix and ends with boxcloud.com + // A custom download host: + // 1. does not begin with the default "dl." and + // 2. ends with boxcloud.com return ( - !downloadUrl.startsWith(DEFAULT_DOWNLOAD_HOST_PREFIX) && - (!!downloadUrl.match(VALID_CUSTOM_HOST_PREFIX_REGEX) || downloadUrl.indexOf(PROD_CUSTOM_HOST_SUFFIX) !== -1) + !downloadUrl.startsWith(DEFAULT_DOWNLOAD_HOST_PREFIX) && downloadUrl.indexOf(PROD_CUSTOM_HOST_SUFFIX) !== -1 ); } diff --git a/src/lib/__tests__/DownloadReachability-test.js b/src/lib/__tests__/DownloadReachability-test.js index 5ce2ec019..bbe1e4c39 100644 --- a/src/lib/__tests__/DownloadReachability-test.js +++ b/src/lib/__tests__/DownloadReachability-test.js @@ -27,10 +27,14 @@ describe('lib/DownloadReachability', () => { { title: 'default prefix', url: 'https://dl.boxcloud.com/foo', expectedValue: false }, { title: 'google', url: 'https://www.google.com', expectedValue: false }, { title: 'has boxcloud domain', url: 'https://kld3lk.boxcloud.com', expectedValue: true }, - { title: 'number host prefix for inside-box', url: 'https://dl3.user.inside-box.net', expectedValue: true }, + { + title: 'number host prefix for inside-box', + url: 'https://dl3.user.inside-box.net', + expectedValue: false + }, { title: 'default prefix for inside-box', url: 'https://dl.user.inside-box.net', expectedValue: false }, { title: 'dl-hnl for inside-box', url: 'https://dl-hnl.user.inside-box.net', expectedValue: false }, - { title: 'dl-las for inside-box', url: 'https://dl-las.user.inside-box.net', expectedValue: true } + { title: 'dl-las', url: 'https://dl-las.boxcloud.com', expectedValue: true } ]; tests.forEach((testData) => {