Skip to content

Commit

Permalink
refactoring protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Aug 9, 2020
1 parent 2c23cdb commit 8bf8b6f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ export class ConnectionString {
validateUrl(cs); // will throw, if failed

// Extracting the protocol:
let m = cs.match(/^([a-z]+[a-z0-9+-.]*)?:\/\//i);
let m = cs.match(/^(.*)?:\/\//);
if (m) {
if (m[1]) {
this.protocol = m[1];
const p = m[1]; // protocol name
if (p) {
const m2 = p.match(/^([a-z]+[a-z0-9+-.]*)/i);
if (p && (!m2 || m2[1] !== p)) {
throw new Error(`Invalid protocol name: ${p}`);
}
this.protocol = p;
}
cs = cs.substr(m[0].length);
}
Expand Down
18 changes: 17 additions & 1 deletion test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('constructor', () => {

describe('protocol', () => {
it('must recognize standard format', () => {
expect(parse('abc://')).to.eql({protocol: 'abc'});
expect(parse('abc123://')).to.eql({protocol: 'abc123'});
});
it('must allow special symbols', () => {
expect(parse('one+two-three.four://')).to.eql({protocol: 'one+two-three.four'});
Expand All @@ -98,6 +98,22 @@ describe('protocol', () => {
expect(parse('abc:/')).to.eql({hosts: [{name: 'abc', type: 'domain'}]});
expect(parse('://')).to.eql({});
});
it('must throw on invalid symbols', () => {
expect(() => {
parse('a$b://');
}).to.throw('Invalid protocol name: a$b');
expect(() => {
parse('a%://');
}).to.throw('Invalid protocol name: a%');
});
it('must throw on leading digits', () => {
expect(() => {
parse('123://');
}).to.throw('Invalid protocol name: 123');
expect(() => {
parse('1a://');
}).to.throw('Invalid protocol name: 1a');
});
});

describe('hosts', () => {
Expand Down

0 comments on commit 8bf8b6f

Please sign in to comment.