diff --git a/lib/util.js b/lib/util.js index a11dda0d58..a2eb72d54e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -185,7 +185,7 @@ util.getLink = function(obj, linkName, altName) { if (!obj || !obj._links) { return; } - + var link = util.clone(obj._links[linkName]); // If a link has a name and we have an altName, return if they match @@ -235,10 +235,12 @@ util.removeTrailingSlash = function(path) { if (!path) { return; } - if (path.slice(-1) === '/') { - return path.slice(0, -1); + // Remove any whitespace before or after string + var trimmed = path.replace(/^\s+|\s+$/gm,''); + if (trimmed.slice(-1) === '/') { + return trimmed.slice(0, -1); } - return path; + return trimmed; }; util.isIE11OrLess = function() { diff --git a/test/spec/util.js b/test/spec/util.js index e683d834b7..9d69fed396 100644 --- a/test/spec/util.js +++ b/test/spec/util.js @@ -90,5 +90,37 @@ define(function(require) { }); }); + describe('removeTrailingSlash', function() { + it('returns a url with a trailing slash without the trailing slash', function() { + var url = 'https://domain.com/'; + expect(util.removeTrailingSlash(url)).toEqual('https://domain.com'); + }); + + it('returns a url without a trailing slash as is', function() { + var url = 'https://domain.com'; + expect(util.removeTrailingSlash(url)).toEqual('https://domain.com'); + }); + + it('returns a url with a trailing slash and appended whitespace correctly', function() { + var url = 'https://domain.com/ '; + expect(util.removeTrailingSlash(url)).toEqual('https://domain.com'); + }); + + it('returns a url with a trailing slash and prepended whitespace correctly', function() { + var url = ' https://domain.com/'; + expect(util.removeTrailingSlash(url)).toEqual('https://domain.com'); + }); + + it('returns a url without a trailing slash and appended whitespace correctly', function() { + var url = 'https://domain.com '; + expect(util.removeTrailingSlash(url)).toEqual('https://domain.com'); + }); + + it('returns a url without a trailing slash and prepended whitespace correctly', function() { + var url = ' https://domain.com'; + expect(util.removeTrailingSlash(url)).toEqual('https://domain.com'); + }); + }); + }); });