Skip to content

Commit 431573d

Browse files
onebytegonelepture
authored andcommitted
Fix parse unit tests and simplify logic (spmjs#90)
* Fix parse unit tests and simplify the parse logic In 6fbafab the regex for parsing was changed to add support for ':port'. However, the tests were not updated. This fixes the failing tests and simplifies the logic around the parsing. * Allow for underscore in username
1 parent 8bf92f6 commit 431573d

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

lib/client.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,22 @@ Client.prototype.defaults = function(options) {
2424

2525
Client.prototype.parse = function(remote) {
2626
if (_.isString(remote)) {
27-
// username:password@host:/path/to
28-
var regex = /^([a-zA-Z0-9\-\.]+)(\:.*)?@([^:]+):([^:]+:)?(.*)?$/;
27+
// username[:password]@host[:port][:/path/to]
28+
var regex = /^([a-zA-Z0-9\-\._]+)(?:\:(.*))?@([^:]+)(?:\:([0-9]+))?(?:\:(.*))?$/;
2929
var m = remote.match(regex);
3030
if (!m) return {};
3131
var ret = {
3232
username: m[1],
3333
host: m[3],
3434
};
3535
if (m[2]) {
36-
ret.password = m[2].slice(1);
36+
ret.password = m[2];
3737
}
38-
if (m.length===6 && m[4]) {
39-
ret.port = m[4].slice(0,-1);
38+
if (m[4]) {
39+
ret.port = m[4];
4040
}
41-
if (m.length===6 && m[5]) {
41+
if (m[5]) {
4242
ret.path = m[5];
43-
} else if (m.length===5 && m[4]) {
44-
ret.path = m[4];
4543
}
4644
this.remote = ret;
4745
return ret;

test/client.test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,64 @@ describe('Client', function() {
1919
expect(ret.host).to.equal('example.com');
2020
});
2121

22+
// NOTE: At this time, username is always required
23+
// it('can parse host and port', function() {
24+
// ret = client.parse('example.com:12345');
25+
// expect(ret.host).to.equal('example.com');
26+
// expect(ret.port).to.equal('12345');
27+
// });
28+
29+
it('can parse username, host, and port', function() {
30+
ret = client.parse('[email protected]:12345');
31+
expect(ret.username).to.equal('admin');
32+
expect(ret.host).to.equal('example.com');
33+
expect(ret.port).to.equal('12345');
34+
});
35+
36+
it('can parse username, password, host, and port', function() {
37+
ret = client.parse('admin:bx%[email protected]:12345');
38+
expect(ret.username).to.equal('admin');
39+
expect(ret.password).to.equal('bx%9');
40+
expect(ret.host).to.equal('example.com');
41+
expect(ret.port).to.equal('12345');
42+
});
43+
44+
// NOTE: At this time, username is always required
45+
// it('can parse path only', function() {
46+
// ret = client.parse('/home/admin/path');
47+
// expect(ret.path).to.equal('/home/admin/path');
48+
// });
49+
50+
// NOTE: At this time, username is always required
51+
// it('can parse host and path', function() {
52+
// ret = client.parse('example.com:/home/admin/path');
53+
// expect(ret.host).to.equal('example.com');
54+
// expect(ret.path).to.equal('/home/admin/path');
55+
// });
56+
57+
// NOTE: At this time, username is always required
58+
// it('can parse host, port, and path', function() {
59+
// ret = client.parse('example.com:12345:/home/admin/path');
60+
// expect(ret.host).to.equal('example.com');
61+
// expect(ret.port).to.equal('12345');
62+
// expect(ret.path).to.equal('/home/admin/path');
63+
// });
64+
65+
it('can parse username, host, and path', function() {
66+
ret = client.parse('[email protected]:/home/admin/path');
67+
expect(ret.username).to.equal('admin');
68+
expect(ret.host).to.equal('example.com');
69+
expect(ret.path).to.equal('/home/admin/path');
70+
});
71+
72+
it('can parse username, host, port, and path', function() {
73+
ret = client.parse('[email protected]:12345:/home/admin/path');
74+
expect(ret.username).to.equal('admin');
75+
expect(ret.host).to.equal('example.com');
76+
expect(ret.port).to.equal('12345');
77+
expect(ret.path).to.equal('/home/admin/path');
78+
});
79+
2280
it('can parse username, password, host, and path', function() {
2381
ret = client.parse('admin:bx%[email protected]:/home/admin/path');
2482
expect(ret.username).to.equal('admin');
@@ -27,6 +85,41 @@ describe('Client', function() {
2785
expect(ret.path).to.equal('/home/admin/path');
2886
});
2987

88+
it('can parse username, password, host, port, and path', function() {
89+
ret = client.parse('admin:bx%[email protected]:12345:/home/admin/path');
90+
expect(ret.username).to.equal('admin');
91+
expect(ret.password).to.equal('bx%9');
92+
expect(ret.host).to.equal('example.com');
93+
expect(ret.port).to.equal('12345');
94+
expect(ret.path).to.equal('/home/admin/path');
95+
});
96+
97+
it('can handle "@" in password', function() {
98+
ret = client.parse('admin:bx@%[email protected]:12345:/home/admin/path');
99+
expect(ret.username).to.equal('admin');
100+
expect(ret.password).to.equal('bx@%9');
101+
expect(ret.host).to.equal('example.com');
102+
expect(ret.port).to.equal('12345');
103+
expect(ret.path).to.equal('/home/admin/path');
104+
});
105+
106+
it('can handle ":" in password', function() {
107+
ret = client.parse('admin:bx:%[email protected]:12345:/home/admin/path');
108+
expect(ret.username).to.equal('admin');
109+
expect(ret.password).to.equal('bx:%9');
110+
expect(ret.host).to.equal('example.com');
111+
expect(ret.port).to.equal('12345');
112+
expect(ret.path).to.equal('/home/admin/path');
113+
});
114+
115+
it('can handle "_" in username', function() {
116+
ret = client.parse('admin_2:bx%[email protected]:12345:/home/admin/path');
117+
expect(ret.username).to.equal('admin_2');
118+
expect(ret.password).to.equal('bx%9');
119+
expect(ret.host).to.equal('example.com');
120+
expect(ret.port).to.equal('12345');
121+
expect(ret.path).to.equal('/home/admin/path');
122+
});
30123

31124
});
32125

0 commit comments

Comments
 (0)