From b192493cd790e5fd7c05ca4926535e06ceffb55e Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Mon, 14 Feb 2022 10:49:22 -0500 Subject: [PATCH] fix(NODE-3917): Throw an error when directConnection is set with multiple hosts (#3143) --- src/connection_string.ts | 4 ++++ test/spec/uri-options/connection-options.json | 24 +++++++++++-------- test/spec/uri-options/connection-options.yml | 22 +++++++++-------- test/unit/assorted/uri_options.spec.test.ts | 6 +---- test/unit/connection_string.test.ts | 13 ++++++++++ 5 files changed, 44 insertions(+), 25 deletions(-) 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/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..a5e6a19594 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', @@ -36,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' ]; 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 () {