mongo 1.10.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/LICENSE +190 -0
- data/README.md +149 -0
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/bin/mongo_console +43 -0
- data/ext/jsasl/target/jsasl.jar +0 -0
- data/lib/mongo.rb +90 -0
- data/lib/mongo/bulk_write_collection_view.rb +380 -0
- data/lib/mongo/collection.rb +1164 -0
- data/lib/mongo/collection_writer.rb +364 -0
- data/lib/mongo/connection.rb +19 -0
- data/lib/mongo/connection/node.rb +239 -0
- data/lib/mongo/connection/pool.rb +347 -0
- data/lib/mongo/connection/pool_manager.rb +325 -0
- data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
- data/lib/mongo/connection/socket.rb +18 -0
- data/lib/mongo/connection/socket/socket_util.rb +37 -0
- data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
- data/lib/mongo/connection/socket/tcp_socket.rb +86 -0
- data/lib/mongo/connection/socket/unix_socket.rb +39 -0
- data/lib/mongo/cursor.rb +719 -0
- data/lib/mongo/db.rb +735 -0
- data/lib/mongo/exception.rb +88 -0
- data/lib/mongo/functional.rb +21 -0
- data/lib/mongo/functional/authentication.rb +318 -0
- data/lib/mongo/functional/logging.rb +85 -0
- data/lib/mongo/functional/read_preference.rb +174 -0
- data/lib/mongo/functional/sasl_java.rb +48 -0
- data/lib/mongo/functional/uri_parser.rb +374 -0
- data/lib/mongo/functional/write_concern.rb +66 -0
- data/lib/mongo/gridfs.rb +18 -0
- data/lib/mongo/gridfs/grid.rb +112 -0
- data/lib/mongo/gridfs/grid_ext.rb +53 -0
- data/lib/mongo/gridfs/grid_file_system.rb +163 -0
- data/lib/mongo/gridfs/grid_io.rb +484 -0
- data/lib/mongo/legacy.rb +140 -0
- data/lib/mongo/mongo_client.rb +702 -0
- data/lib/mongo/mongo_replica_set_client.rb +523 -0
- data/lib/mongo/mongo_sharded_client.rb +159 -0
- data/lib/mongo/networking.rb +370 -0
- data/lib/mongo/utils.rb +19 -0
- data/lib/mongo/utils/conversions.rb +110 -0
- data/lib/mongo/utils/core_ext.rb +70 -0
- data/lib/mongo/utils/server_version.rb +69 -0
- data/lib/mongo/utils/support.rb +80 -0
- data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
- data/mongo.gemspec +36 -0
- data/test/functional/authentication_test.rb +35 -0
- data/test/functional/bulk_api_stress_test.rb +133 -0
- data/test/functional/bulk_write_collection_view_test.rb +1129 -0
- data/test/functional/client_test.rb +565 -0
- data/test/functional/collection_test.rb +2073 -0
- data/test/functional/collection_writer_test.rb +83 -0
- data/test/functional/conversions_test.rb +163 -0
- data/test/functional/cursor_fail_test.rb +63 -0
- data/test/functional/cursor_message_test.rb +57 -0
- data/test/functional/cursor_test.rb +625 -0
- data/test/functional/db_api_test.rb +819 -0
- data/test/functional/db_connection_test.rb +27 -0
- data/test/functional/db_test.rb +344 -0
- data/test/functional/grid_file_system_test.rb +285 -0
- data/test/functional/grid_io_test.rb +252 -0
- data/test/functional/grid_test.rb +273 -0
- data/test/functional/pool_test.rb +62 -0
- data/test/functional/safe_test.rb +98 -0
- data/test/functional/ssl_test.rb +29 -0
- data/test/functional/support_test.rb +62 -0
- data/test/functional/timeout_test.rb +58 -0
- data/test/functional/uri_test.rb +330 -0
- data/test/functional/write_concern_test.rb +118 -0
- data/test/helpers/general.rb +50 -0
- data/test/helpers/test_unit.rb +317 -0
- data/test/replica_set/authentication_test.rb +35 -0
- data/test/replica_set/basic_test.rb +174 -0
- data/test/replica_set/client_test.rb +341 -0
- data/test/replica_set/complex_connect_test.rb +77 -0
- data/test/replica_set/connection_test.rb +138 -0
- data/test/replica_set/count_test.rb +64 -0
- data/test/replica_set/cursor_test.rb +212 -0
- data/test/replica_set/insert_test.rb +140 -0
- data/test/replica_set/max_values_test.rb +145 -0
- data/test/replica_set/pinning_test.rb +55 -0
- data/test/replica_set/query_test.rb +73 -0
- data/test/replica_set/read_preference_test.rb +214 -0
- data/test/replica_set/refresh_test.rb +175 -0
- data/test/replica_set/replication_ack_test.rb +94 -0
- data/test/replica_set/ssl_test.rb +32 -0
- data/test/sharded_cluster/basic_test.rb +197 -0
- data/test/shared/authentication/basic_auth_shared.rb +286 -0
- data/test/shared/authentication/bulk_api_auth_shared.rb +259 -0
- data/test/shared/authentication/gssapi_shared.rb +164 -0
- data/test/shared/authentication/sasl_plain_shared.rb +96 -0
- data/test/shared/ssl_shared.rb +235 -0
- data/test/test_helper.rb +56 -0
- data/test/threading/basic_test.rb +120 -0
- data/test/tools/mongo_config.rb +608 -0
- data/test/tools/mongo_config_test.rb +160 -0
- data/test/unit/client_test.rb +347 -0
- data/test/unit/collection_test.rb +166 -0
- data/test/unit/connection_test.rb +325 -0
- data/test/unit/cursor_test.rb +299 -0
- data/test/unit/db_test.rb +136 -0
- data/test/unit/grid_test.rb +76 -0
- data/test/unit/mongo_sharded_client_test.rb +48 -0
- data/test/unit/node_test.rb +93 -0
- data/test/unit/pool_manager_test.rb +142 -0
- data/test/unit/read_pref_test.rb +115 -0
- data/test/unit/read_test.rb +159 -0
- data/test/unit/safe_test.rb +158 -0
- data/test/unit/sharding_pool_manager_test.rb +84 -0
- data/test/unit/write_concern_test.rb +175 -0
- metadata +260 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,166 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
module Mongo
|
18
|
+
class Collection
|
19
|
+
attr_reader :operation_writer,
|
20
|
+
:command_writer
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class CollectionUnitTest < Test::Unit::TestCase
|
25
|
+
|
26
|
+
context "Basic operations: " do
|
27
|
+
setup do
|
28
|
+
@logger = mock()
|
29
|
+
@logger.stubs(:level => 0)
|
30
|
+
@logger.expects(:debug)
|
31
|
+
|
32
|
+
@client = MongoClient.new('localhost', 27017, :logger => @logger, :connect => false)
|
33
|
+
@db = @client[TEST_DB]
|
34
|
+
@coll = @db.collection('collection-unit-test')
|
35
|
+
end
|
36
|
+
|
37
|
+
should "send update message" do
|
38
|
+
@client.expects(:send_message_with_gle).with do |op, msg, log|
|
39
|
+
op == 2001
|
40
|
+
end
|
41
|
+
@coll.operation_writer.stubs(:log_operation)
|
42
|
+
@coll.update({}, {:title => 'Moby Dick'})
|
43
|
+
end
|
44
|
+
|
45
|
+
should "send insert message" do
|
46
|
+
@client.expects(:send_message_with_gle).with do |op, msg, log|
|
47
|
+
op == 2002
|
48
|
+
end
|
49
|
+
@coll.operation_writer.expects(:log_operation).with do |name, payload|
|
50
|
+
(name == :insert) && payload[:documents][:title].include?('Moby')
|
51
|
+
end
|
52
|
+
@coll.insert({:title => 'Moby Dick'})
|
53
|
+
end
|
54
|
+
|
55
|
+
should "send sort data" do
|
56
|
+
@client.expects(:checkout_reader).returns(new_mock_socket)
|
57
|
+
@client.expects(:receive_message).with do |op, msg, log, sock|
|
58
|
+
op == 2004
|
59
|
+
end.returns([[], 0, 0])
|
60
|
+
@logger.expects(:debug)
|
61
|
+
@coll.find({:title => 'Moby Dick'}).sort([['title', 1], ['author', 1]]).next_document
|
62
|
+
end
|
63
|
+
|
64
|
+
should "not log binary data" do
|
65
|
+
data = BSON::Binary.new(("BINARY " * 1000).unpack("c*"))
|
66
|
+
@client.expects(:send_message_with_gle).with do |op, msg, log|
|
67
|
+
op == 2002
|
68
|
+
end
|
69
|
+
@coll.operation_writer.expects(:log_operation).with do |name, payload|
|
70
|
+
(name == :insert) && payload[:documents][:data].inspect.include?('Binary')
|
71
|
+
end
|
72
|
+
@coll.insert({:data => data})
|
73
|
+
end
|
74
|
+
|
75
|
+
should "send safe update message" do
|
76
|
+
@client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
|
77
|
+
op == 2001
|
78
|
+
end
|
79
|
+
@coll.operation_writer.expects(:log_operation).with do |name, payload|
|
80
|
+
(name == :update) && payload[:documents][:title].include?('Moby')
|
81
|
+
end
|
82
|
+
@coll.update({}, {:title => 'Moby Dick'})
|
83
|
+
end
|
84
|
+
|
85
|
+
should "send safe update message with legacy" do
|
86
|
+
connection = Connection.new('localhost', 27017, :safe => true, :connect => false)
|
87
|
+
db = connection[TEST_DB]
|
88
|
+
coll = db.collection('collection-unit-test')
|
89
|
+
connection.expects(:send_message_with_gle).with do |op, msg, db_name, log|
|
90
|
+
op == 2001
|
91
|
+
end
|
92
|
+
coll.operation_writer.expects(:log_operation).with do |name, payload|
|
93
|
+
(name == :update) && payload[:documents][:title].include?('Moby')
|
94
|
+
end
|
95
|
+
coll.update({}, {:title => 'Moby Dick'})
|
96
|
+
end
|
97
|
+
|
98
|
+
should "send safe insert message" do
|
99
|
+
@client.expects(:send_message_with_gle).with do |op, msg, db_name, log|
|
100
|
+
op == 2001
|
101
|
+
end
|
102
|
+
@coll.operation_writer.stubs(:log_operation)
|
103
|
+
@coll.update({}, {:title => 'Moby Dick'})
|
104
|
+
end
|
105
|
+
|
106
|
+
should "not call insert for each ensure_index call" do
|
107
|
+
@coll.expects(:generate_indexes).once
|
108
|
+
|
109
|
+
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
110
|
+
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
111
|
+
end
|
112
|
+
|
113
|
+
should "call generate_indexes for a new type on the same field for ensure_index" do
|
114
|
+
@coll.expects(:generate_indexes).twice
|
115
|
+
|
116
|
+
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
117
|
+
@coll.ensure_index [["x", Mongo::ASCENDING]]
|
118
|
+
end
|
119
|
+
|
120
|
+
should "call generate_indexes twice because the cache time is 0 seconds" do
|
121
|
+
@db.cache_time = 0
|
122
|
+
@coll = @db.collection('collection-unit-test')
|
123
|
+
@coll.expects(:generate_indexes).twice
|
124
|
+
|
125
|
+
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
126
|
+
@coll.ensure_index [["x", Mongo::DESCENDING]]
|
127
|
+
end
|
128
|
+
|
129
|
+
should "call generate_indexes for each key when calling ensure_indexes" do
|
130
|
+
@db.cache_time = 300
|
131
|
+
@coll = @db.collection('collection-unit-test')
|
132
|
+
@coll.expects(:generate_indexes).once.with do |a, b, c|
|
133
|
+
a == {"x"=>-1, "y"=>-1}
|
134
|
+
end
|
135
|
+
|
136
|
+
@coll.ensure_index [["x", Mongo::DESCENDING], ["y", Mongo::DESCENDING]]
|
137
|
+
end
|
138
|
+
|
139
|
+
should "call generate_indexes for each key when calling ensure_indexes with a hash" do
|
140
|
+
@db.cache_time = 300
|
141
|
+
@coll = @db.collection('collection-unit-test')
|
142
|
+
oh = BSON::OrderedHash.new
|
143
|
+
oh['x'] = Mongo::DESCENDING
|
144
|
+
oh['y'] = Mongo::DESCENDING
|
145
|
+
@coll.expects(:generate_indexes).once.with do |a, b, c|
|
146
|
+
a == oh
|
147
|
+
end
|
148
|
+
|
149
|
+
if RUBY_VERSION > '1.9'
|
150
|
+
@coll.ensure_index({"x" => Mongo::DESCENDING, "y" => Mongo::DESCENDING})
|
151
|
+
else
|
152
|
+
ordered_hash = BSON::OrderedHash.new
|
153
|
+
ordered_hash['x'] = Mongo::DESCENDING
|
154
|
+
ordered_hash['y'] = Mongo::DESCENDING
|
155
|
+
@coll.ensure_index(ordered_hash)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
should "use the connection's logger" do
|
160
|
+
@logger.expects(:warn).with do |msg|
|
161
|
+
msg == "MONGODB [WARNING] test warning"
|
162
|
+
end
|
163
|
+
@coll.log(:warn, "test warning")
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,325 @@
|
|
1
|
+
# Copyright (C) 2009-2013 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'test_helper'
|
16
|
+
|
17
|
+
class ConnectionUnitTest < Test::Unit::TestCase
|
18
|
+
context "Mongo::MongoClient initialization " do
|
19
|
+
context "given a single node" do
|
20
|
+
setup do
|
21
|
+
@connection = Mongo::Connection.new('localhost', 27017, :safe => true, :connect => false)
|
22
|
+
TCPSocket.stubs(:new).returns(new_mock_socket)
|
23
|
+
|
24
|
+
admin_db = new_mock_db
|
25
|
+
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
26
|
+
@connection.expects(:[]).with('admin').returns(admin_db)
|
27
|
+
@connection.connect
|
28
|
+
end
|
29
|
+
|
30
|
+
should "set safe mode true" do
|
31
|
+
assert_equal true, @connection.safe
|
32
|
+
end
|
33
|
+
|
34
|
+
should "set localhost and port to master" do
|
35
|
+
assert_equal 'localhost', @connection.primary_pool.host
|
36
|
+
assert_equal 27017, @connection.primary_pool.port
|
37
|
+
end
|
38
|
+
|
39
|
+
should "set connection pool to 1" do
|
40
|
+
assert_equal 1, @connection.primary_pool.size
|
41
|
+
end
|
42
|
+
|
43
|
+
should "default slave_ok to false" do
|
44
|
+
assert !@connection.slave_ok?
|
45
|
+
end
|
46
|
+
|
47
|
+
should "not raise error if no host or port is supplied" do
|
48
|
+
assert_nothing_raised do
|
49
|
+
Mongo::Connection.new(:safe => true, :connect => false)
|
50
|
+
end
|
51
|
+
assert_nothing_raised do
|
52
|
+
Mongo::Connection.new('localhost', :safe => true, :connect => false)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
should "warn if invalid options are specified" do
|
57
|
+
connection = Mongo::Connection.allocate
|
58
|
+
opts = {:connect => false}
|
59
|
+
|
60
|
+
Mongo::ReplSetConnection::REPL_SET_OPTS.each do |opt|
|
61
|
+
connection.expects(:warn).with("#{opt} is not a valid option for #{connection.class}")
|
62
|
+
opts[opt] = true
|
63
|
+
end
|
64
|
+
|
65
|
+
args = ['localhost', 27017, opts]
|
66
|
+
connection.send(:initialize, *args)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
context "initializing with a unix socket" do
|
72
|
+
setup do
|
73
|
+
@connection = Mongo::Connection.new('/tmp/mongod.sock', :safe => true, :connect => false)
|
74
|
+
UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
|
75
|
+
end
|
76
|
+
should "parse a unix socket" do
|
77
|
+
assert_equal "/tmp/mongod.sock", @connection.host_port.first
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "initializing with a mongodb uri" do
|
82
|
+
should "parse a simple uri" do
|
83
|
+
@connection = Mongo::Connection.from_uri("mongodb://localhost", :connect => false)
|
84
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
85
|
+
end
|
86
|
+
|
87
|
+
should "set auth source" do
|
88
|
+
@connection = Mongo::Connection.from_uri("mongodb://user:pass@localhost?authSource=foo", :connect => false)
|
89
|
+
assert_equal 'foo', @connection.auths.first[:source]
|
90
|
+
end
|
91
|
+
|
92
|
+
should "set auth mechanism" do
|
93
|
+
@connection = Mongo::Connection.from_uri("mongodb://user@localhost?authMechanism=MONGODB-X509", :connect => false)
|
94
|
+
assert_equal 'MONGODB-X509', @connection.auths.first[:mechanism]
|
95
|
+
|
96
|
+
assert_raise MongoArgumentError do
|
97
|
+
Mongo::Connection.from_uri("mongodb://localhost?authMechanism=INVALID", :connect => false)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
should "allow a complex host names" do
|
102
|
+
host_name = "foo.bar-12345.org"
|
103
|
+
@connection = Mongo::Connection.from_uri("mongodb://#{host_name}", :connect => false)
|
104
|
+
assert_equal [host_name, 27017], @connection.host_port
|
105
|
+
end
|
106
|
+
|
107
|
+
should "allow db without username and password" do
|
108
|
+
host_name = "foo.bar-12345.org"
|
109
|
+
@connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo", :connect => false)
|
110
|
+
assert_equal [host_name, 27017], @connection.host_port
|
111
|
+
end
|
112
|
+
|
113
|
+
should "set safe options on connection" do
|
114
|
+
host_name = "localhost"
|
115
|
+
opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true"
|
116
|
+
@connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
|
117
|
+
assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @connection.write_concern)
|
118
|
+
end
|
119
|
+
|
120
|
+
should "set timeout options on connection" do
|
121
|
+
host_name = "localhost"
|
122
|
+
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
|
123
|
+
@connection = Mongo::Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
|
124
|
+
assert_equal 1, @connection.connect_timeout
|
125
|
+
assert_equal 5, @connection.op_timeout
|
126
|
+
end
|
127
|
+
|
128
|
+
should "parse a uri with a hyphen & underscore in the username or password" do
|
129
|
+
@connection = Mongo::Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
|
130
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
131
|
+
|
132
|
+
auth_hash = {
|
133
|
+
:db_name => 'db',
|
134
|
+
:username => 'hyphen-user_name',
|
135
|
+
:password => 'p-s_s',
|
136
|
+
:source => 'db',
|
137
|
+
:mechanism => Authentication::DEFAULT_MECHANISM,
|
138
|
+
:extra => {}
|
139
|
+
}
|
140
|
+
assert_equal auth_hash, @connection.auths.first
|
141
|
+
end
|
142
|
+
|
143
|
+
should "attempt to connect" do
|
144
|
+
TCPSocket.stubs(:new).returns(new_mock_socket)
|
145
|
+
@connection = Mongo::Connection.from_uri("mongodb://localhost", :connect => false)
|
146
|
+
|
147
|
+
admin_db = new_mock_db
|
148
|
+
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
149
|
+
@connection.expects(:[]).with('admin').returns(admin_db)
|
150
|
+
@connection.connect
|
151
|
+
end
|
152
|
+
|
153
|
+
should "raise an error on invalid uris" do
|
154
|
+
assert_raise MongoArgumentError do
|
155
|
+
Mongo::Connection.from_uri("mongo://localhost", :connect => false)
|
156
|
+
end
|
157
|
+
|
158
|
+
assert_raise MongoArgumentError do
|
159
|
+
Mongo::Connection.from_uri("mongodb://localhost:abc", :connect => false)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
should "require password if using legacy auth and username present" do
|
164
|
+
assert Mongo::Connection.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
|
165
|
+
|
166
|
+
assert_raise MongoArgumentError do
|
167
|
+
Mongo::Connection.from_uri("mongodb://kyle:@localhost", :connect => false)
|
168
|
+
end
|
169
|
+
|
170
|
+
assert_raise MongoArgumentError do
|
171
|
+
Mongo::Connection.from_uri("mongodb://kyle@localhost", :connect => false)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
context "initializing with ENV['MONGODB_URI']" do
|
177
|
+
should "parse a simple uri" do
|
178
|
+
uri = "mongodb://localhost?connect=false"
|
179
|
+
with_preserved_env_uri(uri) do
|
180
|
+
@connection = Mongo::Connection.new
|
181
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
should "set auth source" do
|
186
|
+
uri = "mongodb://user:pass@localhost?authSource=foo&connect=false"
|
187
|
+
with_preserved_env_uri(uri) do
|
188
|
+
@connection = Mongo::Connection.new
|
189
|
+
assert_equal 'foo', @connection.auths.first[:source]
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
should "set auth mechanism" do
|
194
|
+
uri = "mongodb://user@localhost?authMechanism=MONGODB-X509&connect=false"
|
195
|
+
with_preserved_env_uri(uri) do
|
196
|
+
@connection = Mongo::Connection.new
|
197
|
+
assert_equal 'MONGODB-X509', @connection.auths.first[:mechanism]
|
198
|
+
|
199
|
+
ENV['MONGODB_URI'] = "mongodb://user@localhost?authMechanism=INVALID&connect=false"
|
200
|
+
assert_raise MongoArgumentError do
|
201
|
+
Mongo::Connection.new
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
should "allow a complex host names" do
|
207
|
+
host_name = "foo.bar-12345.org"
|
208
|
+
uri = "mongodb://#{host_name}?connect=false"
|
209
|
+
with_preserved_env_uri(uri) do
|
210
|
+
@connection = Mongo::Connection.new
|
211
|
+
assert_equal [host_name, 27017], @connection.host_port
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
should "allow db without username and password" do
|
216
|
+
host_name = "foo.bar-12345.org"
|
217
|
+
uri = "mongodb://#{host_name}/foo?connect=false"
|
218
|
+
with_preserved_env_uri(uri) do
|
219
|
+
@connection = Mongo::Connection.new
|
220
|
+
assert_equal [host_name, 27017], @connection.host_port
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
should "set safe options on connection" do
|
225
|
+
host_name = "localhost"
|
226
|
+
opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
|
227
|
+
uri = "mongodb://#{host_name}/foo?#{opts}"
|
228
|
+
with_preserved_env_uri(uri) do
|
229
|
+
@connection = Mongo::Connection.new
|
230
|
+
assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @connection.safe)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
should "set timeout options on connection" do
|
235
|
+
host_name = "localhost"
|
236
|
+
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
|
237
|
+
uri = "mongodb://#{host_name}/foo?#{opts}"
|
238
|
+
with_preserved_env_uri(uri) do
|
239
|
+
@connection = Mongo::Connection.new
|
240
|
+
assert_equal 1, @connection.connect_timeout
|
241
|
+
assert_equal 5, @connection.op_timeout
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
should "parse a uri with a hyphen & underscore in the username or password" do
|
246
|
+
uri = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
|
247
|
+
with_preserved_env_uri(uri) do
|
248
|
+
@connection = Mongo::Connection.new
|
249
|
+
assert_equal ['localhost', 27017], @connection.host_port
|
250
|
+
|
251
|
+
auth_hash = {
|
252
|
+
:db_name => 'db',
|
253
|
+
:username => 'hyphen-user_name',
|
254
|
+
:password => 'p-s_s',
|
255
|
+
:source => 'db',
|
256
|
+
:mechanism => Authentication::DEFAULT_MECHANISM,
|
257
|
+
:extra => {}
|
258
|
+
}
|
259
|
+
assert_equal auth_hash, @connection.auths.first
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
should "attempt to connect" do
|
264
|
+
TCPSocket.stubs(:new).returns(new_mock_socket)
|
265
|
+
uri = "mongodb://localhost?connect=false"
|
266
|
+
with_preserved_env_uri(uri) do
|
267
|
+
@connection = Mongo::Connection.new
|
268
|
+
|
269
|
+
admin_db = new_mock_db
|
270
|
+
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
271
|
+
@connection.expects(:[]).with('admin').returns(admin_db)
|
272
|
+
@connection.connect
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
should "raise an error on invalid uris" do
|
277
|
+
uri = "mongo://localhost"
|
278
|
+
with_preserved_env_uri(uri) do
|
279
|
+
assert_raise MongoArgumentError do
|
280
|
+
Mongo::Connection.new
|
281
|
+
end
|
282
|
+
|
283
|
+
ENV['MONGODB_URI'] = "mongodb://localhost:abc"
|
284
|
+
assert_raise MongoArgumentError do
|
285
|
+
Mongo::Connection.new
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
should "require password if using legacy auth and username present" do
|
291
|
+
uri = "mongodb://kyle:jones@localhost/db?connect=false"
|
292
|
+
with_preserved_env_uri(uri) do
|
293
|
+
assert Mongo::Connection.new
|
294
|
+
|
295
|
+
ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false"
|
296
|
+
assert_raise MongoArgumentError do
|
297
|
+
Mongo::Connection.new
|
298
|
+
end
|
299
|
+
|
300
|
+
ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false"
|
301
|
+
assert_raise MongoArgumentError do
|
302
|
+
Mongo::Connection.new
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
should "require password if using PLAIN auth and username present" do
|
308
|
+
uri = "mongodb://kyle:jones@localhost/db?connect=false&authMechanism=PLAIN"
|
309
|
+
with_preserved_env_uri(uri) do
|
310
|
+
assert Mongo::Connection.new
|
311
|
+
|
312
|
+
ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false&authMechanism=PLAIN"
|
313
|
+
assert_raise MongoArgumentError do
|
314
|
+
Mongo::Connection.new
|
315
|
+
end
|
316
|
+
|
317
|
+
ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false&authMechanism=PLAIN"
|
318
|
+
assert_raise MongoArgumentError do
|
319
|
+
Mongo::Connection.new
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|