diff --git a/lib/utils.js b/lib/utils.js index 694cb601..486d2172 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -9,10 +9,11 @@ function isUrl (path) { } function getUrl (currentUrl, path) { - var pathObj = url.parse(path); - if (isUrl(path) && !pathObj.protocol) { - pathObj.protocol = 'http'; - path = url.format(pathObj); + 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 a83ba78b..aee37626 100644 --- a/test/unit/utils-test.js +++ b/test/unit/utils-test.js @@ -30,6 +30,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 () {