Skip to content

Commit

Permalink
Add parsing validation
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Feb 11, 2022
1 parent 9242de5 commit 15127fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/connection_string.ts
Expand Up @@ -464,6 +464,10 @@ export function parseOptions(
}
}

if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {
throw new MongoParseError('directConnection option requires exactly one host');
}

if (
!mongoOptions.proxyHost &&
(mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)
Expand Down
13 changes: 13 additions & 0 deletions test/unit/connection_string.test.ts
Expand Up @@ -148,6 +148,19 @@ describe('Connection String', function () {
expect(options.replicaSet).to.equal('123abc');
});

context('when directionConnection is set', () => {
it('sets directConnection successfully when there is one host', () => {
const options = parseOptions('mongodb://localhost:27027/?directConnection=true');
expect(options.directConnection).to.be.true;
});

it('throws when directConnection is true and there is more than one host', () => {
expect(() =>
parseOptions('mongodb://localhost:27027,localhost:27018/?directConnection=true')
).to.throw(MongoParseError, 'directConnection option requires exactly one host');
});
});

context('when both tls and ssl options are provided', function () {
context('when the options are provided in the URI', function () {
context('when the options are equal', function () {
Expand Down

0 comments on commit 15127fe

Please sign in to comment.