Skip to content

Commit

Permalink
[fix] Handle the username and password properties
Browse files Browse the repository at this point in the history
Update the value of the `auth` property when the `username` or
`password` property is set.

Refs: #213 (comment)
  • Loading branch information
lpinca committed Aug 23, 2021
1 parent 0be9572 commit 53d4d6d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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';
Expand Down
56 changes: 56 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 53d4d6d

Please sign in to comment.