Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed May 26, 2018
1 parent c9bb1ad commit 6b80543
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 64 deletions.
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
cs = cs.substr(m[0].length);
}

// extract hostname + port:
// if it starts now with `/`, it is the first segment, or else it is hostname:port
// extract hosts details:
// if it starts now with `/`, it is the first segment, or else it is the first host:port
if (cs[0] !== '/') {

var endOfHosts = cs.search(/\/|\?/);
Expand All @@ -60,18 +60,19 @@

for (var i = 0; i < hosts.length; i++) {
var host = hosts[i];
var h = {};
var h = {}, isIPv6 = false;

if (host[0] === '[') {
// It is an IPv6, with [::] being the shortest possible
m = host.match(/(\[([0-9a-z:%]{2,45})](?::([0-9]+[^/?]*))?)/i);
isIPv6 = true;
} else {
// It is either IPv4 or a name
m = host.match(/(([a-z0-9.-]*)(?::([0-9]+[^/?]*))?)/i);
m = host.match(/(([a-z0-9%.-]*)(?::([0-9]+[^/?]*))?)/i);
}
if (m) {
if (m[2]) {
h.name = m[2];
h.name = isIPv6 ? m[2] : decode(m[2]);
}
if (m[3]) {
var p = m[3], port = parseInt(p);
Expand Down Expand Up @@ -142,7 +143,7 @@
s += this.hosts.map(function (h) {
var a = '';
if (h.name) {
a += h.name;
a += encode(h.name);
}
if (h.port) {
a += ':' + h.port;
Expand Down
126 changes: 68 additions & 58 deletions test/mainSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('protocol', function () {
expect(parse('abc://')).toEqual({protocol: 'abc'});
});
it('must ignore incomplete format', function () {
expect(parse('abc:/')).toEqual({host: 'abc', hostname: 'abc'});
expect(parse('abc:/')).toEqual({hosts: [{name: 'abc'}]});
expect(parse('://')).toEqual({});
});
it('must decode URL-encoded characters', function () {
Expand All @@ -65,78 +65,54 @@ describe('protocol', function () {
});
});

describe('user', function () {
it('must allow only the user', function () {
expect(parse('Name@')).toEqual({user: 'Name'});
});
it('must allow user name = 0', function () {
expect(parse('0@')).toEqual({user: '0'});
});
it('must decode URL-encoded characters', function () {
expect(parse('First%20name%3F@')).toEqual({user: 'First name?'});
});
it('must support special symbols', function () {
expect(parse('A9z$-_.+!*\'()@')).toEqual({user: 'A9z$-_.+!*\'()'});
});
});

describe('password', function () {
it('must allow only the password', function () {
expect(parse(':pass@')).toEqual({password: 'pass'});
});
it('must decode URL-encoded characters', function () {
expect(parse(':pass%20123%3F@')).toEqual({password: 'pass 123?'});
});
it('must support special symbols', function () {
expect(parse(':$-_.+!*\'()@')).toEqual({password: '$-_.+!*\'()'});
});
});

describe('user + password', function () {
it('must allow skipping both', function () {
expect(parse('@')).toEqual({});
expect(parse(':@')).toEqual({});
});
});

describe('host', function () {
describe('hosts', function () {
it('must allow without port', function () {
expect(parse('server')).toEqual({
host: 'server',
hostname: 'server'
hosts: [{
name: 'server'
}]
});
});
it('must skip port endings', function () {
expect(parse('local:123/')).toEqual({host: 'local:123', hostname: 'local', port: 123});
expect(parse('local:123?')).toEqual({host: 'local:123', hostname: 'local', port: 123});
});
it('must not allow URL characters', function () {
expect(parse('server%20')).toEqual({
host: 'server',
hostname: 'server'
it('must ignore port endings', function () {
expect(parse('local:123/')).toEqual({hosts: [{name: 'local', port: 123}]});
expect(parse('local:123?')).toEqual({hosts: [{name: 'local', port: 123}]});
});
it('must allow URL characters', function () {
expect(parse('server%201,server%3F2')).toEqual({
hosts: [{
name: 'server 1'
}, {
name: 'server?2'
}]
});
});
it('must allow special characters', function () {
expect(parse('one-1.TWO-23')).toEqual({
host: 'one-1.TWO-23',
hostname: 'one-1.TWO-23'
it('must allow special symbols', function () {
expect(parse('one-1.TWO-23,three-%3F.sock')).toEqual({
hosts: [{
name: 'one-1.TWO-23'
}, {
name: 'three-?.sock'
}]
});
});
it('must recognize IPv6 addresses', function () {
expect(parse('[2001:0db8:0000:0000:0000:FF00:0042:8329]')).toEqual({
host: '[2001:0db8:0000:0000:0000:FF00:0042:8329]',
hostname: '2001:0db8:0000:0000:0000:FF00:0042:8329'
hosts: [{
name: '2001:0db8:0000:0000:0000:FF00:0042:8329'
}]
});
expect(parse('[2001:0db8]:123')).toEqual({
host: '[2001:0db8]:123',
hostname: '2001:0db8',
port: 123
hosts: [{
name: '2001:0db8',
port: 123
}]
});
});
it('must not treat IPv6 scopes as special characters', function () {
expect(parse('[2001:0db8%20]')).toEqual({
host: '[2001:0db8%20]',
hostname: '2001:0db8%20'
hosts: [{
name: '2001:0db8%20'
}]
});
});
it('must skip invalid IPv6 addresses', function () {
Expand All @@ -148,7 +124,7 @@ describe('host', function () {
expect(function () {
parse('[::]:1a');
}).toThrow('Invalid port: 1a');
expect(parse('[::]:abc')).toEqual({host: '[::]', hostname: '::'});
expect(parse('[::]:abc')).toEqual({hosts: [{name: '::'}]});
});
it('must allow valid ports', function () {
expect(parse('[::]:1')).toEqual({hosts: [{name: '::', port: 1}]});
Expand Down Expand Up @@ -177,6 +153,40 @@ describe('port', function () {
});
});

describe('user', function () {
it('must allow only the user', function () {
expect(parse('Name@')).toEqual({user: 'Name'});
});
it('must allow user name = 0', function () {
expect(parse('0@')).toEqual({user: '0'});
});
it('must decode URL-encoded characters', function () {
expect(parse('First%20name%3F@')).toEqual({user: 'First name?'});
});
it('must support special symbols', function () {
expect(parse('A9z$-_.+!*\'()@')).toEqual({user: 'A9z$-_.+!*\'()'});
});
});

describe('password', function () {
it('must allow only the password', function () {
expect(parse(':pass@')).toEqual({password: 'pass'});
});
it('must decode URL-encoded characters', function () {
expect(parse(':pass%20123%3F@')).toEqual({password: 'pass 123?'});
});
it('must support special symbols', function () {
expect(parse(':$-_.+!*\'()@')).toEqual({password: '$-_.+!*\'()'});
});
});

describe('user + password', function () {
it('must allow skipping both', function () {
expect(parse('@')).toEqual({});
expect(parse(':@')).toEqual({});
});
});

describe('segments', function () {
it('must ignore empty segments', function () {
expect(parse('/')).toEqual({});
Expand Down

0 comments on commit 6b80543

Please sign in to comment.