Skip to content

Commit

Permalink
fix(NODE-3917): Throw an error when directConnection is set with mult…
Browse files Browse the repository at this point in the history
…iple hosts (#3143)
  • Loading branch information
baileympearson committed Feb 14, 2022
1 parent 3e7b894 commit b192493
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/connection_string.ts
Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions test/spec/uri-options/connection-options.json
Expand Up @@ -135,7 +135,8 @@
"valid": false,
"warning": false,
"hosts": null,
"auth": null
"auth": null,
"options": {}
},
{
"description": "directConnection=false",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
22 changes: 12 additions & 10 deletions test/spec/uri-options/connection-options.yml
Expand Up @@ -64,7 +64,7 @@ tests:
hosts: ~
auth: ~
options: {}
-
-
description: "Invalid retryWrites causes a warning"
uri: "mongodb://example.com/?retryWrites=invalid"
valid: true
Expand Down Expand Up @@ -104,7 +104,6 @@ tests:
hosts: ~
auth: ~
options: {}

-
description: directConnection=true
uri: "mongodb://example.com/?directConnection=true"
Expand All @@ -121,6 +120,7 @@ tests:
warning: false
hosts: ~
auth: ~
options: {}
-
description: directConnection=false
uri: "mongodb://example.com/?directConnection=false"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 1 addition & 5 deletions test/unit/assorted/uri_options.spec.test.ts
Expand Up @@ -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',

Expand All @@ -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'
];

Expand Down
13 changes: 13 additions & 0 deletions test/unit/connection_string.test.ts
Expand Up @@ -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 () {
Expand Down

0 comments on commit b192493

Please sign in to comment.