From 90a1f10f3a2320991229e42c57b5653c2bf9f697 Mon Sep 17 00:00:00 2001 From: "Paul.Siersma" Date: Tue, 10 Jul 2018 20:43:50 +0200 Subject: [PATCH 1/4] Allowing for unspecified port and protocol --- index.js | 34 ++++++++++++++++++++------ test/malformedConnectionString.test.js | 4 +-- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 4ea19d4..db0ed8b 100644 --- a/index.js +++ b/index.js @@ -20,11 +20,31 @@ module.exports = function (connectionString) { let port; const dataSource = result['data source']; if (dataSource) { - const regex = /.*:(.*),([0-9]+)/; - const match = regex.exec(dataSource); - if (match) { - host = match[1]; - port = match[2]; + const regexFull = /.*:(.*)/; + const regexHostPort = /(.*),([0-9]+)/; + + const matchFull = regexFull.exec(dataSource); + + if (matchFull) { + const matchHostPort1 = regexHostPort.exec(matchFull[1]); + if (matchHostPort1) { + host = matchHostPort1[1]; + port = matchHostPort1[2]; + } + else if (isNaN(matchFull[1])) { + + host = matchFull[1]; + } + } + else{ + const matchHostPort2 = regexHostPort.exec(dataSource); + if (matchHostPort2) { + host = matchHostPort2[1]; + port = matchHostPort2[2]; + } + else if (isNaN(dataSource)) { + host = datasource; + } } } @@ -42,8 +62,8 @@ module.exports = function (connectionString) { } // check if all data was found - if (!port || !host) { - throw new Error('Port or host not found'); + if (!host) { + throw new Error('Host not found'); } if (!user) { throw new Error('User not found'); diff --git a/test/malformedConnectionString.test.js b/test/malformedConnectionString.test.js index a1413e9..edd3036 100644 --- a/test/malformedConnectionString.test.js +++ b/test/malformedConnectionString.test.js @@ -2,12 +2,12 @@ const expect = require('chai').expect; const parser = require('../index'); describe('#malformedConnectionString', () => { - it('should find missing port', () => { + it('should allow missing port', () => { const connectionString = 'Data Source=tcp:database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; expect(() => { parser(connectionString); - }).to.throw(Error); + }).to.not.throw(Error); }); it('should find missing host', () => { From 2d06933f582842f6b471aa60e3e14f97118775a1 Mon Sep 17 00:00:00 2001 From: "Paul.Siersma" Date: Wed, 11 Jul 2018 08:05:08 +0200 Subject: [PATCH 2/4] Fixed bug, added tests --- index.js | 5 +-- test/knexSetup.test.js | 52 ++++++++++++++++++++++++++ test/malformedConnectionString.test.js | 15 +++++++- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index db0ed8b..8f0cbba 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,6 @@ module.exports = function (connectionString) { if (dataSource) { const regexFull = /.*:(.*)/; const regexHostPort = /(.*),([0-9]+)/; - const matchFull = regexFull.exec(dataSource); if (matchFull) { @@ -38,12 +37,12 @@ module.exports = function (connectionString) { } else{ const matchHostPort2 = regexHostPort.exec(dataSource); - if (matchHostPort2) { + if (matchHostPort2) { host = matchHostPort2[1]; port = matchHostPort2[2]; } else if (isNaN(dataSource)) { - host = datasource; + host = dataSource; } } } diff --git a/test/knexSetup.test.js b/test/knexSetup.test.js index 7fb47d9..9db4f4d 100644 --- a/test/knexSetup.test.js +++ b/test/knexSetup.test.js @@ -52,4 +52,56 @@ describe('#knexSetup', () => { const result = parser(connectionString); expect(result).to.deep.equal(expectedSetup); }); + + it('should return proper config when protocol is missing', () => { + const connectionString = 'Data Source=database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + const expectedSetup = { + 'host': 'database.com', + 'options': { + 'database': 'numbers', + 'encrypt': true, + 'port': '1433', + }, + 'password': 'fjsflregewbfldsfhsew3', + 'user': 'service', + }; + + const result = parser(connectionString); + expect(result).to.deep.equal(expectedSetup); + }); + + it('should return proper config when port is missing', () => { + const connectionString = 'Data Source=tcp:database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + const expectedSetup = { + 'host': 'database.com', + 'options': { + 'database': 'numbers', + 'encrypt': true, + 'port': undefined, + }, + 'password': 'fjsflregewbfldsfhsew3', + 'user': 'service', + }; + + const result = parser(connectionString); + expect(result).to.deep.equal(expectedSetup); + }); + + it('should return proper config when protocol and port are missing', () => { + const connectionString = 'Data Source=database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + const expectedSetup = { + 'host': 'database.com', + 'options': { + 'database': 'numbers', + 'encrypt': true, + 'port': undefined, + }, + 'password': 'fjsflregewbfldsfhsew3', + 'user': 'service', + }; + + const result = parser(connectionString); + expect(result).to.deep.equal(expectedSetup); + }); + }); diff --git a/test/malformedConnectionString.test.js b/test/malformedConnectionString.test.js index edd3036..85a6e5a 100644 --- a/test/malformedConnectionString.test.js +++ b/test/malformedConnectionString.test.js @@ -9,7 +9,20 @@ describe('#malformedConnectionString', () => { parser(connectionString); }).to.not.throw(Error); }); + it('should allow missing protocol', () => { + const connectionString = 'Data Source=database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + expect(() => { + parser(connectionString); + }).to.not.throw(Error); + }); + it('should allow missing port and protocol', () => { + const connectionString = 'Data Source=database.com;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + + expect(() => { + parser(connectionString); + }).to.not.throw(Error); + }); it('should find missing host', () => { const connectionString = 'Data Source=tcp:1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; @@ -17,7 +30,6 @@ describe('#malformedConnectionString', () => { parser(connectionString); }).to.throw(Error); }); - it('should find missing "Data Source"', () => { const connectionString = 'Data=tcp:database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; @@ -25,7 +37,6 @@ describe('#malformedConnectionString', () => { parser(connectionString); }).to.throw(Error); }); - it('should find missing "User Id"', () => { const connectionString = 'Data Source=tcp:database.com,1433;Initial Catalog=numbers;User=service@database.com;Password=fjsflregewbfldsfhsew3;'; From 02c84f184844f7cde21e0907ad7aa1fb32cacb42 Mon Sep 17 00:00:00 2001 From: "Paul.Siersma" Date: Wed, 11 Jul 2018 08:31:02 +0200 Subject: [PATCH 3/4] Only adding port to config object when present --- index.js | 11 ++++++++--- test/knexSetup.test.js | 2 -- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 8f0cbba..324b1c1 100644 --- a/index.js +++ b/index.js @@ -74,16 +74,21 @@ module.exports = function (connectionString) { throw new Error('Password not found'); } - return { + var config = { host, options: { database: result['initial catalog'], - encrypt: true, - port: port, + encrypt: true }, /* tslint:disable */ password: result['password'], /* tslint:enable */ user, }; + + if (port){ + config.options.port = port; + } + + return config; }; diff --git a/test/knexSetup.test.js b/test/knexSetup.test.js index 9db4f4d..94355b7 100644 --- a/test/knexSetup.test.js +++ b/test/knexSetup.test.js @@ -77,7 +77,6 @@ describe('#knexSetup', () => { 'options': { 'database': 'numbers', 'encrypt': true, - 'port': undefined, }, 'password': 'fjsflregewbfldsfhsew3', 'user': 'service', @@ -94,7 +93,6 @@ describe('#knexSetup', () => { 'options': { 'database': 'numbers', 'encrypt': true, - 'port': undefined, }, 'password': 'fjsflregewbfldsfhsew3', 'user': 'service', From 624d4bc2a92044a79ae97cae44de298c4f0bc22f Mon Sep 17 00:00:00 2001 From: "Paul.Siersma" Date: Wed, 11 Jul 2018 08:44:12 +0200 Subject: [PATCH 4/4] Improved code coverage --- test/malformedConnectionString.test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/malformedConnectionString.test.js b/test/malformedConnectionString.test.js index 85a6e5a..02d624a 100644 --- a/test/malformedConnectionString.test.js +++ b/test/malformedConnectionString.test.js @@ -23,13 +23,20 @@ describe('#malformedConnectionString', () => { parser(connectionString); }).to.not.throw(Error); }); - it('should find missing host', () => { + it('should find missing host with protocol', () => { const connectionString = 'Data Source=tcp:1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; expect(() => { parser(connectionString); }).to.throw(Error); }); + it('should find missing host without protocol', () => { + const connectionString = 'Data Source=1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; + + expect(() => { + parser(connectionString); + }).to.throw(Error); + }); it('should find missing "Data Source"', () => { const connectionString = 'Data=tcp:database.com,1433;Initial Catalog=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;';