Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/service-provider-core/src/uri-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ describe('uri-generator.generate-uri', () => {
const options = { _: [] };

it('returns the default uri', () => {
expect(generateUri(options)).to.equal('mongodb://127.0.0.1:27017/?directConnection=true');
expect(generateUri(options)).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000');
});
});

context('when no URI is provided', () => {
it('handles host', () => {
expect(generateUri({ _: [], host: 'localhost' })).to.equal('mongodb://localhost:27017/?directConnection=true');
expect(generateUri({ _: [], host: 'localhost' })).to.equal('mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('handles port', () => {
expect(generateUri({ _: [], port: '27018' })).to.equal('mongodb://127.0.0.1:27018/?directConnection=true');
expect(generateUri({ _: [], port: '27018' })).to.equal('mongodb://127.0.0.1:27018/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('handles both host and port', () => {
expect(generateUri({ _: [], host: 'localhost', port: '27018' })).to.equal('mongodb://localhost:27018/?directConnection=true');
expect(generateUri({ _: [], host: 'localhost', port: '27018' })).to.equal('mongodb://localhost:27018/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('handles host with port included', () => {
expect(generateUri({ _: [], host: 'localhost:27018' })).to.equal('mongodb://localhost:27018/?directConnection=true');
expect(generateUri({ _: [], host: 'localhost:27018' })).to.equal('mongodb://localhost:27018/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('throws if host has port AND port set to other value', () => {
try {
Expand All @@ -34,7 +34,7 @@ describe('uri-generator.generate-uri', () => {
}
});
it('handles host has port AND port set to equal value', () => {
expect(generateUri({ _: [], host: 'localhost:27018', port: '27018' })).to.equal('mongodb://localhost:27018/?directConnection=true');
expect(generateUri({ _: [], host: 'localhost:27018', port: '27018' })).to.equal('mongodb://localhost:27018/?directConnection=true&serverSelectionTimeoutMS=2000');
});
});

Expand Down Expand Up @@ -228,7 +228,7 @@ describe('uri-generator.generate-uri', () => {
const options = { _: [uri], port: '27018' };

it('uses the provided host with default port', () => {
expect(generateUri(options)).to.equal('mongodb://127.0.0.1:27018/foo?directConnection=true');
expect(generateUri(options)).to.equal('mongodb://127.0.0.1:27018/foo?directConnection=true&serverSelectionTimeoutMS=2000');
});
});

Expand Down
10 changes: 9 additions & 1 deletion packages/service-provider-core/src/uri-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ function generatePort(options: CliOptions): string {
* gssapiServiceName?: string; // needs to go in URI
*/
function generateUri(options: CliOptions): string {
return generateUriNormalized(options).toString();
const connectionString = generateUriNormalized(options);
if (connectionString.hosts.length === 1 &&
['localhost', '127.0.0.1'].includes(connectionString.hosts[0].split(':')[0])) {
const params = connectionString.searchParams;
if (!params.has('serverSelectionTimeoutMS')) {
params.set('serverSelectionTimeoutMS', '2000');
}
}
return connectionString.toString();
}
function generateUriNormalized(options: CliOptions): ConnectionString {
const uri = options._?.[0];
Expand Down
4 changes: 2 additions & 2 deletions packages/shell-api/src/field-level-encryption.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('Field Level Encryption', () => {
expect((await toShellResult(keyVault.help)).type).to.equal('Help');
});
it('calls print function', async() => {
expect((await toShellResult(clientEncryption)).printable).to.equal('ClientEncryption class for mongodb://localhost:27017/test?directConnection=true');
expect((await toShellResult(keyVault)).printable).to.equal('KeyVault class for mongodb://localhost:27017/test?directConnection=true');
expect((await toShellResult(clientEncryption)).printable).to.equal('ClientEncryption class for mongodb://localhost:27017/test?directConnection=true&serverSelectionTimeoutMS=2000');
expect((await toShellResult(keyVault)).printable).to.equal('KeyVault class for mongodb://localhost:27017/test?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('has metadata type', async() => {
expect((await toShellResult(clientEncryption)).type).to.equal('ClientEncryption');
Expand Down
4 changes: 2 additions & 2 deletions packages/shell-api/src/mongo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Mongo', () => {
describe('toShellResult', () => {
const mongo = new Mongo({} as any, 'localhost:37017');
it('value', async() => {
expect((await toShellResult(mongo)).printable).to.equal('mongodb://localhost:37017/test?directConnection=true');
expect((await toShellResult(mongo)).printable).to.equal('mongodb://localhost:37017/test?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('type', async() => {
expect((await toShellResult(mongo)).type).to.equal('Mongo');
Expand Down Expand Up @@ -561,7 +561,7 @@ describe('Mongo', () => {
const expectedResult = { ChangeStreamCursor: 1 } as any;
serviceProvider.watch.returns(expectedResult);
const result = mongo.watch();
expect(result).to.deep.equal(new ChangeStreamCursor(expectedResult, 'mongodb://localhost/?directConnection=true', mongo));
expect(result).to.deep.equal(new ChangeStreamCursor(expectedResult, 'mongodb://localhost/?directConnection=true&serverSelectionTimeoutMS=2000', mongo));
expect(mongo._internalState.currentCursor).to.equal(result);
});

Expand Down
28 changes: 15 additions & 13 deletions packages/shell-api/src/shell-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('ShellApi', () => {
it('returns a new Mongo object', async() => {
const m = await internalState.shellApi.Mongo('localhost:27017');
expect((await toShellResult(m)).type).to.equal('Mongo');
expect(m._uri).to.equal('mongodb://localhost:27017/test?directConnection=true');
expect(m._uri).to.equal('mongodb://localhost:27017/test?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('fails for non-CLI', async() => {
serviceProvider.platform = ReplPlatform.Browser;
Expand All @@ -204,11 +204,11 @@ describe('ShellApi', () => {
});
it('parses URI with mongodb://', async() => {
const m = await internalState.shellApi.Mongo('mongodb://127.0.0.1:27017');
expect(m._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true');
expect(m._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('parses URI with just db', async() => {
const m = await internalState.shellApi.Mongo('dbname');
expect(m._uri).to.equal('mongodb://127.0.0.1:27017/dbname?directConnection=true');
expect(m._uri).to.equal('mongodb://127.0.0.1:27017/dbname?directConnection=true&serverSelectionTimeoutMS=2000');
});
context('FLE', () => {
[
Expand All @@ -226,7 +226,7 @@ describe('ShellApi', () => {
}
});
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly(
'mongodb://127.0.0.1:27017/dbname?directConnection=true',
'mongodb://127.0.0.1:27017/dbname?directConnection=true&serverSelectionTimeoutMS=2000',
{
autoEncryption: {
keyVaultClient: undefined,
Expand All @@ -247,7 +247,7 @@ describe('ShellApi', () => {
}
});
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly(
'mongodb://127.0.0.1:27017/dbname?directConnection=true',
'mongodb://127.0.0.1:27017/dbname?directConnection=true&serverSelectionTimeoutMS=2000',
{
autoEncryption: {
keyVaultClient: undefined,
Expand All @@ -267,7 +267,7 @@ describe('ShellApi', () => {
keyVaultClient: mongo
});
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly(
'mongodb://127.0.0.1:27017/dbname?directConnection=true',
'mongodb://127.0.0.1:27017/dbname?directConnection=true&serverSelectionTimeoutMS=2000',
{
autoEncryption: {
keyVaultClient: rawClientStub,
Expand All @@ -291,7 +291,7 @@ describe('ShellApi', () => {
keyVaultClient: m
});
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly(
'mongodb://127.0.0.1:27017/dbname?directConnection=true',
'mongodb://127.0.0.1:27017/dbname?directConnection=true&serverSelectionTimeoutMS=2000',
{
autoEncryption: {
keyVaultClient: rc,
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('ShellApi', () => {
bypassAutoEncryption: true
});
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly(
'mongodb://127.0.0.1:27017/dbname?directConnection=true',
'mongodb://127.0.0.1:27017/dbname?directConnection=true&serverSelectionTimeoutMS=2000',
{
autoEncryption: {
keyVaultClient: undefined,
Expand All @@ -372,7 +372,7 @@ describe('ShellApi', () => {
serviceProvider.platform = ReplPlatform.CLI;
const db = await internalState.shellApi.connect('localhost:27017', 'username', 'pwd');
expect((await toShellResult(db)).type).to.equal('Database');
expect(db.getMongo()._uri).to.equal('mongodb://localhost:27017/test?directConnection=true');
expect(db.getMongo()._uri).to.equal('mongodb://localhost:27017/test?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('fails with no arg', async() => {
serviceProvider.platform = ReplPlatform.CLI;
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('ShellApi', () => {
it('returns a new Mongo object', async() => {
const m = await internalState.context.Mongo('mongodb://127.0.0.1:27017');
expect((await toShellResult(m)).type).to.equal('Mongo');
expect(m._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true');
expect(m._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('fails for non-CLI', async() => {
serviceProvider.platform = ReplPlatform.Browser;
Expand All @@ -473,14 +473,16 @@ describe('ShellApi', () => {
serviceProvider.platform = ReplPlatform.CLI;
const db = await internalState.context.connect('mongodb://127.0.0.1:27017');
expect((await toShellResult(db)).type).to.equal('Database');
expect(db.getMongo()._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true');
expect(db.getMongo()._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000');
});
it('handles username/pwd', async() => {
serviceProvider.platform = ReplPlatform.CLI;
const db = await internalState.context.connect('mongodb://127.0.0.1:27017', 'username', 'pwd');
expect((await toShellResult(db)).type).to.equal('Database');
expect(db.getMongo()._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true');
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly('mongodb://127.0.0.1:27017/?directConnection=true', { auth: { username: 'username', password: 'pwd' } });
expect(db.getMongo()._uri).to.equal('mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000');
expect(serviceProvider.getNewConnection).to.have.been.calledOnceWithExactly(
'mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000',
{ auth: { username: 'username', password: 'pwd' } });
});
});
describe('version', () => {
Expand Down