Skip to content

Commit 34e1621

Browse files
test cases passing
1 parent 2cadfcc commit 34e1621

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/mongo_client.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,52 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
364364
return true;
365365
}
366366
};
367+
this.checkForNonGenuineHosts();
368+
}
369+
370+
/** @internal */
371+
private checkForNonGenuineHosts() {
372+
if (this.mongoLogger && this.mongoLogger.logDestination) {
373+
const documentDBHostnames = this[kOptions].hosts.filter(
374+
(hostAddress: HostAddress) =>
375+
hostAddress.host &&
376+
(hostAddress.host.endsWith('.docdb.amazonaws.com') ||
377+
hostAddress.host.endsWith('.docdb-elastic.amazonaws.com'))
378+
);
379+
380+
const srvHostIsDocumentDB =
381+
this[kOptions].srvHost &&
382+
(this[kOptions].srvHost.endsWith('.docdb.amazonaws.com') ||
383+
this[kOptions].srvHost.endsWith('.docdb-elastic.amazonaws.com'));
384+
385+
if (documentDBHostnames.length !== 0 || srvHostIsDocumentDB) {
386+
this.mongoLogger.logDestination.write({
387+
t: new Date(),
388+
c: 'topology',
389+
s: 'info',
390+
message:
391+
'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb'
392+
});
393+
}
394+
395+
const cosmosDBHostnames = this[kOptions].hosts.filter(
396+
(hostAddress: HostAddress) =>
397+
hostAddress.host && hostAddress.host.endsWith('.cosmos.azure.com')
398+
);
399+
400+
const srvHostIsCosmosDB =
401+
this[kOptions].srvHost && this[kOptions].srvHost.endsWith('.cosmos.azure.com');
402+
403+
if (cosmosDBHostnames.length !== 0 || srvHostIsCosmosDB) {
404+
this.mongoLogger.logDestination.write({
405+
t: new Date(),
406+
c: 'topology',
407+
s: 'info',
408+
message:
409+
'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb'
410+
});
411+
}
412+
}
367413
}
368414

369415
/** @see MongoOptions */

test/unit/connection_string.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,4 +883,48 @@ describe('Connection String', function () {
883883
});
884884
});
885885
});
886+
887+
describe('non-genuine hosts', () => {
888+
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
889+
const docDBmsg =
890+
'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb';
891+
const cosmosDBmsg =
892+
'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb';
893+
const test_cases = [
894+
['non-SRV example uri', 'mongodb://a.example.com:27017,b.example.com:27017/', ''],
895+
['non-SRV default uri', 'mongodb://a.mongodb.net:27017', ''],
896+
['SRV example uri', 'mongodb+srv://a.example.com/', ''],
897+
['SRV default uri', 'mongodb+srv://a.mongodb.net/', ''],
898+
['non-SRV cosmosDB uri', 'mongodb://a.mongo.cosmos.azure.com:19555/', cosmosDBmsg],
899+
['non-SRV documentDB uri', 'mongodb://a.docdb.amazonaws.com:27017/', docDBmsg],
900+
['non-SRV documentDB uri ', 'mongodb://a.docdb-elastic.amazonaws.com:27017/', docDBmsg],
901+
['SRV cosmosDB uri', 'mongodb+srv://a.mongo.cosmos.azure.com/', cosmosDBmsg],
902+
['SRV documentDB uri', 'mongodb+srv://a.docdb.amazonaws.com/', docDBmsg],
903+
['SRV documentDB uri 2', 'mongodb+srv://a.docdb-elastic.amazonaws.com/', docDBmsg]
904+
];
905+
906+
context('when logging is turned on', () => {
907+
for (const [name, uri, message] of test_cases) {
908+
it(`${name} triggers ${message.length === 0 ? 'no' : 'correct info'} msg`, () => {
909+
const stream = {
910+
buffer: [],
911+
write(log) {
912+
this.buffer.push(log);
913+
}
914+
};
915+
new MongoClient(uri, {
916+
[loggerFeatureFlag]: true,
917+
mongodbLogPath: stream
918+
});
919+
920+
if (message.length > 0) {
921+
expect(stream.buffer).to.have.lengthOf(1);
922+
expect(stream.buffer[0]).to.have.property('message', message);
923+
} else {
924+
expect(stream.buffer).to.have.lengthOf(0);
925+
}
926+
});
927+
}
928+
});
929+
});
886930
});

0 commit comments

Comments
 (0)