From 4ced30a1c5ad278eae70bb06dee8d7886e68052b Mon Sep 17 00:00:00 2001 From: Rodney Rehm Date: Sun, 20 Oct 2019 16:35:25 +0200 Subject: [PATCH] fix(build): handle relative paths with missing authority closes #387 --- src/URI.js | 4 +++- test/test.js | 10 ++++++++++ test/urls.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/URI.js b/src/URI.js index 8b0c24d6..bd23fc66 100644 --- a/src/URI.js +++ b/src/URI.js @@ -669,6 +669,7 @@ URI.build = function(parts) { var t = ''; + var requireAbsolutePath = false if (parts.protocol) { t += parts.protocol + ':'; @@ -676,12 +677,13 @@ if (!parts.urn && (t || parts.hostname)) { t += '//'; + requireAbsolutePath = true } t += (URI.buildAuthority(parts) || ''); if (typeof parts.path === 'string') { - if (parts.path.charAt(0) !== '/' && typeof parts.hostname === 'string') { + if (parts.path.charAt(0) !== '/' && requireAbsolutePath) { t += '/'; } diff --git a/test/test.js b/test/test.js index 716ec761..672b3f93 100644 --- a/test/test.js +++ b/test/test.js @@ -234,6 +234,16 @@ })(t); } + module('serializing'); + test('scheme and relative path', function() { + var u = new URI('') + .protocol('food') + .path('test/file.csv') + .toString() + + equal(u.toString(), 'food:///test/file.csv', 'relative-path with scheme but no authority'); + }); + module('mutating basics'); test('protocol', function() { var u = new URI('http://example.org/foo.html'); diff --git a/test/urls.js b/test/urls.js index 06e4ae54..61d36771 100644 --- a/test/urls.js +++ b/test/urls.js @@ -629,6 +629,54 @@ var urls = [{ idn: false, punycode: false } + }, { + name: 'missing authority', + url: 'food:///test/file.csv', + parts: { + protocol: 'food', + username: null, + password: null, + hostname: null, + port: null, + path: '/test/file.csv', + query: null, + fragment: null + }, + accessors: { + protocol: 'food', + username: '', + password: '', + port: '', + path: '/test/file.csv', + query: '', + fragment: '', + resource: '/test/file.csv', + authority: '', + origin: '', + userinfo: '', + subdomain: '', + domain: '', + tld: '', + directory: '/test', + filename: 'file.csv', + suffix: 'csv', + hash: '', + search: '', + host: '', + hostname: '' + }, + is: { + urn: false, + url: true, + relative: true, + name: false, + sld: false, + ip: false, + ip4: false, + ip6: false, + idn: false, + punycode: false + } }, { name: 'IPv4', url: 'http://user:pass@123.123.123.123:123/some/directory/file.html?query=string#fragment',