From 2bd48d4f95a7e04110badf3fcabd8595c1c5f7fe Mon Sep 17 00:00:00 2001 From: Lochlan Bunn Date: Wed, 7 Dec 2016 11:19:04 +1000 Subject: [PATCH 1/2] Fixing combineURLs to support an empty relativeURL When combining the base and relative URLs, we should forego force appending a slash to the base when the relative URL is empty. This leads to a semantic url. --- lib/helpers/combineURLs.js | 5 ++++- test/specs/helpers/combineURLs.spec.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/helpers/combineURLs.js b/lib/helpers/combineURLs.js index 7145d10490..bfd00e5284 100644 --- a/lib/helpers/combineURLs.js +++ b/lib/helpers/combineURLs.js @@ -8,5 +8,8 @@ * @returns {string} The combined URL */ module.exports = function combineURLs(baseURL, relativeURL) { - return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, ''); + var end = relativeURL.replace(/^\/+/, ''); + return end + ? baseURL.replace(/\/+$/, '') + '/' + end + : baseURL; }; diff --git a/test/specs/helpers/combineURLs.spec.js b/test/specs/helpers/combineURLs.spec.js index e09b730bbd..4db0611ccb 100644 --- a/test/specs/helpers/combineURLs.spec.js +++ b/test/specs/helpers/combineURLs.spec.js @@ -12,4 +12,8 @@ describe('helpers::combineURLs', function () { it('should insert missing slash', function () { expect(combineURLs('https://api.github.com', 'users')).toBe('https://api.github.com/users'); }); + + it('should not insert slash when relative url missing/empty', function () { + expect(combineURLs('https://api.github.com/users', '')).toBe('https://api.github.com/users'); + }); }); From 75965ff0652c978de55de3b239c9e8c6615b9f9c Mon Sep 17 00:00:00 2001 From: Lochlan Bunn Date: Thu, 8 Dec 2016 14:18:07 +1000 Subject: [PATCH 2/2] Fixing combineURLs, allowing single slash relatives --- lib/helpers/combineURLs.js | 5 ++--- test/specs/helpers/combineURLs.spec.js | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/helpers/combineURLs.js b/lib/helpers/combineURLs.js index bfd00e5284..f1b58a5864 100644 --- a/lib/helpers/combineURLs.js +++ b/lib/helpers/combineURLs.js @@ -8,8 +8,7 @@ * @returns {string} The combined URL */ module.exports = function combineURLs(baseURL, relativeURL) { - var end = relativeURL.replace(/^\/+/, ''); - return end - ? baseURL.replace(/\/+$/, '') + '/' + end + return relativeURL + ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '') : baseURL; }; diff --git a/test/specs/helpers/combineURLs.spec.js b/test/specs/helpers/combineURLs.spec.js index 4db0611ccb..2a427f5acd 100644 --- a/test/specs/helpers/combineURLs.spec.js +++ b/test/specs/helpers/combineURLs.spec.js @@ -16,4 +16,8 @@ describe('helpers::combineURLs', function () { it('should not insert slash when relative url missing/empty', function () { expect(combineURLs('https://api.github.com/users', '')).toBe('https://api.github.com/users'); }); + + it('should allow a single slash for relative url', function () { + expect(combineURLs('https://api.github.com/users', '/')).toBe('https://api.github.com/users/'); + }); });