From bf0a25e68eddc942ef7cb2539e725c178d288b48 Mon Sep 17 00:00:00 2001 From: Mark Lagendijk Date: Wed, 20 Apr 2016 14:08:38 +0200 Subject: [PATCH] Fixed bug with unspecified protocol in resources on https page. (#44) * Fixed bug with unspecified protocol in resources on https page. - When a resource link does not have a protocol defined the protocol from the url of the containing page should be used, instead of just choosing 'http'. * Added unit tests --- lib/utils.js | 11 ++++++----- test/unit/utils-test.js | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index dbc27ea7..9bd102a4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -7,11 +7,12 @@ function isUrl(path) { return urlRegexp.test(path); } -function getUrl(currentUrl, path) { - var pathObj = url.parse(path); - if (isUrl(path) && !pathObj.protocol) { - pathObj.protocol = 'http'; - path = url.format(pathObj); +function getUrl (currentUrl, path) { + var pathObject = url.parse(path); + if (isUrl(path) && !pathObject.protocol) { + var urlObject = url.parse(currentUrl); + pathObject.protocol = urlObject.protocol; + path = url.format(pathObject); } return url.resolve(currentUrl, path); } diff --git a/test/unit/utils-test.js b/test/unit/utils-test.js index 21606eb0..7f3b6cb5 100644 --- a/test/unit/utils-test.js +++ b/test/unit/utils-test.js @@ -29,6 +29,10 @@ describe('Common utils', function () { utils.getUrl('http://google.com', 'http://my.site.com/').should.be.equal('http://my.site.com/'); utils.getUrl('http://google.com/qwe/qwe/qwe', '//my.site.com').should.be.equal('http://my.site.com/'); }); + it('should use the protocol from the url, if the path is a protocol-less url', function (){ + utils.getUrl('http://my.site.com', '//cdn.com/library.js').should.be.equal('http://cdn.com/library.js'); + utils.getUrl('https://my.site.com', '//cdn.com/library.js').should.be.equal('https://cdn.com/library.js'); + }); }); describe('#getUnixPath(path)', function () {