|
8 | 8 | LEGACY_HELLO_COMMAND, |
9 | 9 | type MongoClient |
10 | 10 | } from '../../mongodb'; |
| 11 | +import { sleep } from '../../tools/utils'; |
11 | 12 |
|
12 | 13 | type EnvironmentVariables = Array<[string, string]>; |
13 | 14 |
|
@@ -200,5 +201,88 @@ describe('Client Metadata Update Prose Tests', function () { |
200 | 201 |
|
201 | 202 | afterEach(async function () { |
202 | 203 | await client?.close(); |
| 204 | + sinon.restore(); |
| 205 | + }); |
| 206 | + |
| 207 | + describe('Test 1: Test that the driver updates metadata', function () { |
| 208 | + let initialClientMetadata; |
| 209 | + let updatedClientMetadata; |
| 210 | + |
| 211 | + const tests = [ |
| 212 | + { testCase: 1, name: 'framework', version: '2.0', platform: 'Framework Platform' }, |
| 213 | + { testCase: 2, name: 'framework', version: '2.0' }, |
| 214 | + { testCase: 3, name: 'framework', platform: 'Framework Platform' }, |
| 215 | + { testCase: 4, name: 'framework' } |
| 216 | + ]; |
| 217 | + |
| 218 | + for (const { testCase, name, version, platform } of tests) { |
| 219 | + context(`Case: ${testCase}`, function () { |
| 220 | + // 1. Create a `MongoClient` instance with the following: |
| 221 | + // - `maxIdleTimeMS` set to `1ms` |
| 222 | + // - Wrapping library metadata: |
| 223 | + // | Field | Value | |
| 224 | + // | -------- | ---------------- | |
| 225 | + // | name | library | |
| 226 | + // | version | 1.2 | |
| 227 | + // | platform | Library Platform | |
| 228 | + // 2. Send a `ping` command to the server and verify that the command succeeds. |
| 229 | + // 3. Save intercepted `client` document as `initialClientMetadata`. |
| 230 | + // 4. Wait 5ms for the connection to become idle. |
| 231 | + beforeEach(async function () { |
| 232 | + client = this.configuration.newClient( |
| 233 | + {}, |
| 234 | + { |
| 235 | + maxIdleTimeMS: 1, |
| 236 | + monitorCommands: true, |
| 237 | + driverInfo: { name: 'library', version: '1.2', platform: 'Library Platform' } |
| 238 | + } |
| 239 | + ); |
| 240 | + |
| 241 | + sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { |
| 242 | + // @ts-expect-error: sinon will place wrappedMethod on the command method. |
| 243 | + const command = Connection.prototype.command.wrappedMethod.bind(this); |
| 244 | + |
| 245 | + if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { |
| 246 | + if (!initialClientMetadata) { |
| 247 | + initialClientMetadata = cmd.client; |
| 248 | + } else { |
| 249 | + updatedClientMetadata = cmd.client; |
| 250 | + } |
| 251 | + } |
| 252 | + return command(ns, cmd, options); |
| 253 | + }); |
| 254 | + |
| 255 | + await client.db('test').command({ ping: 1 }); |
| 256 | + await sleep(5); |
| 257 | + }); |
| 258 | + |
| 259 | + it('appends the metadata', async function () { |
| 260 | + // 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata. |
| 261 | + // 2. Send a `ping` command to the server and verify: |
| 262 | + // - The command succeeds. |
| 263 | + // - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello` |
| 264 | + // command, with values separated by a pipe `|`. |
| 265 | + client.appendMetadata({ name, version, platform }); |
| 266 | + await client.db('test').command({ ping: 1 }); |
| 267 | + |
| 268 | + expect(updatedClientMetadata.driver.name).to.equal( |
| 269 | + name |
| 270 | + ? `${initialClientMetadata.driver.name}|${name}` |
| 271 | + : initialClientMetadata.driver.name |
| 272 | + ); |
| 273 | + expect(updatedClientMetadata.driver.version).to.equal( |
| 274 | + version |
| 275 | + ? `${initialClientMetadata.driver.version}|${version}` |
| 276 | + : initialClientMetadata.driver.version |
| 277 | + ); |
| 278 | + expect(updatedClientMetadata.platform).to.equal( |
| 279 | + platform |
| 280 | + ? `${initialClientMetadata.platform}|${platform}` |
| 281 | + : initialClientMetadata.platform |
| 282 | + ); |
| 283 | + expect(updatedClientMetadata.os).to.deep.equal(initialClientMetadata.os); |
| 284 | + }); |
| 285 | + }); |
| 286 | + } |
203 | 287 | }); |
204 | 288 | }); |
0 commit comments