From fe7d09bb08fa1c0e414956b7fc760c80459b0a43 Mon Sep 17 00:00:00 2001 From: Lochlan Bunn Date: Thu, 8 Dec 2016 15:23:45 +1000 Subject: [PATCH] Fixing combineURLs to support an empty relativeURL (#581) * 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. * Fixing combineURLs, allowing single slash relatives --- lib/helpers/combineURLs.js | 4 +++- test/specs/helpers/combineURLs.spec.js | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/helpers/combineURLs.js b/lib/helpers/combineURLs.js index 7145d10490..f1b58a5864 100644 --- a/lib/helpers/combineURLs.js +++ b/lib/helpers/combineURLs.js @@ -8,5 +8,7 @@ * @returns {string} The combined URL */ module.exports = function combineURLs(baseURL, relativeURL) { - return baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, ''); + return relativeURL + ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '') + : baseURL; }; diff --git a/test/specs/helpers/combineURLs.spec.js b/test/specs/helpers/combineURLs.spec.js index e09b730bbd..2a427f5acd 100644 --- a/test/specs/helpers/combineURLs.spec.js +++ b/test/specs/helpers/combineURLs.spec.js @@ -12,4 +12,12 @@ 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'); + }); + + it('should allow a single slash for relative url', function () { + expect(combineURLs('https://api.github.com/users', '/')).toBe('https://api.github.com/users/'); + }); });