diff --git a/lib/url_parser.js b/lib/url_parser.js index 686c04e84cc..f2df82fcc1b 100644 --- a/lib/url_parser.js +++ b/lib/url_parser.js @@ -48,8 +48,9 @@ module.exports = function(url, options, callback) { } } + let base = result.auth ? `mongodb://${result.auth}@` : `mongodb://`; let connectionStrings = addresses.map(function(address, i) { - if (i === 0) return `mongodb://${address.name}:${address.port}`; + if (i === 0) return `${base}${address.name}:${address.port}`; else return `${address.name}:${address.port}`; }); diff --git a/test/functional/dns_txt_records_tests.js b/test/functional/mongodb_srv_tests.js similarity index 67% rename from test/functional/dns_txt_records_tests.js rename to test/functional/mongodb_srv_tests.js index adee036e2b2..4b11c535278 100644 --- a/test/functional/dns_txt_records_tests.js +++ b/test/functional/mongodb_srv_tests.js @@ -42,4 +42,23 @@ describe('DNS and TXT record tests', function() { } }); }); + + it('preserves auth credentials in the connection string', { + metadata: { + requires: { topology: ['single'] } + }, + test: function(done) { + let user = 'auser'; + let password = 'apass'; + let uri = `mongodb+srv://${user}:${password}@test18.test.build.10gen.cc/?replicaSet=repl0`; + parse(uri, function(err, object) { + expect(err).to.not.exist; + expect(object.auth.user).to.not.be.undefined; + expect(object.auth.user).to.equal(user); + expect(object.auth.password).to.not.be.undefined; + expect(object.auth.password).to.equal(password); + done(); + }); + } + }); });