|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const mock = require('mongodb-mock-server'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const MongoServerSelectionError = require('../../lib/core/error').MongoServerSelectionError; |
| 6 | + |
| 7 | +const minCompatErrMsg = `minimum wire version ${Number.MAX_SAFE_INTEGER - |
| 8 | + 1}, but this version of the Node.js Driver requires at most 13`; |
| 9 | +const maxCompatErrMsg = `reports maximum wire version 1, but this version of the Node.js Driver requires at least 2`; |
| 10 | + |
| 11 | +describe('Wire Protocol Version', () => { |
| 12 | + let server; |
| 13 | + |
| 14 | + function setWireProtocolMessageHandler(min, max) { |
| 15 | + server.setMessageHandler(req => { |
| 16 | + const doc = req.document; |
| 17 | + if (doc.ismaster || doc.hello) { |
| 18 | + const hello = Object.assign({}, mock.DEFAULT_ISMASTER_36, { |
| 19 | + minWireVersion: min, |
| 20 | + maxWireVersion: max |
| 21 | + }); |
| 22 | + return req.reply(hello); |
| 23 | + } |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + return mock.createServer().then(s => { |
| 29 | + server = s; |
| 30 | + }); |
| 31 | + }); |
| 32 | + afterEach(() => { |
| 33 | + return mock.cleanup(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('minimum is greater than 13', () => { |
| 37 | + it('should raise a compatibility error', function() { |
| 38 | + setWireProtocolMessageHandler(Number.MAX_SAFE_INTEGER - 1, Number.MAX_SAFE_INTEGER); |
| 39 | + |
| 40 | + const client = this.configuration.newClient( |
| 41 | + `mongodb://${server.uri()}/wireVersionTest?serverSelectionTimeoutMS=200` |
| 42 | + ); |
| 43 | + return client |
| 44 | + .connect() |
| 45 | + .then(() => { |
| 46 | + expect.fail('should fail to select server!'); |
| 47 | + }) |
| 48 | + .catch(error => { |
| 49 | + expect(error).to.be.instanceOf(MongoServerSelectionError); |
| 50 | + expect(error) |
| 51 | + .to.have.property('message') |
| 52 | + .that.includes(minCompatErrMsg); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('maximum is less than 2', () => { |
| 58 | + it('should raise a compatibility error', function() { |
| 59 | + setWireProtocolMessageHandler(1, 1); |
| 60 | + |
| 61 | + const client = this.configuration.newClient( |
| 62 | + `mongodb://${server.uri()}/wireVersionTest?serverSelectionTimeoutMS=200` |
| 63 | + ); |
| 64 | + return client |
| 65 | + .connect() |
| 66 | + .then(() => { |
| 67 | + expect.fail('should fail to select server!'); |
| 68 | + }) |
| 69 | + .catch(error => { |
| 70 | + expect(error).to.be.instanceOf(MongoServerSelectionError); |
| 71 | + expect(error) |
| 72 | + .to.have.property('message') |
| 73 | + .that.includes(maxCompatErrMsg); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments