Skip to content

Commit

Permalink
feat(test): use connection strings for all calls to newClient
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbroadst committed Aug 24, 2018
1 parent 771e555 commit 1dac18f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
const ConfigurationBase = require('mongodb-test-runner').ConfigurationBase;
const f = require('util').format;

const url = require('url');
const qs = require('querystring');
class NativeConfiguration extends ConfigurationBase {
constructor(options) {
super(options);
Expand Down Expand Up @@ -59,17 +60,23 @@ class NativeConfiguration extends ConfigurationBase {
if (keys.indexOf('sslOnNormalPorts') !== -1) serverOptions.ssl = true;

// Fall back
const dbHost = (serverOptions && serverOptions.host) || 'localhost';
let dbHost = (serverOptions && serverOptions.host) || 'localhost';
const dbPort = (serverOptions && serverOptions.port) || this.options.port || 27017;

// Default topology
const DbTopology = this.options.topology ? this.options.topology : this.mongo.Server;
if (dbHost.indexOf('.sock') !== -1) {
dbHost = qs.escape(dbHost);
}

const connectionString = url.format({
protocol: 'mongodb',
slashes: true,
hostname: dbHost,
port: dbPort,
query: dbOptions,
pathname: '/'
});

// Return a new MongoClient instance
return new this.mongo.MongoClient(
new DbTopology(dbHost, dbPort, serverOptions, this.mongo),
dbOptions
);
return new this.mongo.MongoClient(connectionString, serverOptions);
}

url(username, password) {
Expand Down

0 comments on commit 1dac18f

Please sign in to comment.