diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index 41bcade..26dcd7c 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -522,11 +522,11 @@ module Mongo port = port.to_i db = matches[8] - if (uname || pwd || db) && !(uname && pwd && db) + if uname && pwd && db + add_auth(db, uname, pwd) + elsif uname || pwd || db raise MongoArgumentError, "MongoDB URI must include all three of username, password, " + "and db if any one of these is specified." - else - add_auth(db, uname, pwd) end nodes << [host, port] diff --git a/test/connection_test.rb b/test/connection_test.rb index 3816616..1dbb790 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -24,6 +24,12 @@ class TestConnection < Test::Unit::TestCase assert Mongo::Support.ok?(server_info) end + def test_connection_uri + con = Connection.from_uri("mongodb://localhost:27017") + assert_equal "localhost", con.host + assert_equal 27017, con.port + end + def test_server_version assert_match /\d\.\d+(\.\d+)?/, @mongo.server_version.to_s end