Skip to content

Commit 3d371df

Browse files
aivuss0ph1e
authored andcommitted
Fixed bug with unspecified protocol in resources on https page. (#44) (#46)
* 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
1 parent 003efcb commit 3d371df

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/utils.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ function isUrl(path) {
77
return urlRegexp.test(path);
88
}
99

10-
function getUrl(currentUrl, path) {
11-
var pathObj = url.parse(path);
12-
if (isUrl(path) && !pathObj.protocol) {
13-
pathObj.protocol = 'http';
14-
path = url.format(pathObj);
10+
function getUrl (currentUrl, path) {
11+
var pathObject = url.parse(path);
12+
if (isUrl(path) && !pathObject.protocol) {
13+
var urlObject = url.parse(currentUrl);
14+
pathObject.protocol = urlObject.protocol;
15+
path = url.format(pathObject);
1516
}
1617
return url.resolve(currentUrl, path);
1718
}

test/unit/utils-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ describe('Common utils', function () {
2929
utils.getUrl('http://google.com', 'http://my.site.com/').should.be.equal('http://my.site.com/');
3030
utils.getUrl('http://google.com/qwe/qwe/qwe', '//my.site.com').should.be.equal('http://my.site.com/');
3131
});
32+
it('should use the protocol from the url, if the path is a protocol-less url', function (){
33+
utils.getUrl('http://my.site.com', '//cdn.com/library.js').should.be.equal('http://cdn.com/library.js');
34+
utils.getUrl('https://my.site.com', '//cdn.com/library.js').should.be.equal('https://cdn.com/library.js');
35+
});
3236
});
3337

3438
describe('#getUnixPath(path)', function () {

0 commit comments

Comments
 (0)