mongo 1.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/LICENSE +190 -0
  5. data/README.md +149 -0
  6. data/Rakefile +31 -0
  7. data/VERSION +1 -0
  8. data/bin/mongo_console +43 -0
  9. data/ext/jsasl/target/jsasl.jar +0 -0
  10. data/lib/mongo.rb +90 -0
  11. data/lib/mongo/bulk_write_collection_view.rb +380 -0
  12. data/lib/mongo/collection.rb +1164 -0
  13. data/lib/mongo/collection_writer.rb +364 -0
  14. data/lib/mongo/connection.rb +19 -0
  15. data/lib/mongo/connection/node.rb +239 -0
  16. data/lib/mongo/connection/pool.rb +347 -0
  17. data/lib/mongo/connection/pool_manager.rb +325 -0
  18. data/lib/mongo/connection/sharding_pool_manager.rb +67 -0
  19. data/lib/mongo/connection/socket.rb +18 -0
  20. data/lib/mongo/connection/socket/socket_util.rb +37 -0
  21. data/lib/mongo/connection/socket/ssl_socket.rb +95 -0
  22. data/lib/mongo/connection/socket/tcp_socket.rb +86 -0
  23. data/lib/mongo/connection/socket/unix_socket.rb +39 -0
  24. data/lib/mongo/cursor.rb +719 -0
  25. data/lib/mongo/db.rb +735 -0
  26. data/lib/mongo/exception.rb +88 -0
  27. data/lib/mongo/functional.rb +21 -0
  28. data/lib/mongo/functional/authentication.rb +318 -0
  29. data/lib/mongo/functional/logging.rb +85 -0
  30. data/lib/mongo/functional/read_preference.rb +174 -0
  31. data/lib/mongo/functional/sasl_java.rb +48 -0
  32. data/lib/mongo/functional/uri_parser.rb +374 -0
  33. data/lib/mongo/functional/write_concern.rb +66 -0
  34. data/lib/mongo/gridfs.rb +18 -0
  35. data/lib/mongo/gridfs/grid.rb +112 -0
  36. data/lib/mongo/gridfs/grid_ext.rb +53 -0
  37. data/lib/mongo/gridfs/grid_file_system.rb +163 -0
  38. data/lib/mongo/gridfs/grid_io.rb +484 -0
  39. data/lib/mongo/legacy.rb +140 -0
  40. data/lib/mongo/mongo_client.rb +702 -0
  41. data/lib/mongo/mongo_replica_set_client.rb +523 -0
  42. data/lib/mongo/mongo_sharded_client.rb +159 -0
  43. data/lib/mongo/networking.rb +370 -0
  44. data/lib/mongo/utils.rb +19 -0
  45. data/lib/mongo/utils/conversions.rb +110 -0
  46. data/lib/mongo/utils/core_ext.rb +70 -0
  47. data/lib/mongo/utils/server_version.rb +69 -0
  48. data/lib/mongo/utils/support.rb +80 -0
  49. data/lib/mongo/utils/thread_local_variable_manager.rb +25 -0
  50. data/mongo.gemspec +36 -0
  51. data/test/functional/authentication_test.rb +35 -0
  52. data/test/functional/bulk_api_stress_test.rb +133 -0
  53. data/test/functional/bulk_write_collection_view_test.rb +1129 -0
  54. data/test/functional/client_test.rb +565 -0
  55. data/test/functional/collection_test.rb +2073 -0
  56. data/test/functional/collection_writer_test.rb +83 -0
  57. data/test/functional/conversions_test.rb +163 -0
  58. data/test/functional/cursor_fail_test.rb +63 -0
  59. data/test/functional/cursor_message_test.rb +57 -0
  60. data/test/functional/cursor_test.rb +625 -0
  61. data/test/functional/db_api_test.rb +819 -0
  62. data/test/functional/db_connection_test.rb +27 -0
  63. data/test/functional/db_test.rb +344 -0
  64. data/test/functional/grid_file_system_test.rb +285 -0
  65. data/test/functional/grid_io_test.rb +252 -0
  66. data/test/functional/grid_test.rb +273 -0
  67. data/test/functional/pool_test.rb +62 -0
  68. data/test/functional/safe_test.rb +98 -0
  69. data/test/functional/ssl_test.rb +29 -0
  70. data/test/functional/support_test.rb +62 -0
  71. data/test/functional/timeout_test.rb +58 -0
  72. data/test/functional/uri_test.rb +330 -0
  73. data/test/functional/write_concern_test.rb +118 -0
  74. data/test/helpers/general.rb +50 -0
  75. data/test/helpers/test_unit.rb +317 -0
  76. data/test/replica_set/authentication_test.rb +35 -0
  77. data/test/replica_set/basic_test.rb +174 -0
  78. data/test/replica_set/client_test.rb +341 -0
  79. data/test/replica_set/complex_connect_test.rb +77 -0
  80. data/test/replica_set/connection_test.rb +138 -0
  81. data/test/replica_set/count_test.rb +64 -0
  82. data/test/replica_set/cursor_test.rb +212 -0
  83. data/test/replica_set/insert_test.rb +140 -0
  84. data/test/replica_set/max_values_test.rb +145 -0
  85. data/test/replica_set/pinning_test.rb +55 -0
  86. data/test/replica_set/query_test.rb +73 -0
  87. data/test/replica_set/read_preference_test.rb +214 -0
  88. data/test/replica_set/refresh_test.rb +175 -0
  89. data/test/replica_set/replication_ack_test.rb +94 -0
  90. data/test/replica_set/ssl_test.rb +32 -0
  91. data/test/sharded_cluster/basic_test.rb +197 -0
  92. data/test/shared/authentication/basic_auth_shared.rb +286 -0
  93. data/test/shared/authentication/bulk_api_auth_shared.rb +259 -0
  94. data/test/shared/authentication/gssapi_shared.rb +164 -0
  95. data/test/shared/authentication/sasl_plain_shared.rb +96 -0
  96. data/test/shared/ssl_shared.rb +235 -0
  97. data/test/test_helper.rb +56 -0
  98. data/test/threading/basic_test.rb +120 -0
  99. data/test/tools/mongo_config.rb +608 -0
  100. data/test/tools/mongo_config_test.rb +160 -0
  101. data/test/unit/client_test.rb +347 -0
  102. data/test/unit/collection_test.rb +166 -0
  103. data/test/unit/connection_test.rb +325 -0
  104. data/test/unit/cursor_test.rb +299 -0
  105. data/test/unit/db_test.rb +136 -0
  106. data/test/unit/grid_test.rb +76 -0
  107. data/test/unit/mongo_sharded_client_test.rb +48 -0
  108. data/test/unit/node_test.rb +93 -0
  109. data/test/unit/pool_manager_test.rb +142 -0
  110. data/test/unit/read_pref_test.rb +115 -0
  111. data/test/unit/read_test.rb +159 -0
  112. data/test/unit/safe_test.rb +158 -0
  113. data/test/unit/sharding_pool_manager_test.rb +84 -0
  114. data/test/unit/write_concern_test.rb +175 -0
  115. metadata +260 -0
  116. metadata.gz.sig +0 -0
@@ -0,0 +1,160 @@
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 MongoConfig < Test::Unit::TestCase
18
+
19
+ def startup
20
+ @sys_proc = nil
21
+ end
22
+
23
+ def shutdown
24
+ @sys_proc.stop if @sys_proc && @sys_proc.running?
25
+ end
26
+
27
+ test "config defaults" do
28
+ [ Mongo::Config::DEFAULT_BASE_OPTS,
29
+ Mongo::Config::DEFAULT_REPLICA_SET,
30
+ Mongo::Config::DEFAULT_SHARDED_SIMPLE,
31
+ Mongo::Config::DEFAULT_SHARDED_REPLICA
32
+ ].each do |params|
33
+ config = Mongo::Config.cluster(params)
34
+ assert(config.size > 0)
35
+ end
36
+ end
37
+
38
+ test "get available port" do
39
+ assert_not_nil(Mongo::Config.get_available_port)
40
+ end
41
+
42
+ test "SysProc start" do
43
+ cmd = "true"
44
+ @sys_proc = Mongo::Config::SysProc.new(cmd)
45
+ assert_equal(cmd, @sys_proc.cmd)
46
+ assert_nil(@sys_proc.pid)
47
+ start_and_assert_running?(@sys_proc)
48
+ end
49
+
50
+ test "SysProc wait" do
51
+ @sys_proc = Mongo::Config::SysProc.new("true")
52
+ start_and_assert_running?(@sys_proc)
53
+ assert(@sys_proc.running?)
54
+ @sys_proc.wait
55
+ assert(!@sys_proc.running?)
56
+ end
57
+
58
+ test "SysProc kill" do
59
+ @sys_proc = Mongo::Config::SysProc.new("true")
60
+ start_and_assert_running?(@sys_proc)
61
+ @sys_proc.kill
62
+ @sys_proc.wait
63
+ assert(!@sys_proc.running?)
64
+ end
65
+
66
+ test "SysProc stop" do
67
+ @sys_proc = Mongo::Config::SysProc.new("true")
68
+ start_and_assert_running?(@sys_proc)
69
+ @sys_proc.stop
70
+ assert(!@sys_proc.running?)
71
+ end
72
+
73
+ test "SysProc zombie respawn" do
74
+ @sys_proc = Mongo::Config::SysProc.new("true")
75
+ start_and_assert_running?(@sys_proc)
76
+ prev_pid = @sys_proc.pid
77
+ @sys_proc.kill
78
+ # don't wait, leaving a zombie
79
+ assert(@sys_proc.running?)
80
+ start_and_assert_running?(@sys_proc)
81
+ assert(prev_pid && @sys_proc.pid && prev_pid != @sys_proc.pid, 'SysProc#start should spawn a new process after a zombie')
82
+ @sys_proc.stop
83
+ assert(!@sys_proc.running?)
84
+ end
85
+
86
+ test "Server" do
87
+ server = Mongo::Config::Server.new('a cmd', 'host', 1234)
88
+ assert_equal('a cmd', server.cmd)
89
+ assert_equal('host', server.host)
90
+ assert_equal(1234, server.port)
91
+ end
92
+
93
+ test "DbServer" do
94
+ config = Mongo::Config::DEFAULT_BASE_OPTS
95
+ server = Mongo::Config::DbServer.new(config)
96
+ assert_equal(config, server.config)
97
+ assert_equal("mongod --dbpath data --logpath data/log", server.cmd)
98
+ assert_equal(config[:host], server.host)
99
+ assert_equal(config[:port], server.port)
100
+ end
101
+
102
+ def cluster_test(opts)
103
+ #debug 1, opts.inspect
104
+ config = Mongo::Config.cluster(opts)
105
+ #debug 1, config.inspect
106
+ manager = Mongo::Config::ClusterManager.new(config)
107
+ assert_equal(config, manager.config)
108
+ manager.start
109
+ yield manager
110
+ manager.stop
111
+ manager.servers.each{|s| assert(!s.running?)}
112
+ manager.clobber
113
+ end
114
+
115
+ test "cluster manager base" do
116
+ cluster_test(Mongo::Config::DEFAULT_BASE_OPTS) do |manager|
117
+
118
+ end
119
+ end
120
+
121
+ test "cluster manager replica set" do
122
+ cluster_test(Mongo::Config::DEFAULT_REPLICA_SET) do |manager|
123
+ servers = manager.servers
124
+ servers.each do |server|
125
+ assert_not_nil(Mongo::MongoClient.new(server.host, server.port))
126
+ assert_match(/oplogSize/, server.cmd, '--oplogSize option should be specified')
127
+ assert_match(/smallfiles/, server.cmd, '--smallfiles option should be specified')
128
+ assert_no_match(/nojournal/, server.cmd, '--nojournal option should not be specified')
129
+ assert_match(/noprealloc/, server.cmd, '--noprealloc option should be specified')
130
+ end
131
+ end
132
+ end
133
+
134
+ test "cluster manager sharded simple" do
135
+ cluster_test(Mongo::Config::DEFAULT_SHARDED_SIMPLE) do |manager|
136
+ servers = manager.shards + manager.configs
137
+ servers.each do |server|
138
+ assert_not_nil(Mongo::MongoClient.new(server.host, server.port))
139
+ assert_match(/oplogSize/, server.cmd, '--oplogSize option should be specified')
140
+ assert_match(/smallfiles/, server.cmd, '--smallfiles option should be specified')
141
+ assert_no_match(/nojournal/, server.cmd, '--nojournal option should not be specified')
142
+ assert_match(/noprealloc/, server.cmd, '--noprealloc option should be specified')
143
+ end
144
+ end
145
+ end
146
+
147
+ test "cluster manager sharded replica" do
148
+ #cluster_test(Mongo::Config::DEFAULT_SHARDED_REPLICA) # not yet supported by ClusterManager
149
+ end
150
+
151
+ private
152
+
153
+ def start_and_assert_running?(sys_proc)
154
+ assert_not_nil(sys_proc.start(0))
155
+ assert_not_nil(sys_proc.pid)
156
+ assert(sys_proc.running?)
157
+ end
158
+
159
+ end
160
+
@@ -0,0 +1,347 @@
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
+ include Mongo
17
+
18
+ class ClientUnitTest < Test::Unit::TestCase
19
+ context "Mongo::MongoClient initialization " do
20
+ context "given a single node" do
21
+ setup do
22
+ @client = MongoClient.new('localhost', 27017, :connect => false)
23
+ TCPSocket.stubs(:new).returns(new_mock_socket)
24
+
25
+ admin_db = new_mock_db
26
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
27
+ @client.expects(:[]).with('admin').returns(admin_db)
28
+ @client.connect
29
+ end
30
+
31
+ should "gle writes by default" do
32
+ assert_equal 1, @client.write_concern[:w]
33
+ end
34
+
35
+ should "set localhost and port to master" do
36
+ assert_equal 'localhost', @client.primary_pool.host
37
+ assert_equal 27017, @client.primary_pool.port
38
+ end
39
+
40
+ should "set connection pool to 1" do
41
+ assert_equal 1, @client.primary_pool.size
42
+ end
43
+
44
+ should "default slave_ok to false" do
45
+ assert !@client.slave_ok?
46
+ end
47
+
48
+ should "not raise error if no host or port is supplied" do
49
+ assert_nothing_raised do
50
+ MongoClient.new(:w => 1, :connect => false)
51
+ end
52
+ assert_nothing_raised do
53
+ MongoClient.new('localhost', :w => 1, :connect=> false)
54
+ end
55
+ end
56
+
57
+ should "warn if invalid options are specified" do
58
+ client = MongoClient.allocate
59
+ opts = {:connect => false}
60
+
61
+ MongoReplicaSetClient::REPL_SET_OPTS.each do |opt|
62
+ client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
63
+ opts[opt] = true
64
+ end
65
+
66
+ args = ['localhost', 27017, opts]
67
+ client.send(:initialize, *args)
68
+ end
69
+
70
+ context "given a replica set" do
71
+
72
+ should "warn if invalid options are specified" do
73
+ client = MongoReplicaSetClient.allocate
74
+ opts = {:connect => false}
75
+
76
+ MongoClient::CLIENT_ONLY_OPTS.each do |opt|
77
+ client.expects(:warn).with("#{opt} is not a valid option for #{client.class}")
78
+ opts[opt] = true
79
+ end
80
+
81
+ args = [['localhost:27017'], opts]
82
+ client.send(:initialize, *args)
83
+ end
84
+
85
+ should "throw error if superflous arguments are specified" do
86
+ assert_raise MongoArgumentError do
87
+ MongoReplicaSetClient.new(['localhost:27017'], ['localhost:27018'], {:connect => false})
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ context "initializing with a unix socket" do
94
+ setup do
95
+ @connection = Mongo::Connection.new('/tmp/mongod.sock', :connect => false)
96
+ UNIXSocket.stubs(:new).returns(new_mock_unix_socket)
97
+ end
98
+ should "parse a unix socket" do
99
+ assert_equal "/tmp/mongod.sock", @connection.host_port.first
100
+ end
101
+ end
102
+
103
+ context "initializing with a mongodb uri" do
104
+ should "parse a simple uri" do
105
+ @client = MongoClient.from_uri("mongodb://localhost", :connect => false)
106
+ assert_equal ['localhost', 27017], @client.host_port
107
+ end
108
+
109
+ should "set auth source" do
110
+ @client = MongoClient.from_uri("mongodb://user:pass@localhost?authSource=foo", :connect => false)
111
+ assert_equal 'foo', @client.auths.first[:source]
112
+ end
113
+
114
+ should "set auth mechanism" do
115
+ @client = MongoClient.from_uri("mongodb://user@localhost?authMechanism=MONGODB-X509", :connect => false)
116
+ assert_equal 'MONGODB-X509', @client.auths.first[:mechanism]
117
+
118
+ assert_raise MongoArgumentError do
119
+ MongoClient.from_uri("mongodb://user@localhost?authMechanism=INVALID", :connect => false)
120
+ end
121
+ end
122
+
123
+ should "allow a complex host names" do
124
+ host_name = "foo.bar-12345.org"
125
+ @client = MongoClient.from_uri("mongodb://#{host_name}", :connect => false)
126
+ assert_equal [host_name, 27017], @client.host_port
127
+ end
128
+
129
+ should "allow db without username and password" do
130
+ host_name = "foo.bar-12345.org"
131
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo", :connect => false)
132
+ assert_equal [host_name, 27017], @client.host_port
133
+ end
134
+
135
+ should "set write concern options on connection" do
136
+ host_name = "localhost"
137
+ opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true"
138
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
139
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
140
+ end
141
+
142
+ should "set timeout options on connection" do
143
+ host_name = "localhost"
144
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
145
+ @client = MongoClient.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
146
+ assert_equal 1, @client.connect_timeout
147
+ assert_equal 5, @client.op_timeout
148
+ end
149
+
150
+ should "parse a uri with a hyphen & underscore in the username or password" do
151
+ @client = MongoClient.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
152
+ assert_equal ['localhost', 27017], @client.host_port
153
+
154
+ auth_hash = {
155
+ :db_name => 'db',
156
+ :username => 'hyphen-user_name',
157
+ :password => 'p-s_s',
158
+ :source => 'db',
159
+ :mechanism => Authentication::DEFAULT_MECHANISM,
160
+ :extra => {}
161
+ }
162
+ assert_equal auth_hash, @client.auths.first
163
+ end
164
+
165
+ should "attempt to connect" do
166
+ TCPSocket.stubs(:new).returns(new_mock_socket)
167
+ @client = MongoClient.from_uri("mongodb://localhost", :connect => false)
168
+
169
+ admin_db = new_mock_db
170
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
171
+ @client.expects(:[]).with('admin').returns(admin_db)
172
+ @client.connect
173
+ end
174
+
175
+ should "raise an error on invalid uris" do
176
+ assert_raise MongoArgumentError do
177
+ MongoClient.from_uri("mongo://localhost", :connect => false)
178
+ end
179
+
180
+ assert_raise MongoArgumentError do
181
+ MongoClient.from_uri("mongodb://localhost:abc", :connect => false)
182
+ end
183
+ end
184
+
185
+ should "require password if using legacy auth and username present" do
186
+ assert MongoClient.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
187
+
188
+ assert_raise MongoArgumentError do
189
+ MongoClient.from_uri("mongodb://kyle:@localhost", :connect => false)
190
+ end
191
+
192
+ assert_raise MongoArgumentError do
193
+ MongoClient.from_uri("mongodb://kyle@localhost", :connect => false)
194
+ end
195
+ end
196
+ end
197
+
198
+ context "initializing with ENV['MONGODB_URI']" do
199
+ should "parse a simple uri" do
200
+ uri = "mongodb://localhost?connect=false"
201
+ with_preserved_env_uri(uri) do
202
+ @client = MongoClient.new
203
+ assert_equal ['localhost', 27017], @client.host_port
204
+ end
205
+ end
206
+
207
+ should "set auth source" do
208
+ uri = "mongodb://user:pass@localhost?authSource=foo&connect=false"
209
+ with_preserved_env_uri(uri) do
210
+ @client = MongoClient.new
211
+ assert_equal 'foo', @client.auths.first[:source]
212
+ end
213
+ end
214
+
215
+ should "set auth mechanism" do
216
+ uri = "mongodb://user@localhost?authMechanism=MONGODB-X509&connect=false"
217
+ with_preserved_env_uri(uri) do
218
+ @client = MongoClient.new
219
+ assert_equal 'MONGODB-X509', @client.auths.first[:mechanism]
220
+
221
+ ENV['MONGODB_URI'] = "mongodb://user@localhost?authMechanism=INVALID&connect=false"
222
+ assert_raise MongoArgumentError do
223
+ MongoClient.new
224
+ end
225
+ end
226
+ end
227
+
228
+ should "allow a complex host names" do
229
+ host_name = "foo.bar-12345.org"
230
+ uri = "mongodb://#{host_name}?connect=false"
231
+ with_preserved_env_uri(uri) do
232
+ @client = MongoClient.new
233
+ assert_equal [host_name, 27017], @client.host_port
234
+ end
235
+ end
236
+
237
+ should "allow db without username and password" do
238
+ host_name = "foo.bar-12345.org"
239
+ uri = "mongodb://#{host_name}/foo?connect=false"
240
+ with_preserved_env_uri(uri) do
241
+ @client = MongoClient.new
242
+ assert_equal [host_name, 27017], @client.host_port
243
+ end
244
+ end
245
+
246
+ should "set write concern options on connection" do
247
+ host_name = "localhost"
248
+ opts = "w=2&wtimeoutMS=1000&fsync=true&journal=true&connect=false"
249
+ uri = "mongodb://#{host_name}/foo?#{opts}"
250
+ with_preserved_env_uri(uri) do
251
+ @client = MongoClient.new
252
+ assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @client.write_concern)
253
+ end
254
+ end
255
+
256
+ should "set timeout options on connection" do
257
+ host_name = "localhost"
258
+ opts = "connectTimeoutMS=1000&socketTimeoutMS=5000&connect=false"
259
+ uri = "mongodb://#{host_name}/foo?#{opts}"
260
+ with_preserved_env_uri(uri) do
261
+ @client = MongoClient.new
262
+ assert_equal 1, @client.connect_timeout
263
+ assert_equal 5, @client.op_timeout
264
+ end
265
+ end
266
+
267
+ should "parse a uri with a hyphen & underscore in the username or password" do
268
+ uri = "mongodb://hyphen-user_name:p-s_s@localhost:27017/db?connect=false"
269
+ with_preserved_env_uri(uri) do
270
+ @client = MongoClient.new
271
+ assert_equal ['localhost', 27017], @client.host_port
272
+
273
+ auth_hash = {
274
+ :db_name => 'db',
275
+ :username => 'hyphen-user_name',
276
+ :password => 'p-s_s',
277
+ :source => 'db',
278
+ :mechanism => Authentication::DEFAULT_MECHANISM,
279
+ :extra => {}
280
+ }
281
+ assert_equal auth_hash, @client.auths.first
282
+ end
283
+ end
284
+
285
+ should "attempt to connect" do
286
+ TCPSocket.stubs(:new).returns(new_mock_socket)
287
+ uri = "mongodb://localhost?connect=false"
288
+ with_preserved_env_uri(uri) do
289
+ @client = MongoClient.new
290
+
291
+ admin_db = new_mock_db
292
+ admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
293
+ @client.expects(:[]).with('admin').returns(admin_db)
294
+ @client.connect
295
+ end
296
+ end
297
+
298
+ should "raise an error on invalid uris" do
299
+ uri = "mongo://localhost"
300
+ with_preserved_env_uri(uri) do
301
+ assert_raise MongoArgumentError do
302
+ MongoClient.new
303
+ end
304
+
305
+ ENV['MONGODB_URI'] = "mongodb://localhost:abc?connect=false"
306
+ assert_raise MongoArgumentError do
307
+ MongoClient.new
308
+ end
309
+ end
310
+ end
311
+
312
+ should "require password if using legacy auth and username present" do
313
+ uri = "mongodb://kyle:jones@localhost?connect=false"
314
+ with_preserved_env_uri(uri) do
315
+ assert MongoClient.new
316
+
317
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false"
318
+ assert_raise MongoArgumentError do
319
+ MongoClient.new
320
+ end
321
+
322
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false"
323
+ assert_raise MongoArgumentError do
324
+ MongoClient.new
325
+ end
326
+ end
327
+ end
328
+
329
+ should "require password if using PLAIN auth and username present" do
330
+ uri = "mongodb://kyle:jones@localhost?connect=false&authMechanism=PLAIN"
331
+ with_preserved_env_uri(uri) do
332
+ assert MongoClient.new
333
+
334
+ ENV['MONGODB_URI'] = "mongodb://kyle:@localhost?connect=false&authMechanism=PLAIN"
335
+ assert_raise MongoArgumentError do
336
+ MongoClient.new
337
+ end
338
+
339
+ ENV['MONGODB_URI'] = "mongodb://kyle@localhost?connect=false&authMechanism=PLAIN"
340
+ assert_raise MongoArgumentError do
341
+ MongoClient.new
342
+ end
343
+ end
344
+ end
345
+ end
346
+ end
347
+ end