Skip to content

Commit eddca5e

Browse files
author
Jessica Lord
authored
fix(url parser): preserve auth creds when composing conn string (#1640)
Fixes NODE-1286
1 parent d9fb750 commit eddca5e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/url_parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ module.exports = function(url, options, callback) {
4848
}
4949
}
5050

51+
let base = result.auth ? `mongodb://${result.auth}@` : `mongodb://`;
5152
let connectionStrings = addresses.map(function(address, i) {
52-
if (i === 0) return `mongodb://${address.name}:${address.port}`;
53+
if (i === 0) return `${base}${address.name}:${address.port}`;
5354
else return `${address.name}:${address.port}`;
5455
});
5556

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,23 @@ describe('DNS and TXT record tests', function() {
4242
}
4343
});
4444
});
45+
46+
it('preserves auth credentials in the connection string', {
47+
metadata: {
48+
requires: { topology: ['single'] }
49+
},
50+
test: function(done) {
51+
let user = 'auser';
52+
let password = 'apass';
53+
let uri = `mongodb+srv://${user}:${password}@test18.test.build.10gen.cc/?replicaSet=repl0`;
54+
parse(uri, function(err, object) {
55+
expect(err).to.not.exist;
56+
expect(object.auth.user).to.not.be.undefined;
57+
expect(object.auth.user).to.equal(user);
58+
expect(object.auth.password).to.not.be.undefined;
59+
expect(object.auth.password).to.equal(password);
60+
done();
61+
});
62+
}
63+
});
4564
});

0 commit comments

Comments
 (0)