Skip to content

Commit

Permalink
Fix: DownloadReachability regex (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored Mar 5, 2019
1 parent 904d452 commit 9cb9f7c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
18 changes: 5 additions & 13 deletions src/lib/DownloadReachability.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +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 CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/[A-Za-z0-9]+./;
const CUSTOM_HOST_PREFIX_REGEX = /^https:\/\/.+?\./;

let IS_STORAGE_AVAILABLE;

Expand Down Expand Up @@ -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(NUMBERED_HOST_PREFIX_REGEX) || downloadUrl.indexOf(PROD_CUSTOM_HOST_SUFFIX) !== -1)
!downloadUrl.startsWith(DEFAULT_DOWNLOAD_HOST_PREFIX) && downloadUrl.indexOf(PROD_CUSTOM_HOST_SUFFIX) !== -1
);
}

Expand All @@ -67,12 +65,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);
}

Expand Down
73 changes: 53 additions & 20 deletions src/lib/__tests__/DownloadReachability-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,30 @@ 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: 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', url: 'https://dl-las.boxcloud.com', expectedValue: true }
];

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];
});
});
});

describe('replaceDownloadHostWithDefault()', () => {
describe('setDownloadHostNotificationShown()', () => {
it('should add the given host to the array of shown hosts', () => {
const blockedHost = 'https://dl3.boxcloud.com';

Expand Down Expand Up @@ -209,4 +210,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
);
});
});
});
});

0 comments on commit 9cb9f7c

Please sign in to comment.