From 53d4d6dfd1ec24f5747271c6f2e86ffa6959b22b Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 23 Aug 2021 10:02:37 +0200 Subject: [PATCH] [fix] Handle the `username` and `password` properties Update the value of the `auth` property when the `username` or `password` property is set. Refs: https://github.com/unshiftio/url-parse/pull/213#issuecomment-897373589 --- index.js | 11 +++++++---- test/test.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3dd673e..ec936e0 100644 --- a/index.js +++ b/index.js @@ -459,15 +459,16 @@ function set(part, value, fn) { } break; + case 'username': + case 'password': + url[part] = encodeURIComponent(value); + break; + case 'auth': var splits = value.split(':'); url.username = splits[0]; url.password = splits.length === 2 ? splits[1] : ''; url[part] = value; - break; - - default: - url[part] = value; } for (var i = 0; i < rules.length; i++) { @@ -476,6 +477,8 @@ function set(part, value, fn) { if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase(); } + url.auth = url.password ? url.username +':'+ url.password : url.username; + url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host ? url.protocol +'//'+ url.host : 'null'; diff --git a/test/test.js b/test/test.js index 188e072..8130081 100644 --- a/test/test.js +++ b/test/test.js @@ -1046,6 +1046,62 @@ describe('url-parse', function () { assume(data.href).equals('mailto:alice@atlanta.com'); }); + it('updates auth when updating username', function() { + var data = parse('https://example.com'); + + assume(data.set('username', 'foo')).equals(data); + assume(data.username).equals('foo'); + assume(data.auth).equals('foo') + assume(data.href).equals('https://foo@example.com/'); + + data.set('username', ''); + + assume(data.username).equals(''); + assume(data.auth).equals('') + assume(data.href).equals('https://example.com/'); + + data.set('username', 'foo:'); + + assume(data.username).equals('foo%3A'); + assume(data.auth).equals('foo%3A') + assume(data.href).equals('https://foo%3A@example.com/'); + + data = parse('https://foo:bar@example.com') + data.set('username', 'baz'); + + assume(data.username).equals('baz'); + assume(data.auth).equals('baz:bar') + assume(data.href).equals('https://baz:bar@example.com/'); + }); + + it('updates auth when updating password', function() { + var data = parse('https://example.com'); + + assume(data.set('password', 'foo')).equals(data); + assume(data.password).equals('foo'); + assume(data.auth).equals(':foo') + assume(data.href).equals('https://:foo@example.com/'); + + data.set('password', ''); + + assume(data.password).equals(''); + assume(data.auth).equals('') + assume(data.href).equals('https://example.com/'); + + data.set('password', ':foo@'); + + assume(data.password).equals('%3Afoo%40'); + assume(data.auth).equals(':%3Afoo%40') + assume(data.href).equals('https://:%3Afoo%40@example.com/'); + + data = parse('https://foo:bar@example.com') + data.set('password', 'baz'); + + assume(data.password).equals('baz'); + assume(data.auth).equals('foo:baz') + assume(data.href).equals('https://foo:baz@example.com/'); + }); + it('updates username and password when updating auth', function() { var data = parse('https://example.com');