Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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