2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../test_helper", __FILE__)
|
2010-12-30 20:40:50 +00:00
|
|
|
|
2012-01-30 17:06:20 +00:00
|
|
|
class URITest < Test::Unit::TestCase
|
2010-12-30 20:40:50 +00:00
|
|
|
include Mongo
|
|
|
|
|
|
|
|
def test_uri_without_port
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost')
|
|
|
|
assert_equal 1, parser.nodes.length
|
|
|
|
assert_equal 'localhost', parser.nodes[0][0]
|
|
|
|
assert_equal 27017, parser.nodes[0][1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_basic_uri
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018')
|
|
|
|
assert_equal 1, parser.nodes.length
|
|
|
|
assert_equal 'localhost', parser.nodes[0][0]
|
|
|
|
assert_equal 27018, parser.nodes[0][1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_multiple_uris
|
|
|
|
parser = Mongo::URIParser.new('mongodb://a.example.com:27018,b.example.com')
|
|
|
|
assert_equal 2, parser.nodes.length
|
|
|
|
assert_equal 'a.example.com', parser.nodes[0][0]
|
|
|
|
assert_equal 27018, parser.nodes[0][1]
|
|
|
|
assert_equal 'b.example.com', parser.nodes[1][0]
|
|
|
|
assert_equal 27017, parser.nodes[1][1]
|
|
|
|
end
|
|
|
|
|
2011-01-17 16:05:24 +00:00
|
|
|
def test_complex_passwords
|
|
|
|
parser = Mongo::URIParser.new('mongodb://bob:secret.word@a.example.com:27018/test')
|
|
|
|
assert_equal "bob", parser.auths[0]["username"]
|
|
|
|
assert_equal "secret.word", parser.auths[0]["password"]
|
|
|
|
|
|
|
|
parser = Mongo::URIParser.new('mongodb://bob:s-_3#%R.t@a.example.com:27018/test')
|
|
|
|
assert_equal "bob", parser.auths[0]["username"]
|
|
|
|
assert_equal "s-_3#%R.t", parser.auths[0]["password"]
|
|
|
|
end
|
|
|
|
|
2011-11-15 21:31:21 +00:00
|
|
|
def test_complex_usernames
|
|
|
|
parser = Mongo::URIParser.new('mongodb://b:ob:secret.word@a.example.com:27018/test')
|
|
|
|
assert_equal "b:ob", parser.auths[0]["username"]
|
|
|
|
end
|
|
|
|
|
2011-01-17 16:05:24 +00:00
|
|
|
def test_passwords_contain_no_commas
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
Mongo::URIParser.new('mongodb://bob:a,b@a.example.com:27018/test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-30 20:40:50 +00:00
|
|
|
def test_multiple_uris_with_auths
|
2011-09-08 22:31:33 +00:00
|
|
|
parser = Mongo::URIParser.new('mongodb://bob:secret@a.example.com:27018,b.example.com/test')
|
2010-12-30 20:40:50 +00:00
|
|
|
assert_equal 2, parser.nodes.length
|
|
|
|
assert_equal 'a.example.com', parser.nodes[0][0]
|
|
|
|
assert_equal 27018, parser.nodes[0][1]
|
|
|
|
assert_equal 'b.example.com', parser.nodes[1][0]
|
|
|
|
assert_equal 27017, parser.nodes[1][1]
|
|
|
|
assert_equal 2, parser.auths.length
|
|
|
|
assert_equal "bob", parser.auths[0]["username"]
|
|
|
|
assert_equal "secret", parser.auths[0]["password"]
|
|
|
|
assert_equal "test", parser.auths[0]["db_name"]
|
2011-09-08 22:31:33 +00:00
|
|
|
assert_equal "bob", parser.auths[1]["username"]
|
|
|
|
assert_equal "secret", parser.auths[1]["password"]
|
|
|
|
assert_equal "test", parser.auths[1]["db_name"]
|
2010-12-30 20:40:50 +00:00
|
|
|
end
|
|
|
|
|
2012-05-18 16:17:21 +00:00
|
|
|
def test_opts_with_semincolon_separator
|
2010-12-30 20:40:50 +00:00
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=direct;slaveok=true;safe=true')
|
|
|
|
assert_equal 'direct', parser.connect
|
2012-05-18 16:17:21 +00:00
|
|
|
assert parser.direct?
|
2010-12-30 20:40:50 +00:00
|
|
|
assert parser.slaveok
|
|
|
|
assert parser.safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_opts_with_amp_separator
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=direct&slaveok=true&safe=true')
|
|
|
|
assert_equal 'direct', parser.connect
|
2012-05-18 16:17:21 +00:00
|
|
|
assert parser.direct?
|
2010-12-30 20:40:50 +00:00
|
|
|
assert parser.slaveok
|
|
|
|
assert parser.safe
|
|
|
|
end
|
|
|
|
|
2012-06-04 15:03:49 +00:00
|
|
|
def test_opts_with_uri_encoded_stuff
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=%64%69%72%65%63%74&slaveok=%74%72%75%65&safe=true')
|
|
|
|
assert_equal 'direct', parser.connect
|
|
|
|
assert parser.direct?
|
|
|
|
assert parser.slaveok
|
|
|
|
assert parser.safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_opts_made_invalid_by_mixed_separators
|
|
|
|
assert_raise_error MongoArgumentError, "must not mix URL separators ; and &" do
|
|
|
|
Mongo::URIParser.new('mongodb://localhost:27018?replicaset=foo;bar&slaveok=true&safe=true')
|
|
|
|
end
|
|
|
|
end
|
2012-05-18 16:17:21 +00:00
|
|
|
|
2010-12-30 20:40:50 +00:00
|
|
|
def test_opts_safe
|
2012-03-16 20:17:33 +00:00
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;journal=true;fsync=true;wtimeoutMS=200')
|
2010-12-30 20:40:50 +00:00
|
|
|
assert parser.safe
|
|
|
|
assert_equal 2, parser.w
|
|
|
|
assert parser.fsync
|
2012-01-17 23:45:09 +00:00
|
|
|
assert parser.journal
|
2012-01-30 17:06:20 +00:00
|
|
|
assert_equal 200, parser.wtimeoutms
|
2012-01-17 23:45:09 +00:00
|
|
|
end
|
|
|
|
|
2012-01-26 21:52:25 +00:00
|
|
|
def test_opts_nonsafe_timeout
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?connectTimeoutMS=5500&socketTimeoutMS=500')
|
2012-01-30 17:06:20 +00:00
|
|
|
assert_equal 5.5, parser.connecttimeoutms
|
|
|
|
assert_equal 0.5, parser.sockettimeoutms
|
2010-12-30 20:40:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_opts_replica_set
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=replicaset;replicaset=foo')
|
|
|
|
assert_equal 'foo', parser.replicaset
|
|
|
|
assert_equal 'replicaset', parser.connect
|
2012-05-18 16:17:21 +00:00
|
|
|
assert parser.replicaset?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_opts_conflicting_replica_set
|
|
|
|
assert_raise_error MongoArgumentError, "connect=direct conflicts with setting a replicaset name" do
|
|
|
|
Mongo::URIParser.new('mongodb://localhost:27018?connect=direct;replicaset=foo')
|
|
|
|
end
|
2010-12-30 20:40:50 +00:00
|
|
|
end
|
2012-01-30 17:06:20 +00:00
|
|
|
|
|
|
|
def test_case_insensitivity
|
|
|
|
parser = Mongo::URIParser.new('mongodb://localhost:27018?wtimeoutms=500&JOURNAL=true&SaFe=true')
|
|
|
|
assert_equal 500, parser.wtimeoutms
|
|
|
|
assert_equal true, parser.journal
|
|
|
|
assert_equal true, parser.safe
|
|
|
|
end
|
2010-12-30 20:40:50 +00:00
|
|
|
end
|