Skip to content

Commit

Permalink
fix(build): handle relative paths with missing authority
Browse files Browse the repository at this point in the history
closes #387
  • Loading branch information
rodneyrehm committed Oct 20, 2019
1 parent 7168049 commit 4ced30a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,19 +669,21 @@

URI.build = function(parts) {
var t = '';
var requireAbsolutePath = false

if (parts.protocol) {
t += parts.protocol + ':';
}

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 += '/';
}

Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
48 changes: 48 additions & 0 deletions test/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:123/some/directory/file.html?query=string#fragment',
Expand Down

0 comments on commit 4ced30a

Please sign in to comment.