Skip to content

Commit

Permalink
The protocol slashes should be normalized also when the protocol is n…
Browse files Browse the repository at this point in the history
…ot alone in the first argument.
  • Loading branch information
Tero Keski-Valkama committed Dec 19, 2017
1 parent 8cde667 commit 0ce1239
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/url-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
if (strArray[0].match(/:\/*$/) && strArray.length > 1) {
var first = strArray.shift();
strArray[0] = first + strArray[0];
// There must be at least two in any, at most three slashes in the file protocol.
if (strArray[0].match(/file:\/\/\//)) {
strArray[0] = strArray[0].replace(/:\/*/, ':///');
} else {
strArray[0] = strArray[0].replace(/:\/*/, '://');
}
}

// There must be two or three slashes in the file protocol, two slashes in anything else.
if (strArray[0].match(/file:\/\/\//)) {
strArray[0] = strArray[0].replace(/:\/*/, ':///');
} else {
strArray[0] = strArray[0].replace(/:\/*/, '://');
}

for (var i = 0; i < strArray.length; i++) {
Expand Down
21 changes: 20 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('url join', function () {
.should.eql('http://www.google.com/foo/bar?test=123&boom=value&key=456');

urljoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4')
.should.eql('http://example.org/x?a=1&b=2&c=3&d=4');
.should.eql('http://example.org/x?a=1&b=2&c=3&d=4');
});

it('should merge slashes in paths correctly', function () {
Expand All @@ -92,4 +92,23 @@ describe('url join', function () {
urljoin('http://example.org/', ':foo:', 'bar')
.should.eql('http://example.org/:foo:/bar');
});

it('should merge slashes in protocol correctly', function () {
urljoin('http://example.org', 'a')
.should.eql('http://example.org/a');
urljoin('http:', '//example.org', 'a')
.should.eql('http://example.org/a');
urljoin('http:///example.org', 'a')
.should.eql('http://example.org/a');
urljoin('file:///example.org', 'a')
.should.eql('file:///example.org/a');
urljoin('file:example.org', 'a')
.should.eql('file://example.org/a');
urljoin('file:/', 'example.org', 'a')
.should.eql('file://example.org/a');
urljoin('file:', '/example.org', 'a')
.should.eql('file://example.org/a');
urljoin('file:', '//example.org', 'a')
.should.eql('file://example.org/a');
});
});

0 comments on commit 0ce1239

Please # to comment.