diff --git a/src/URI.js b/src/URI.js index 48d37bbe..fe3ed434 100644 --- a/src/URI.js +++ b/src/URI.js @@ -685,11 +685,13 @@ if (parts.username) { t += URI.encode(parts.username); + } - if (parts.password) { - t += ':' + URI.encode(parts.password); - } + if (parts.password) { + t += ':' + URI.encode(parts.password); + } + if (t) { t += '@'; } @@ -1334,12 +1336,8 @@ } if (v === undefined) { - if (!this._parts.username) { - return ''; - } - var t = URI.buildUserinfo(this._parts); - return t.substring(0, t.length -1); + return t ? t.substring(0, t.length -1) : t; } else { if (v[v.length-1] !== '@') { v += '@'; diff --git a/test/urls.js b/test/urls.js index 33353604..b38337ec 100644 --- a/test/urls.js +++ b/test/urls.js @@ -290,6 +290,55 @@ var urls = [{ idn: false, punycode: false } + }, { + name: 'empty username with non-empty password', + url: 'scheme://:password@www.example.org/', + _url: 'scheme://:password@www.example.org/', + parts: { + protocol: 'scheme', + username: null, + password: 'password', + hostname: 'www.example.org', + port: null, + path: '/', + query: null, + fragment: null + }, + accessors: { + protocol: 'scheme', + username: '', + password: 'password', + port: '', + path: '/', + query: '', + fragment: '', + resource: '/', + authority: ':password@www.example.org', + origin: 'scheme://:password@www.example.org', + userinfo: ':password', + subdomain: 'www', + domain: 'example.org', + tld: 'org', + directory: '/', + filename: '', + suffix: '', + hash: '', + search: '', + host: 'www.example.org', + hostname: 'www.example.org' + }, + is: { + urn: false, + url: true, + relative: false, + name: true, + sld: false, + ip: false, + ip4: false, + ip6: false, + idn: false, + punycode: false + } }, { name: 'malformed email in userinfo', url: 'scheme://john@doe.com:pass:word@www.example.org/',