Skip to content

Commit 5fc5079

Browse files
committed
test: second round of prose tests
1 parent 15394bf commit 5fc5079

File tree

4 files changed

+98
-17
lines changed

4 files changed

+98
-17
lines changed

src/cmap/handshake/client_metadata.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
123123
};
124124

125125
// This is where we handle additional driver info added after client construction.
126-
if (options.additionalDriverInfo) {
127-
const { name: n = '', version: v = '' } = options.additionalDriverInfo;
128-
if (n.length > 0) {
129-
driverInfo.name = `${driverInfo.name}|${n}`;
130-
}
131-
if (v.length > 0) {
132-
driverInfo.version = `${driverInfo.version}|${v}`;
126+
if (options.additionalDriverInfo.length > 0) {
127+
for (const { name: n = '', version: v = '' } of options.additionalDriverInfo) {
128+
if (n.length > 0) {
129+
driverInfo.name = `${driverInfo.name}|${n}`;
130+
}
131+
if (v.length > 0) {
132+
driverInfo.version = `${driverInfo.version}|${v}`;
133+
}
133134
}
134135
}
135136

@@ -143,10 +144,12 @@ export function makeClientMetadata(options: MakeClientMetadataOptions): ClientMe
143144
if (platform.length > 0) {
144145
runtimeInfo = `${runtimeInfo}|${platform}`;
145146
}
146-
if (options.additionalDriverInfo) {
147-
const { platform: p = '' } = options.additionalDriverInfo;
148-
if (p.length > 0) {
149-
runtimeInfo = `${runtimeInfo}|${p}`;
147+
148+
if (options.additionalDriverInfo.length > 0) {
149+
for (const { platform: p = '' } of options.additionalDriverInfo) {
150+
if (p.length > 0) {
151+
runtimeInfo = `${runtimeInfo}|${p}`;
152+
}
150153
}
151154
}
152155

src/connection_string.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ export function parseOptions(
540540
}
541541
);
542542

543+
// Set the default for the additional driver info.
544+
mongoOptions.additionalDriverInfo = [];
545+
543546
mongoOptions.metadata = makeClientMetadata(mongoOptions);
544547

545548
mongoOptions.extendedMetadata = addContainerMetadata(mongoOptions.metadata).then(

src/mongo_client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
405405
| 'crl'
406406
| 'key'
407407
| 'cert'
408+
| 'driverInfo'
408409
| 'additionalDriverInfo'
409410
| 'metadata'
410411
| 'extendedMetadata'
@@ -417,6 +418,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
417418
| 'crl'
418419
| 'key'
419420
| 'cert'
421+
| 'driverInfo'
420422
| 'additionalDriverInfo'
421423
| 'metadata'
422424
| 'extendedMetadata'
@@ -484,7 +486,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
484486
* @param driverInfo - Information abou the application or libraary.
485487
*/
486488
appendMetadata(driverInfo: DriverInfo) {
487-
this.options.additionalDriverInfo = driverInfo;
489+
this.options.additionalDriverInfo.push(driverInfo);
488490
this.options.metadata = makeClientMetadata(this.options);
489491
this.options.extendedMetadata = addContainerMetadata(this.options.metadata)
490492
.then(undefined, squashError)
@@ -1073,10 +1075,8 @@ export interface MongoOptions
10731075
dbName: string;
10741076
/** @deprecated - Will be made internal in a future major release. */
10751077
metadata: ClientMetadata;
1076-
/** @internal */
10771078
extendedMetadata: Promise<Document>;
1078-
/** @internal */
1079-
additionalDriverInfo: DriverInfo;
1079+
additionalDriverInfo: DriverInfo[];
10801080
/** @internal */
10811081
autoEncrypter?: AutoEncrypter;
10821082
/** @internal */

test/integration/mongodb-handshake/mongodb-handshake.prose.test.ts

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe('Client Metadata Update Prose Tests', function () {
204204
sinon.restore();
205205
});
206206

207-
describe('Test 1: Test that the driver updates metadata', function () {
207+
describe('Test 2: Multiple Successive Metadata Updates', function () {
208208
let initialClientMetadata;
209209
let updatedClientMetadata;
210210

@@ -233,7 +233,6 @@ describe('Client Metadata Update Prose Tests', function () {
233233
{},
234234
{
235235
maxIdleTimeMS: 1,
236-
monitorCommands: true,
237236
driverInfo: { name: 'library', version: '1.2', platform: 'Library Platform' }
238237
}
239238
);
@@ -285,4 +284,80 @@ describe('Client Metadata Update Prose Tests', function () {
285284
});
286285
}
287286
});
287+
288+
describe('Test 1: Test that the driver updates metadata', function () {
289+
let initialClientMetadata;
290+
let updatedClientMetadata;
291+
292+
const tests = [
293+
{ testCase: 1, name: 'framework', version: '2.0', platform: 'Framework Platform' },
294+
{ testCase: 2, name: 'framework', version: '2.0' },
295+
{ testCase: 3, name: 'framework', platform: 'Framework Platform' },
296+
{ testCase: 4, name: 'framework' }
297+
];
298+
299+
for (const { testCase, name, version, platform } of tests) {
300+
context(`Case: ${testCase}`, function () {
301+
// 1. Create a `MongoClient` instance with the following:
302+
// - `maxIdleTimeMS` set to `1ms`
303+
// 2. Append the following `DriverInfoOptions` to the `MongoClient` metadata:
304+
// | Field | Value |
305+
// | -------- | ---------------- |
306+
// | name | library |
307+
// | version | 1.2 |
308+
// | platform | Library Platform |
309+
// 3. Send a `ping` command to the server and verify that the command succeeds.
310+
// 4. Save intercepted `client` document as `updatedClientMetadata`.
311+
// 5. Wait 5ms for the connection to become idle.
312+
beforeEach(async function () {
313+
client = this.configuration.newClient({}, { maxIdleTimeMS: 1 });
314+
client.appendMetadata({ name: 'library', version: '1.2', platform: 'Library Platform' });
315+
316+
sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) {
317+
// @ts-expect-error: sinon will place wrappedMethod on the command method.
318+
const command = Connection.prototype.command.wrappedMethod.bind(this);
319+
320+
if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) {
321+
if (!initialClientMetadata) {
322+
initialClientMetadata = cmd.client;
323+
} else {
324+
updatedClientMetadata = cmd.client;
325+
}
326+
}
327+
return command(ns, cmd, options);
328+
});
329+
330+
await client.db('test').command({ ping: 1 });
331+
await sleep(5);
332+
});
333+
334+
it('appends the metadata', async function () {
335+
// 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata.
336+
// 2. Send a `ping` command to the server and verify:
337+
// - The command succeeds.
338+
// - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello`
339+
// command, with values separated by a pipe `|`.
340+
client.appendMetadata({ name, version, platform });
341+
await client.db('test').command({ ping: 1 });
342+
343+
expect(updatedClientMetadata.driver.name).to.equal(
344+
name
345+
? `${initialClientMetadata.driver.name}|${name}`
346+
: initialClientMetadata.driver.name
347+
);
348+
expect(updatedClientMetadata.driver.version).to.equal(
349+
version
350+
? `${initialClientMetadata.driver.version}|${version}`
351+
: initialClientMetadata.driver.version
352+
);
353+
expect(updatedClientMetadata.platform).to.equal(
354+
platform
355+
? `${initialClientMetadata.platform}|${platform}`
356+
: initialClientMetadata.platform
357+
);
358+
expect(updatedClientMetadata.os).to.deep.equal(initialClientMetadata.os);
359+
});
360+
});
361+
}
362+
});
288363
});

0 commit comments

Comments
 (0)