From 750d8e8a9d45dbce9ff09759f0fe4564cdd47d74 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier <3rd-Eden@users.noreply.github.com> Date: Thu, 18 Feb 2021 15:46:02 +0100 Subject: [PATCH] [fix] Fixes relative path resolving #199 #200 (#201) * [fix] Fixes relative path resolving #199 #200 * [test] Additional extractProtocol tests --- index.js | 5 +++-- test/test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e54575a..72b27c0 100644 --- a/index.js +++ b/index.js @@ -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 }; } diff --git a/test/test.js b/test/test.js index 3020236..216891e 100644 --- a/test/test.js +++ b/test/test.js @@ -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'; @@ -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);