diff --git a/lib/url-join.js b/lib/url-join.js index f81cdee..5574319 100644 --- a/lib/url-join.js +++ b/lib/url-join.js @@ -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++) { diff --git a/test/tests.js b/test/tests.js index 818ed25..d6d732a 100644 --- a/test/tests.js +++ b/test/tests.js @@ -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 () { @@ -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'); + }); });