Skip to content

Commit

Permalink
Fixed bug with unspecified protocol in resources on https page. (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Mark Lagendijk authored and aivus committed Apr 20, 2016
1 parent 003efcb commit bf0a25e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions test/unit/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit bf0a25e

Please sign in to comment.