Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 0ac4870

Browse files
author
Matt Cotter
committed
INT-1049 getId improvements
* remove username/password from URL * remove connection options
1 parent 64cc23b commit 0ac4870

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/model.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,7 @@ module.exports.getId = function(id) {
142142
return id.toLowerCase()
143143
.replace(hostname.toLowerCase(), 'localhost')
144144
.replace('127.0.0.1', 'localhost')
145-
.replace('mongodb://', '');
145+
.replace('mongodb://', '')
146+
.replace(/^[^@]+@/, '')
147+
.replace(/\/?\?(?:\w+=[^&]+&)*\w+=[^&]+$/, '');
146148
};

test/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ describe('mongodb-instance-model', function() {
2323
it('should remove mongodb://', function() {
2424
assert.equal(Instance.getId('mongodb://localhost:27017'), 'localhost:27017');
2525
});
26+
it('should remove mongodb://user:pass@', function() {
27+
assert.equal(Instance.getId('mongodb://matt:123@localhost:27017'), 'localhost:27017');
28+
});
29+
it('should remove user:pass@', function() {
30+
assert.equal(Instance.getId('matt:123@localhost:27017'), 'localhost:27017');
31+
});
32+
it('should remove a trailing option', function() {
33+
assert.equal(Instance.getId('localhost:27017/?option=thing'), 'localhost:27017');
34+
});
35+
it('should remove many trailing options', function() {
36+
assert.equal(Instance.getId('localhost:27017/?option=thing&otherThing=coolStuff&zombies=true'), 'localhost:27017');
37+
});
2638
it('should substitute localhost for 127.0.0.1', function() {
2739
assert.equal(Instance.getId('127.0.0.1:27017'), 'localhost:27017');
2840
});

0 commit comments

Comments
 (0)