Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3917): Throw an error when directConnection is set with multiple hosts #3143

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
}
},
{
dariakp marked this conversation as resolved.
Show resolved Hide resolved
"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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
dariakp marked this conversation as resolved.
Show resolved Hide resolved
'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
Original file line number Diff line number Diff line change
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');
});
});
dariakp marked this conversation as resolved.
Show resolved Hide resolved

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