Skip to content

Commit 1dac18f

Browse files
committed
feat(test): use connection strings for all calls to newClient
We have been inconsistently testing our code for some time using a deprecated form of creating a `MongoClient` by passing in a topology to the constructor. Now we take the passed in variables and construct a connection string to connect to the server, just like users of the module would.
1 parent 771e555 commit 1dac18f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

test/config.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22
const ConfigurationBase = require('mongodb-test-runner').ConfigurationBase;
33
const f = require('util').format;
4-
4+
const url = require('url');
5+
const qs = require('querystring');
56
class NativeConfiguration extends ConfigurationBase {
67
constructor(options) {
78
super(options);
@@ -59,17 +60,23 @@ class NativeConfiguration extends ConfigurationBase {
5960
if (keys.indexOf('sslOnNormalPorts') !== -1) serverOptions.ssl = true;
6061

6162
// Fall back
62-
const dbHost = (serverOptions && serverOptions.host) || 'localhost';
63+
let dbHost = (serverOptions && serverOptions.host) || 'localhost';
6364
const dbPort = (serverOptions && serverOptions.port) || this.options.port || 27017;
6465

65-
// Default topology
66-
const DbTopology = this.options.topology ? this.options.topology : this.mongo.Server;
66+
if (dbHost.indexOf('.sock') !== -1) {
67+
dbHost = qs.escape(dbHost);
68+
}
69+
70+
const connectionString = url.format({
71+
protocol: 'mongodb',
72+
slashes: true,
73+
hostname: dbHost,
74+
port: dbPort,
75+
query: dbOptions,
76+
pathname: '/'
77+
});
6778

68-
// Return a new MongoClient instance
69-
return new this.mongo.MongoClient(
70-
new DbTopology(dbHost, dbPort, serverOptions, this.mongo),
71-
dbOptions
72-
);
79+
return new this.mongo.MongoClient(connectionString, serverOptions);
7380
}
7481

7582
url(username, password) {

0 commit comments

Comments
 (0)