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

Commit

Permalink
fix(url-parser): support passing in auth to parsing options
Browse files Browse the repository at this point in the history
NODE-1767
  • Loading branch information
mbroadst committed Nov 16, 2018
1 parent 68f4fd3 commit 29455ca
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/uri_parser.js
Expand Up @@ -451,6 +451,13 @@ function parseConnectionString(uri, options, callback) {

parsedOptions = Object.assign({}, parsedOptions, options);
const auth = { username: null, password: null, db: db && db !== '' ? qs.unescape(db) : null };
if (parsedOptions.auth) {
// maintain support for legacy options passed into `MongoClient`
if (parsedOptions.auth.username) auth.username = parsedOptions.auth.username;
if (parsedOptions.auth.user) auth.username = parsedOptions.auth.user;
if (parsedOptions.auth.password) auth.password = parsedOptions.auth.password;
}

if (cap[4].split('?')[0].indexOf('@') !== -1) {
return callback(new MongoParseError('Unescaped slash in userinfo section'));
}
Expand Down
32 changes: 32 additions & 0 deletions test/tests/unit/connection_string_tests.js
Expand Up @@ -29,6 +29,38 @@ const skipTests = [
];

describe('Connection String', function() {
it('should support auth passed in through options', function(done) {
const optionsWithUser = {
authMechanism: 'SCRAM-SHA-1',
auth: { user: 'testing', password: 'llamas' }
};

const optionsWithUsername = {
authMechanism: 'SCRAM-SHA-1',
auth: { username: 'testing', password: 'llamas' }
};

parseConnectionString('mongodb://localhost', optionsWithUser, (err, result) => {
expect(err).to.not.exist;
expect(result.auth).to.containSubset({
db: 'admin',
username: 'testing',
password: 'llamas'
});

parseConnectionString('mongodb://localhost', optionsWithUsername, (err, result) => {
expect(err).to.not.exist;
expect(result.auth).to.containSubset({
db: 'admin',
username: 'testing',
password: 'llamas'
});

done();
});
});
});

it('should provide a default port if one is not provided', function(done) {
parseConnectionString('mongodb://hostname', function(err, result) {
expect(err).to.not.exist;
Expand Down

0 comments on commit 29455ca

Please sign in to comment.