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

Commit 29455ca

Browse files
committed
fix(url-parser): support passing in auth to parsing options
NODE-1767
1 parent 68f4fd3 commit 29455ca

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/uri_parser.js

+7
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,13 @@ function parseConnectionString(uri, options, callback) {
451451

452452
parsedOptions = Object.assign({}, parsedOptions, options);
453453
const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null };
454+
if (parsedOptions.auth) {
455+
// maintain support for legacy options passed into `MongoClient`
456+
if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
457+
if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
458+
if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
459+
}
460+
454461
if (cap[4].split('?')[0].indexOf('@') !== -1) {
455462
return callback(new MongoParseError('Unescaped slash in userinfo section'));
456463
}

test/tests/unit/connection_string_tests.js

+32
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,38 @@ const skipTests = [
2929
];
3030

3131
describe('Connection String', function() {
32+
it('should support auth passed in through options', function(done) {
33+
const optionsWithUser = {
34+
authMechanism: 'SCRAM-SHA-1',
35+
auth: { user: 'testing', password: 'llamas' }
36+
};
37+
38+
const optionsWithUsername = {
39+
authMechanism: 'SCRAM-SHA-1',
40+
auth: { username: 'testing', password: 'llamas' }
41+
};
42+
43+
parseConnectionString('mongodb://localhost', optionsWithUser, (err, result) => {
44+
expect(err).to.not.exist;
45+
expect(result.auth).to.containSubset({
46+
db: 'admin',
47+
username: 'testing',
48+
password: 'llamas'
49+
});
50+
51+
parseConnectionString('mongodb://localhost', optionsWithUsername, (err, result) => {
52+
expect(err).to.not.exist;
53+
expect(result.auth).to.containSubset({
54+
db: 'admin',
55+
username: 'testing',
56+
password: 'llamas'
57+
});
58+
59+
done();
60+
});
61+
});
62+
});
63+
3264
it('should provide a default port if one is not provided', function(done) {
3365
parseConnectionString('mongodb://hostname', function(err, result) {
3466
expect(err).to.not.exist;

0 commit comments

Comments
 (0)