summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2017-10-06 11:31:06 -0400
committerSara Golemon <sara.golemon@mongodb.com>2017-10-06 15:22:16 -0400
commitbe9f8cd19c1d3fa7b0fea18c965cd9964fc7ed68 (patch)
tree10156ac041dac0d2a6701c3421cfe6fbe7d47361
parente65d9be4895ac11837abc0d04007e2f83143d95f (diff)
downloadmongo-be9f8cd19c1d3fa7b0fea18c965cd9964fc7ed68.tar.gz
SERVER-31437 Fix parsing of host/db connect string
-rw-r--r--jstests/core/shell_connection_strings.js33
-rw-r--r--src/mongo/shell/dbshell.cpp6
2 files changed, 36 insertions, 3 deletions
diff --git a/jstests/core/shell_connection_strings.js b/jstests/core/shell_connection_strings.js
new file mode 100644
index 00000000000..22861e6ce25
--- /dev/null
+++ b/jstests/core/shell_connection_strings.js
@@ -0,0 +1,33 @@
+// Test mongo shell connect strings.
+(function() {
+ 'use strict';
+
+ const mongod = new MongoURI(db.getMongo().host).servers[0];
+ const host = mongod.host;
+ const port = mongod.port;
+
+ function testConnect(ok, ...args) {
+ const exitCode = runMongoProgram('mongo', '--eval', ';', ...args);
+ if (ok) {
+ assert.eq(exitCode, 0, "failed to connect with `" + args.join(' ') + "`");
+ } else {
+ assert.neq(
+ exitCode, 0, "unexpectedly succeeded connecting with `" + args.join(' ') + "`");
+ }
+ }
+
+ testConnect(true, `${host}:${port}`);
+ testConnect(true, `${host}:${port}/test`);
+ testConnect(true, `${host}:${port}/admin`);
+ testConnect(true, host, '--port', port);
+ testConnect(true, '--host', host, '--port', port, 'test');
+ testConnect(true, '--host', host, '--port', port, 'admin');
+ testConnect(true, `mongodb://${host}:${port}/test`);
+ testConnect(true, `mongodb://${host}:${port}/test?connectTimeoutMS=10000`);
+
+ // if a full URI is provided, you cannot also specify host or port
+ testConnect(false, `${host}/test`, '--port', port);
+ testConnect(false, `mongodb://${host}:${port}/test`, '--port', port);
+ testConnect(false, `mongodb://${host}:${port}/test`, '--host', host);
+ testConnect(false, `mongodb://${host}:${port}/test`, '--host', host, '--port', port);
+})();
diff --git a/src/mongo/shell/dbshell.cpp b/src/mongo/shell/dbshell.cpp
index 9273e7b6f25..34cdbed3d9e 100644
--- a/src/mongo/shell/dbshell.cpp
+++ b/src/mongo/shell/dbshell.cpp
@@ -332,11 +332,11 @@ string getURIFromArgs(const std::string& arg, const std::string& host, const std
// --host provided, treat it as the connect string and get db from positional arg.
return parseDbHost(arg, host);
} else if (arg.size()) {
- // --host missing, but we have a potential db/host positional arg.
+ // --host missing, but we have a potential host/db positional arg.
const auto slashPos = arg.find('/');
if (slashPos != std::string::npos) {
- // db/host pair.
- return parseDbHost(arg.substr(0, slashPos), arg.substr(slashPos + 1));
+ // host/db pair.
+ return parseDbHost(arg.substr(slashPos + 1), arg.substr(0, slashPos));
}
// Compatability formats.