From 60645321a5d26b857819aa7070e86fea568c7b3c Mon Sep 17 00:00:00 2001 From: Izik Lisbon Date: Mon, 13 Nov 2017 23:48:02 -0800 Subject: [PATCH 1/2] - update the readme instructions to call `npm install` first before calling `npm test` - Add vscode configuration, for easy unit tests debugging - be case agnostic for connection string keys (Both `User ID` and `User Id` should work). - if a user has no @ assume that the entire value is the user id. (required removal of one unit test). --- README.md | 2 +- index.js | 16 ++++++------ test/knexSetup.test.js | 34 ++++++++++++++++++++++++++ test/malformedConnectionString.test.js | 8 ------ 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index aff9509..7db789c 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ A small library that parses mssql connection string and returns database configu ## Tests - + `npm install` `npm test` ## Contributing diff --git a/index.js b/index.js index dd8c7dc..4ea19d4 100644 --- a/index.js +++ b/index.js @@ -11,14 +11,14 @@ module.exports = function (connectionString) { connectionString.split(';').forEach((x) => { const arr = x.split('='); if (arr[1]) { - result[arr[0]] = arr[1]; + result[arr[0].toLowerCase()] = arr[1]; } }); // extract host and port from 'Data Source' let host; let port; - const dataSource = result['Data Source']; + const dataSource = result['data source']; if (dataSource) { const regex = /.*:(.*),([0-9]+)/; const match = regex.exec(dataSource); @@ -30,12 +30,14 @@ module.exports = function (connectionString) { // extract user from 'User Id' let user; - const userId = result['User Id']; + const userId = result['user id']; if (userId) { const regex = /(.*)@.*/; const match = regex.exec(userId); if (match) { user = match[1]; + } else { + user = userId; } } @@ -46,22 +48,22 @@ module.exports = function (connectionString) { if (!user) { throw new Error('User not found'); } - if (!result['Initial Catalog']) { + if (!result['initial catalog']) { throw new Error('Database not found'); } - if (!result['Password']) { + if (!result['password']) { throw new Error('Password not found'); } return { host, options: { - database: result['Initial Catalog'], + database: result['initial catalog'], encrypt: true, port: port, }, /* tslint:disable */ - password: result['Password'], + password: result['password'], /* tslint:enable */ user, }; diff --git a/test/knexSetup.test.js b/test/knexSetup.test.js index f2e4421..7fb47d9 100644 --- a/test/knexSetup.test.js +++ b/test/knexSetup.test.js @@ -18,4 +18,38 @@ describe('#knexSetup', () => { const result = parser(connectionString); expect(result).to.deep.equal(expectedSetup); }); + + it('should be case agnostic to the keys in the connection string', () => { + const connectionString = 'Data souRCE=tcp: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 user that has no @', () => { + const connectionString = 'Data souRCE=tcp:database.com,1433;Initial CataloG=numbers;User ID=service;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); + }); }); diff --git a/test/malformedConnectionString.test.js b/test/malformedConnectionString.test.js index 6819209..a1413e9 100644 --- a/test/malformedConnectionString.test.js +++ b/test/malformedConnectionString.test.js @@ -34,14 +34,6 @@ describe('#malformedConnectionString', () => { }).to.throw(Error); }); - it('should find missing user', () => { - const connectionString = 'Data Source=tcp:database.com,1433;Initial Catalog=numbers;User Id=database.com;Password=fjsflregewbfldsfhsew3;'; - - expect(() => { - parser(connectionString); - }).to.throw(Error); - }); - it('should find missing "Initial Catalog"', () => { const connectionString = 'Data Source=tcp:database.com,1433;Initial=numbers;User Id=service@database.com;Password=fjsflregewbfldsfhsew3;'; From b9e0f88892979a1f5bc678ce73c2ca32fa9b056a Mon Sep 17 00:00:00 2001 From: Izik Lisbon Date: Mon, 13 Nov 2017 23:48:35 -0800 Subject: [PATCH 2/2] add launch configuration. --- .vscode/launch.json | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5aee586 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "request": "launch", + "type": "node", + "name": "Run mocha", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + // Automatically stop program after launch. + "stopOnEntry": false, + // Command line arguments passed to the program. + "args": ["test"] + } + ] +} \ No newline at end of file