Skip to content

Commit

Permalink
[fix] Fixes relative path resolving #199 #200 (#201)
Browse files Browse the repository at this point in the history
* [fix] Fixes relative path resolving #199 #200
* [test] Additional extractProtocol tests
  • Loading branch information
3rd-Eden authored Feb 18, 2021
1 parent 3ac7774 commit 750d8e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ function extractProtocol(address) {

var match = protocolre.exec(address)
, protocol = match[1] ? match[1].toLowerCase() : ''
, slashes = !!(match[2] && match[2].length >= 2);
, slashes = !!(match[2] && match[2].length >= 2)
, rest = match[2] && match[2].length === 1 ? '/' + match[3] : match[3];

return {
protocol: protocol,
slashes: slashes,
rest: match[3]
rest: rest
};
}

Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ describe('url-parse', function () {
});
});

it('correctly resolves paths', function () {
assume(parse.extractProtocol('/foo')).eql({
slashes: false,
protocol: '',
rest: '/foo'
});

assume(parse.extractProtocol('//foo/bar')).eql({
slashes: true,
protocol: '',
rest: 'foo/bar'
});
});

it('does not truncate the input string', function () {
var input = 'foo\nbar\rbaz\u2028qux\u2029';

Expand Down Expand Up @@ -209,6 +223,20 @@ describe('url-parse', function () {
assume(parsed.href).equals('http://example.com/');
});

it('correctly parses pathnames for relative paths', function () {
var url = '/dataApi/PROD/ws'
, parsed = parse(url, 'http://localhost:3000/PROD/trends');

assume(parsed.pathname).equals('/dataApi/PROD/ws');

url = '/sections/?project=default'
parsed = parse(url, 'http://example.com/foo/bar');

assume(parsed.pathname).equals('/sections/');
assume(parsed.hostname).equals('example.com');
assume(parsed.href).equals('http://example.com/sections/?project=default');
});

it('does not care about spaces', function () {
var url = 'http://x.com/path?that\'s#all, folks'
, parsed = parse(url);
Expand Down

0 comments on commit 750d8e8

Please sign in to comment.