Skip to content

Commit

Permalink
test(url paser, connection string, mongo client): use new url parser …
Browse files Browse the repository at this point in the history
…api with callback
  • Loading branch information
Jessica Lord committed Nov 28, 2017
1 parent 2d357bc commit 2b1b163
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 322 deletions.
55 changes: 29 additions & 26 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,24 +482,36 @@ var connect = function(self, url, options, callback) {
// Get a logger for MongoClient
var logger = Logger('MongoClient', options);

// Parse the string
var object = parse(url, options);
var _finalOptions = createUnifiedOptions({}, object);
_finalOptions = mergeOptions(_finalOptions, object, false);
_finalOptions = createUnifiedOptions(_finalOptions, options);

// Check if we have connection and socket timeout set
if(_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
if(_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;

if (_finalOptions.db_options && _finalOptions.db_options.auth) {
delete _finalOptions.db_options.auth;
}
parse(url, options, function(err, object) {
if (err) return callback(err);

// Failure modes
if(object.servers.length == 0) {
throw new Error("connection string must contain at least one seed host");
}
// Parse the string
var _finalOptions = createUnifiedOptions({}, object);
_finalOptions = mergeOptions(_finalOptions, object, false);
_finalOptions = createUnifiedOptions(_finalOptions, options);

// Check if we have connection and socket timeout set
if(_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
if(_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 30000;

if (_finalOptions.db_options && _finalOptions.db_options.auth) {
delete _finalOptions.db_options.auth;
}

// Failure modes
if(object.servers.length == 0) {
throw new Error("connection string must contain at least one seed host");
}

// Do we have a replicaset then skip discovery and go straight to connectivity
if(_finalOptions.replicaSet || _finalOptions.rs_name) {
return createReplicaset(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
} else if(object.servers.length > 1) {
return createMongos(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
} else {
return createServer(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
}
});

function connectCallback(err, db) {
if(err && err.message == 'no mongos proxies found in seed list') {
Expand All @@ -514,15 +526,6 @@ var connect = function(self, url, options, callback) {
// Return the error and db instance
callback(err, db);
}

// Do we have a replicaset then skip discovery and go straight to connectivity
if(_finalOptions.replicaSet || _finalOptions.rs_name) {
return createReplicaset(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
} else if(object.servers.length > 1) {
return createMongos(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
} else {
return createServer(self, _finalOptions, connectHandler(_finalOptions, connectCallback));
}
}

module.exports = MongoClient
32 changes: 5 additions & 27 deletions test/functional/connection_string_spec_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,13 @@ exports['Should run all connection string tests'] = {
for(var j = 0; j < testFile.tests.length; j++) {
var test = testFile.tests[j];
console.log(f(' %s', test.description))
// console.dir(test)

// Unpack the test
var auth = test.auth;
var description = test.description;
var hosts = test.hosts;
var options = test.options;
var uri = test.uri;
var valid = test.valid;
var warning = test.warning;

// Test state
var success = true;

// Parse the test
try {
var result = parser(test.uri);
if(valid == false) success = false;
} catch(err) {
// console.log(err.stack)
if(valid == true) success = false;
}

// If we were unsuccessful
if(!success) {
throw test;
}
};
};
parser(test.uri, {}, function(err) {
if (err && test.valid !== false) throw test;
});
}
}

assert.done();
}
Expand Down
9 changes: 3 additions & 6 deletions test/functional/mongo_client_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,10 @@ exports['Should fail due to wrong uri user:password@localhost'] = {
test: function(configuration, test) {
var MongoClient = configuration.require.MongoClient;

try {
MongoClient.connect('user:password@localhost:27017/test', function(err, db) {
db.close();
});
} catch(err) {
MongoClient.connect('user:password@localhost:27017/test', function (err) {
test.equal(err.message, 'invalid schema, expected mongodb or mongodb+srv');
test.done();
}
});
}
}

Expand Down
Loading

0 comments on commit 2b1b163

Please sign in to comment.