Skip to content

Commit b192493

Browse files
fix(NODE-3917): Throw an error when directConnection is set with multiple hosts (#3143)
1 parent 3e7b894 commit b192493

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

src/connection_string.ts

+4
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ export function parseOptions(
468468
}
469469
}
470470

471+
if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {
472+
throw new MongoParseError('directConnection option requires exactly one host');
473+
}
474+
471475
if (
472476
!mongoOptions.proxyHost &&
473477
(mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)

test/spec/uri-options/connection-options.json

+14-10
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@
135135
"valid": false,
136136
"warning": false,
137137
"hosts": null,
138-
"auth": null
138+
"auth": null,
139+
"options": {}
139140
},
140141
{
141142
"description": "directConnection=false",
@@ -179,6 +180,18 @@
179180
"loadBalanced": true
180181
}
181182
},
183+
{
184+
"description": "loadBalanced=true with directConnection=false",
185+
"uri": "mongodb://example.com/?loadBalanced=true&directConnection=false",
186+
"valid": true,
187+
"warning": false,
188+
"hosts": null,
189+
"auth": null,
190+
"options": {
191+
"loadBalanced": true,
192+
"directConnection": false
193+
}
194+
},
182195
{
183196
"description": "loadBalanced=false",
184197
"uri": "mongodb://example.com/?loadBalanced=false",
@@ -217,15 +230,6 @@
217230
"auth": null,
218231
"options": {}
219232
},
220-
{
221-
"description": "loadBalanced=true with directConnection=false causes an error",
222-
"uri": "mongodb://example.com/?loadBalanced=true&directConnection=false",
223-
"valid": false,
224-
"warning": false,
225-
"hosts": null,
226-
"auth": null,
227-
"options": {}
228-
},
229233
{
230234
"description": "loadBalanced=true with replicaSet causes an error",
231235
"uri": "mongodb://example.com/?loadBalanced=true&replicaSet=replset",

test/spec/uri-options/connection-options.yml

+12-10
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ tests:
6464
hosts: ~
6565
auth: ~
6666
options: {}
67-
-
67+
-
6868
description: "Invalid retryWrites causes a warning"
6969
uri: "mongodb://example.com/?retryWrites=invalid"
7070
valid: true
@@ -104,7 +104,6 @@ tests:
104104
hosts: ~
105105
auth: ~
106106
options: {}
107-
108107
-
109108
description: directConnection=true
110109
uri: "mongodb://example.com/?directConnection=true"
@@ -121,6 +120,7 @@ tests:
121120
warning: false
122121
hosts: ~
123122
auth: ~
123+
options: {}
124124
-
125125
description: directConnection=false
126126
uri: "mongodb://example.com/?directConnection=false"
@@ -156,6 +156,16 @@ tests:
156156
auth: ~
157157
options:
158158
loadBalanced: true
159+
-
160+
description: loadBalanced=true with directConnection=false
161+
uri: "mongodb://example.com/?loadBalanced=true&directConnection=false"
162+
valid: true
163+
warning: false
164+
hosts: ~
165+
auth: ~
166+
options:
167+
loadBalanced: true
168+
directConnection: false
159169
-
160170
description: loadBalanced=false
161171
uri: "mongodb://example.com/?loadBalanced=false"
@@ -189,14 +199,6 @@ tests:
189199
hosts: ~
190200
auth: ~
191201
options: {}
192-
-
193-
description: loadBalanced=true with directConnection=false causes an error
194-
uri: "mongodb://example.com/?loadBalanced=true&directConnection=false"
195-
valid: false
196-
warning: false
197-
hosts: ~
198-
auth: ~
199-
options: {}
200202
-
201203
description: loadBalanced=true with replicaSet causes an error
202204
uri: "mongodb://example.com/?loadBalanced=true&replicaSet=replset"

test/unit/assorted/uri_options.spec.test.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ describe('URI option spec tests', function () {
77
const suites = loadSpecTests('uri-options');
88

99
const skipTests = [
10-
// TODO(NODE-3917): Fix directConnection and loadBalanced option validation
11-
'directConnection=true with multiple seeds',
12-
'loadBalanced=true with directConnection=false causes an error',
13-
1410
// Skipped because this does not apply to Node
1511
'Valid options specific to single-threaded drivers are parsed correctly',
1612

@@ -36,7 +32,7 @@ describe('URI option spec tests', function () {
3632
'Too high zlibCompressionLevel causes a warning',
3733
'Too low zlibCompressionLevel causes a warning',
3834

39-
// TODO(NODE-3917): Fix directConnection and loadBalanced option validation
35+
// TODO(NODE-3989): Fix legacy boolean parsing
4036
'Invalid loadBalanced value'
4137
];
4238

test/unit/connection_string.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ describe('Connection String', function () {
157157
expect(options.replicaSet).to.equal('123abc');
158158
});
159159

160+
context('when directionConnection is set', () => {
161+
it('sets directConnection successfully when there is one host', () => {
162+
const options = parseOptions('mongodb://localhost:27027/?directConnection=true');
163+
expect(options.directConnection).to.be.true;
164+
});
165+
166+
it('throws when directConnection is true and there is more than one host', () => {
167+
expect(() =>
168+
parseOptions('mongodb://localhost:27027,localhost:27018/?directConnection=true')
169+
).to.throw(MongoParseError, 'directConnection option requires exactly one host');
170+
});
171+
});
172+
160173
context('when both tls and ssl options are provided', function () {
161174
context('when the options are provided in the URI', function () {
162175
context('when the options are equal', function () {

0 commit comments

Comments
 (0)