Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(uri_parser): support a default database on mongodb+srv uris
Browse files Browse the repository at this point in the history
NODE-1321
  • Loading branch information
mbroadst committed Mar 26, 2018
1 parent bd8bec4 commit be01ffe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/uri_parser.js
Expand Up @@ -71,9 +71,19 @@ function parseSrvConnectionString(uri, options, callback) {
i === 0 ? `${base}${address.name}:${address.port}` : `${address.name}:${address.port}`
);

let connectionString = connectionStrings.join(',') + '/';
let connectionString = `${connectionStrings.join(',')}/`;
let connectionStringOptions = [];

// Add the default database if needed
if (result.path) {
let defaultDb = result.path.slice(1);
if (defaultDb.indexOf('?') !== -1) {
defaultDb = defaultDb.slice(0, defaultDb.indexOf('?'));
}

connectionString += defaultDb;
}

// Default to SSL true
if (!options.ssl && (!result.search || result.query['ssl'] == null)) {
connectionStringOptions.push('ssl=true');
Expand Down
7 changes: 7 additions & 0 deletions test/tests/unit/mongodb_srv_spec_tests.js
Expand Up @@ -5,6 +5,13 @@ const parseConnectionString = require('../../../lib/uri_parser');
const expect = require('chai').expect;

describe('mongodb+srv (spec)', function() {
it('should parse a default database', function(done) {
parseConnectionString('mongodb+srv://test5.test.build.10gen.cc/somedb', (err, result) => {
expect(result.auth.db).to.eql('somedb');
done();
});
});

const specPath = path.join(__dirname, '../spec', 'initial-dns-seedlist-discovery');
const testFiles = fs
.readdirSync(specPath)
Expand Down

0 comments on commit be01ffe

Please sign in to comment.