Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(uri_parser): ensure default port is 27017
Browse files Browse the repository at this point in the history
NODE-1597
  • Loading branch information
mbroadst committed Aug 1, 2018
1 parent dd4eb9a commit 426a95e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/uri_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function parseConnectionString(uri, options, callback) {

const result = {
host: parsedHost.hostname,
port: parsedHost.port ? parseInt(parsedHost.port) : null
port: parsedHost.port ? parseInt(parsedHost.port) : 27017
};

if (result.port === 0) {
Expand Down
21 changes: 19 additions & 2 deletions test/tests/unit/connection_string_spec_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const parseConnectionString = require('../../../lib/uri_parser');
const fs = require('fs');
const f = require('util').format;
const expect = require('chai').expect;
const punycode = require('punycode');
const MongoParseError = require('../../../lib/error').MongoParseError;
const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-subset'));

// NOTE: These are cases we could never check for unless we write out own
// url parser. The node parser simply won't let these through, so we
Expand All @@ -22,6 +24,14 @@ const skipTests = [
];

describe('Connection String (spec)', function() {
it('should provide a default port if one is not provided', function(done) {
parseConnectionString('mongodb://hostname', function(err, result) {
expect(err).to.not.exist;
expect(result.hosts[0].port).to.equal(27017);
done();
});
});

const testFiles = fs
.readdirSync(f('%s/../spec/connection-string', __dirname))
.filter(x => x.indexOf('.json') !== -1)
Expand Down Expand Up @@ -63,7 +73,14 @@ describe('Connection String (spec)', function() {
return host;
});

expect(result.hosts).to.eql(test.hosts);
// remove values that require no validation
test.hosts.forEach(host => {
Object.keys(host).forEach(key => {
if (host[key] == null) delete host[key];
});
});

expect(result.hosts).to.containSubset(test.hosts);
expect(result.auth).to.eql(test.auth);
expect(result.options).to.eql(test.options);
}
Expand Down

0 comments on commit 426a95e

Please sign in to comment.