From dc6cbf4f6df0858fb38f28ebdacae733d80c5ab4 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Fri, 11 Feb 2022 10:56:05 -0500 Subject: [PATCH 1/3] Add parsing validation --- src/connection_string.ts | 4 ++++ test/unit/connection_string.test.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/connection_string.ts b/src/connection_string.ts index 8aa44bc195..61776e1206 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -468,6 +468,10 @@ export function parseOptions( } } + if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) { + throw new MongoParseError('directConnection option requires exactly one host'); + } + if ( !mongoOptions.proxyHost && (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword) diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index 8328910d8f..b4d45af7c4 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -157,6 +157,19 @@ describe('Connection String', function () { expect(options.replicaSet).to.equal('123abc'); }); + context('when directionConnection is set', () => { + it('sets directConnection successfully when there is one host', () => { + const options = parseOptions('mongodb://localhost:27027/?directConnection=true'); + expect(options.directConnection).to.be.true; + }); + + it('throws when directConnection is true and there is more than one host', () => { + expect(() => + parseOptions('mongodb://localhost:27027,localhost:27018/?directConnection=true') + ).to.throw(MongoParseError, 'directConnection option requires exactly one host'); + }); + }); + context('when both tls and ssl options are provided', function () { context('when the options are provided in the URI', function () { context('when the options are equal', function () { From b73401b26e2aee86702aa0cff816e01982dcb28a Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Fri, 11 Feb 2022 12:01:29 -0500 Subject: [PATCH 2/3] spec sync and unskip now passing spec tests --- test/spec/uri-options/connection-options.json | 24 +++++++++++-------- test/spec/uri-options/connection-options.yml | 22 +++++++++-------- test/unit/assorted/uri_options.spec.test.ts | 4 ---- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/test/spec/uri-options/connection-options.json b/test/spec/uri-options/connection-options.json index 2b3ef2220f..8bb05cc721 100644 --- a/test/spec/uri-options/connection-options.json +++ b/test/spec/uri-options/connection-options.json @@ -135,7 +135,8 @@ "valid": false, "warning": false, "hosts": null, - "auth": null + "auth": null, + "options": {} }, { "description": "directConnection=false", @@ -179,6 +180,18 @@ "loadBalanced": true } }, + { + "description": "loadBalanced=true with directConnection=false", + "uri": "mongodb://example.com/?loadBalanced=true&directConnection=false", + "valid": true, + "warning": false, + "hosts": null, + "auth": null, + "options": { + "loadBalanced": true, + "directConnection": false + } + }, { "description": "loadBalanced=false", "uri": "mongodb://example.com/?loadBalanced=false", @@ -217,15 +230,6 @@ "auth": null, "options": {} }, - { - "description": "loadBalanced=true with directConnection=false causes an error", - "uri": "mongodb://example.com/?loadBalanced=true&directConnection=false", - "valid": false, - "warning": false, - "hosts": null, - "auth": null, - "options": {} - }, { "description": "loadBalanced=true with replicaSet causes an error", "uri": "mongodb://example.com/?loadBalanced=true&replicaSet=replset", diff --git a/test/spec/uri-options/connection-options.yml b/test/spec/uri-options/connection-options.yml index 303473fe13..83f63daa1a 100644 --- a/test/spec/uri-options/connection-options.yml +++ b/test/spec/uri-options/connection-options.yml @@ -64,7 +64,7 @@ tests: hosts: ~ auth: ~ options: {} - - + - description: "Invalid retryWrites causes a warning" uri: "mongodb://example.com/?retryWrites=invalid" valid: true @@ -104,7 +104,6 @@ tests: hosts: ~ auth: ~ options: {} - - description: directConnection=true uri: "mongodb://example.com/?directConnection=true" @@ -121,6 +120,7 @@ tests: warning: false hosts: ~ auth: ~ + options: {} - description: directConnection=false uri: "mongodb://example.com/?directConnection=false" @@ -156,6 +156,16 @@ tests: auth: ~ options: loadBalanced: true + - + description: loadBalanced=true with directConnection=false + uri: "mongodb://example.com/?loadBalanced=true&directConnection=false" + valid: true + warning: false + hosts: ~ + auth: ~ + options: + loadBalanced: true + directConnection: false - description: loadBalanced=false uri: "mongodb://example.com/?loadBalanced=false" @@ -189,14 +199,6 @@ tests: hosts: ~ auth: ~ options: {} - - - description: loadBalanced=true with directConnection=false causes an error - uri: "mongodb://example.com/?loadBalanced=true&directConnection=false" - valid: false - warning: false - hosts: ~ - auth: ~ - options: {} - description: loadBalanced=true with replicaSet causes an error uri: "mongodb://example.com/?loadBalanced=true&replicaSet=replset" diff --git a/test/unit/assorted/uri_options.spec.test.ts b/test/unit/assorted/uri_options.spec.test.ts index 8d965a54c0..0e1c917186 100644 --- a/test/unit/assorted/uri_options.spec.test.ts +++ b/test/unit/assorted/uri_options.spec.test.ts @@ -7,10 +7,6 @@ describe('URI option spec tests', function () { const suites = loadSpecTests('uri-options'); const skipTests = [ - // TODO(NODE-3917): Fix directConnection and loadBalanced option validation - 'directConnection=true with multiple seeds', - 'loadBalanced=true with directConnection=false causes an error', - // Skipped because this does not apply to Node 'Valid options specific to single-threaded drivers are parsed correctly', From 6c28494dd4ee3aaeed2e6e92eaab49001772694b Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 14 Feb 2022 09:47:42 -0500 Subject: [PATCH 3/3] Update linked todo on load balancer test --- test/unit/assorted/uri_options.spec.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/assorted/uri_options.spec.test.ts b/test/unit/assorted/uri_options.spec.test.ts index 0e1c917186..a5e6a19594 100644 --- a/test/unit/assorted/uri_options.spec.test.ts +++ b/test/unit/assorted/uri_options.spec.test.ts @@ -32,7 +32,7 @@ describe('URI option spec tests', function () { 'Too high zlibCompressionLevel causes a warning', 'Too low zlibCompressionLevel causes a warning', - // TODO(NODE-3917): Fix directConnection and loadBalanced option validation + // TODO(NODE-3989): Fix legacy boolean parsing 'Invalid loadBalanced value' ];