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,62 @@
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 PoolTest < Test::Unit::TestCase
18
+ include Mongo
19
+
20
+ def setup
21
+ @client ||= standard_connection({:pool_size => 15, :pool_timeout => 5})
22
+ @db = @client.db(TEST_DB)
23
+ @collection = @db.collection("pool_test")
24
+ end
25
+
26
+ def test_pool_affinity
27
+ pool = Pool.new(@client, TEST_HOST, TEST_PORT, :size => 5)
28
+
29
+ threads = []
30
+ 10.times do
31
+ threads << Thread.new do
32
+ original_socket = pool.checkout
33
+ pool.checkin(original_socket)
34
+ 500.times do
35
+ socket = pool.checkout
36
+ assert_equal original_socket, socket
37
+ pool.checkin(socket)
38
+ end
39
+ end
40
+ end
41
+
42
+ threads.each { |t| t.join }
43
+ end
44
+
45
+ def test_pool_affinity_max_size
46
+ docs = []
47
+ 8000.times {|x| docs << {:value => x}}
48
+ @collection.insert(docs)
49
+
50
+ threads = []
51
+ threads << Thread.new do
52
+ @collection.find({"value" => {"$lt" => 100}}).each {|e| e}
53
+ Thread.pass
54
+ sleep(0.125)
55
+ @collection.find({"value" => {"$gt" => 100}}).each {|e| e}
56
+ end
57
+ threads << Thread.new do
58
+ @collection.find({'$where' => "function() {for(i=0;i<1000;i++) {this.value};}"}).each {|e| e}
59
+ end
60
+ threads.each(&:join)
61
+ end
62
+ end
@@ -0,0 +1,98 @@
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 SafeTest < Test::Unit::TestCase
19
+ context "Safe mode propogation: " do
20
+ setup do
21
+ @connection = standard_connection({:safe => true}, true) # Legacy
22
+ @db = @connection[TEST_DB]
23
+ @collection = @db['test-safe']
24
+ @collection.create_index([[:a, 1]], :unique => true)
25
+ @collection.remove
26
+ end
27
+
28
+ should "propogate safe option on insert" do
29
+ @collection.insert({:a => 1})
30
+
31
+ assert_raise_error(OperationFailure, "duplicate key") do
32
+ @collection.insert({:a => 1})
33
+ end
34
+ end
35
+
36
+ should "allow safe override on insert" do
37
+ @collection.insert({:a => 1})
38
+ @collection.insert({:a => 1}, :safe => false)
39
+ end
40
+
41
+ should "allow safe override on save" do
42
+ @collection.insert({:a => 1})
43
+ id = @collection.insert({:a => 2})
44
+ assert_nothing_raised do
45
+ @collection.save({:_id => id.to_s, :a => 1}, :safe => false)
46
+ end
47
+ end
48
+
49
+ should "propogate safe option on save" do
50
+ @collection.insert({:a => 1})
51
+ id = @collection.insert({:a => 2})
52
+ assert_raise(OperationFailure) do
53
+ @collection.save({:_id => id.to_s, :a => 1})
54
+ end
55
+ end
56
+
57
+ should "propogate safe option on update" do
58
+ @collection.insert({:a => 1})
59
+ @collection.insert({:a => 2})
60
+
61
+ assert_raise_error(OperationFailure, "duplicate key") do
62
+ @collection.update({:a => 2}, {:a => 1})
63
+ end
64
+ end
65
+
66
+ should "allow safe override on update" do
67
+ @collection.insert({:a => 1})
68
+ @collection.insert({:a => 2})
69
+ @collection.update({:a => 2}, {:a => 1}, :safe => false)
70
+ end
71
+ end
72
+
73
+ context "Safe error objects" do
74
+ setup do
75
+ @connection = standard_connection({:safe => true}, true) # Legacy
76
+ @db = @connection[TEST_DB]
77
+ @collection = @db['test']
78
+ @collection.remove
79
+ @collection.insert({:a => 1})
80
+ @collection.insert({:a => 1})
81
+ @collection.insert({:a => 1})
82
+ end
83
+
84
+ should "return object on update" do
85
+ response = @collection.update({:a => 1}, {"$set" => {:a => 2}},
86
+ :multi => true)
87
+
88
+ assert(response['updatedExisting'] || @db.connection.wire_version_feature?(Mongo::MongoClient::BATCH_COMMANDS)) # TODO - review new write command return values
89
+ assert(response['n'] == 3 || @db.connection.wire_version_feature?(Mongo::MongoClient::BATCH_COMMANDS)) # TODO - update command top pending
90
+ end
91
+
92
+ should "return object on remove" do
93
+ response = @collection.remove({})
94
+ assert_equal 3, response['n']
95
+ end
96
+ end
97
+
98
+ end
@@ -0,0 +1,29 @@
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
+ require 'shared/ssl_shared'
17
+
18
+ class SSLTest < Test::Unit::TestCase
19
+ include Mongo
20
+ include SSLTests
21
+
22
+ def setup
23
+ @client_class = MongoClient
24
+ @uri_info = 'server'
25
+ @connect_info = ['server', 27017]
26
+ @bad_connect_info = ['localhost', 27017]
27
+ end
28
+
29
+ end
@@ -0,0 +1,62 @@
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 SupportTest < Test::Unit::TestCase
18
+
19
+ def test_command_response_succeeds
20
+ assert Support.ok?('ok' => 1)
21
+ assert Support.ok?('ok' => 1.0)
22
+ assert Support.ok?('ok' => true)
23
+ end
24
+
25
+ def test_command_response_fails
26
+ assert !Support.ok?('ok' => 0)
27
+ assert !Support.ok?('ok' => 0.0)
28
+ assert !Support.ok?('ok' => 0.0)
29
+ assert !Support.ok?('ok' => 'str')
30
+ assert !Support.ok?('ok' => false)
31
+ end
32
+
33
+ def test_array_of_pairs
34
+ hps = [["localhost", 27017], ["localhost", 27018], ["localhost", 27019]]
35
+ assert_equal [["localhost", 27017], ["localhost", 27018], ["localhost", 27019]], Support.normalize_seeds(hps)
36
+ end
37
+
38
+ def test_array_of_strings
39
+ hps = ["localhost:27017", "localhost:27018", "localhost:27019"]
40
+ assert_equal [["localhost", 27017], ["localhost", 27018], ["localhost", 27019]], Support.normalize_seeds(hps)
41
+ end
42
+
43
+ def test_single_string_with_host_port
44
+ hps = "localhost:27017"
45
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
46
+ end
47
+
48
+ def test_single_string_missing_port
49
+ hps = "localhost"
50
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
51
+ end
52
+
53
+ def test_single_element_array_missing_port
54
+ hps = ["localhost"]
55
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
56
+ end
57
+
58
+ def test_pair_doesnt_get_converted
59
+ hps = ["localhost", 27017]
60
+ assert_equal ["localhost", 27017], Support.normalize_seeds(hps)
61
+ end
62
+ end
@@ -0,0 +1,58 @@
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 TimeoutTest < Test::Unit::TestCase
18
+
19
+ def test_op_timeout
20
+ connection = standard_connection(:op_timeout => 0.5)
21
+
22
+ admin = connection.db('admin')
23
+
24
+ command = {:eval => "sleep(100)"}
25
+ # Should not timeout
26
+ assert admin.command(command)
27
+
28
+ # Should timeout
29
+ command = {:eval => "sleep(1000)"}
30
+ assert_raise Mongo::OperationTimeout do
31
+ admin.command(command)
32
+ end
33
+ end
34
+
35
+ def test_external_timeout_does_not_leave_socket_in_bad_state
36
+ client = standard_connection
37
+ db = client[TEST_DB]
38
+ coll = db['timeout-tests']
39
+
40
+ # prepare the database
41
+ coll.drop
42
+ coll.insert({:a => 1})
43
+
44
+ # use external timeout to mangle socket
45
+ begin
46
+ Timeout::timeout(0.5) do
47
+ db.command({:eval => "sleep(1000)"})
48
+ end
49
+ rescue Timeout::Error
50
+ #puts "Thread timed out and has now mangled the socket"
51
+ end
52
+
53
+ assert_nothing_raised do
54
+ coll.find_one
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,330 @@
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 URITest < Test::Unit::TestCase
18
+ include Mongo
19
+
20
+ def test_uri_without_port
21
+ parser = Mongo::URIParser.new('mongodb://localhost')
22
+ assert_equal 1, parser.nodes.length
23
+ assert_equal 'localhost', parser.nodes[0][0]
24
+ assert_equal 27017, parser.nodes[0][1]
25
+ end
26
+
27
+ def test_basic_uri
28
+ parser = Mongo::URIParser.new('mongodb://localhost:27018')
29
+ assert_equal 1, parser.nodes.length
30
+ assert_equal 'localhost', parser.nodes[0][0]
31
+ assert_equal 27018, parser.nodes[0][1]
32
+ end
33
+
34
+ def test_ipv6_format
35
+ parser = Mongo::URIParser.new('mongodb://[::1]:27018')
36
+ assert_equal 1, parser.nodes.length
37
+ assert_equal '::1', parser.nodes[0][0]
38
+ assert_equal 27018, parser.nodes[0][1]
39
+
40
+ parser = Mongo::URIParser.new('mongodb://[::1]')
41
+ assert_equal 1, parser.nodes.length
42
+ assert_equal '::1', parser.nodes[0][0]
43
+ end
44
+
45
+ def test_ipv6_format_multi
46
+ parser = Mongo::URIParser.new('mongodb://[::1]:27017,[::1]:27018')
47
+ assert_equal 2, parser.nodes.length
48
+ assert_equal '::1', parser.nodes[0][0]
49
+ assert_equal 27017, parser.nodes[0][1]
50
+ assert_equal '::1', parser.nodes[1][0]
51
+ assert_equal 27018, parser.nodes[1][1]
52
+
53
+ parser = Mongo::URIParser.new('mongodb://[::1]:27017,localhost:27018')
54
+ assert_equal 2, parser.nodes.length
55
+ assert_equal '::1', parser.nodes[0][0]
56
+ assert_equal 27017, parser.nodes[0][1]
57
+ assert_equal 'localhost', parser.nodes[1][0]
58
+ assert_equal 27018, parser.nodes[1][1]
59
+
60
+ parser = Mongo::URIParser.new('mongodb://localhost:27017,[::1]:27018')
61
+ assert_equal 2, parser.nodes.length
62
+ assert_equal 'localhost', parser.nodes[0][0]
63
+ assert_equal 27017, parser.nodes[0][1]
64
+ assert_equal '::1', parser.nodes[1][0]
65
+ assert_equal 27018, parser.nodes[1][1]
66
+ end
67
+
68
+ def test_multiple_uris
69
+ parser = Mongo::URIParser.new('mongodb://a.example.com:27018,b.example.com')
70
+ assert_equal 2, parser.nodes.length
71
+ assert_equal ['a.example.com', 27018], parser.nodes[0]
72
+ assert_equal ['b.example.com', 27017], parser.nodes[1]
73
+ end
74
+
75
+ def test_username_without_password
76
+ parser = Mongo::URIParser.new('mongodb://bob:@localhost?authMechanism=GSSAPI')
77
+ assert_equal "bob", parser.auths.first[:username]
78
+ assert_equal nil, parser.auths.first[:password]
79
+
80
+ parser = Mongo::URIParser.new('mongodb://bob@localhost?authMechanism=GSSAPI')
81
+ assert_equal nil, parser.auths.first[:password]
82
+
83
+ assert_raise_error MongoArgumentError do
84
+ Mongo::URIParser.new('mongodb://bob:@localhost')
85
+ end
86
+
87
+ assert_raise_error MongoArgumentError do
88
+ Mongo::URIParser.new('mongodb://bob@localhost')
89
+ end
90
+ end
91
+
92
+ def test_complex_passwords
93
+ parser = Mongo::URIParser.new('mongodb://bob:secret.word@a.example.com:27018/test')
94
+ assert_equal "bob", parser.auths.first[:username]
95
+ assert_equal "secret.word", parser.auths.first[:password]
96
+
97
+ parser = Mongo::URIParser.new('mongodb://bob:s-_3#%R.t@a.example.com:27018/test')
98
+ assert_equal "bob", parser.auths.first[:username]
99
+ assert_equal "s-_3#%R.t", parser.auths.first[:password]
100
+
101
+ assert_raise_error MongoArgumentError do
102
+ Mongo::URIParser.new('mongodb://doctor:bad:wolf@gallifrey.com:27018/test')
103
+ end
104
+
105
+ assert_raise_error MongoArgumentError do
106
+ Mongo::URIParser.new('mongodb://doctor:bow@tie@gallifrey.com:27018/test')
107
+ end
108
+ end
109
+
110
+ def test_complex_usernames
111
+ parser = Mongo::URIParser.new('mongodb://s-_3#%R.t:secret.word@a.example.com:27018/test')
112
+ assert_equal "s-_3#%R.t", parser.auths.first[:username]
113
+
114
+ assert_raise_error MongoArgumentError do
115
+ Mongo::URIParser.new('mongodb://doc:tor:badwolf@gallifrey.com:27018/test')
116
+ end
117
+
118
+ assert_raise_error MongoArgumentError do
119
+ Mongo::URIParser.new('mongodb://d@ctor:bowtie@gallifrey.com:27018/test')
120
+ end
121
+ end
122
+
123
+ def test_username_with_encoded_symbol
124
+ parser = Mongo::URIParser.new('mongodb://f%40o:bar@localhost/admin')
125
+ username = parser.auths.first[:username]
126
+ assert_equal 'f@o', username
127
+
128
+ parser = Mongo::URIParser.new('mongodb://f%3Ao:bar@localhost/admin')
129
+ username = parser.auths.first[:username]
130
+ assert_equal 'f:o', username
131
+ end
132
+
133
+ def test_password_with_encoded_symbol
134
+ parser = Mongo::URIParser.new('mongodb://foo:b%40r@localhost/admin')
135
+ password = parser.auths.first[:password]
136
+ assert_equal 'b@r', password
137
+
138
+ parser = Mongo::URIParser.new('mongodb://foo:b%3Ar@localhost/admin')
139
+ password = parser.auths.first[:password]
140
+ assert_equal 'b:r', password
141
+ end
142
+
143
+ def test_opts_with_semincolon_separator
144
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=direct;slaveok=true;safe=true')
145
+ assert_equal 'direct', parser.connect
146
+ assert parser.direct?
147
+ assert parser.slaveok
148
+ assert parser.safe
149
+ end
150
+
151
+ def test_opts_with_amp_separator
152
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=direct&slaveok=true&safe=true')
153
+ assert_equal 'direct', parser.connect
154
+ assert parser.direct?
155
+ assert parser.slaveok
156
+ assert parser.safe
157
+ end
158
+
159
+ def test_opts_with_uri_encoded_stuff
160
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=%64%69%72%65%63%74&slaveok=%74%72%75%65&safe=true')
161
+ assert_equal 'direct', parser.connect
162
+ assert parser.direct?
163
+ assert parser.slaveok
164
+ assert parser.safe
165
+ end
166
+
167
+ def test_opts_made_invalid_by_mixed_separators
168
+ assert_raise_error MongoArgumentError, "must not mix URL separators ; and &" do
169
+ Mongo::URIParser.new('mongodb://localhost:27018?replicaset=foo;bar&slaveok=true&safe=true')
170
+ end
171
+ end
172
+
173
+ def test_opts_safe
174
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;journal=true;fsync=true;wtimeoutMS=200')
175
+ assert parser.safe
176
+ assert_equal 2, parser.w
177
+ assert parser.fsync
178
+ assert parser.journal
179
+ assert_equal 200, parser.wtimeoutms
180
+ end
181
+
182
+ def test_opts_ssl
183
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?ssl=true;w=2;journal=true;fsync=true;wtimeoutMS=200')
184
+ assert parser.ssl
185
+ end
186
+
187
+ def test_opts_nonsafe_timeout
188
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?connectTimeoutMS=5500&socketTimeoutMS=500')
189
+ assert_equal 5.5, parser.connecttimeoutms
190
+ assert_equal 0.5, parser.sockettimeoutms
191
+ end
192
+
193
+ def test_opts_replica_set
194
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?connect=replicaset;replicaset=foo')
195
+ assert_equal 'foo', parser.replicaset
196
+ assert_equal 'replicaset', parser.connect
197
+ assert parser.replicaset?
198
+ end
199
+
200
+ def test_opts_conflicting_replica_set
201
+ assert_raise_error MongoArgumentError, "connect=direct conflicts with setting a replicaset name" do
202
+ Mongo::URIParser.new('mongodb://localhost:27018?connect=direct;replicaset=foo')
203
+ end
204
+ end
205
+
206
+ def test_case_insensitivity
207
+ parser = Mongo::URIParser.new('mongodb://localhost:27018?wtimeoutms=500&JOURNAL=true&SaFe=true')
208
+ assert_equal 500, parser.wtimeoutms
209
+ assert_equal true, parser.journal
210
+ assert_equal true, parser.safe
211
+ end
212
+
213
+ def test_read_preference_option_primary
214
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=primary")
215
+ assert_equal :primary, parser.readpreference
216
+ end
217
+
218
+ def test_read_preference_option_primary_preferred
219
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=primaryPreferred")
220
+ assert_equal :primary_preferred, parser.readpreference
221
+ end
222
+
223
+ def test_read_preference_option_secondary
224
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=secondary")
225
+ assert_equal :secondary, parser.readpreference
226
+ end
227
+
228
+ def test_read_preference_option_secondary_preferred
229
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=secondaryPreferred")
230
+ assert_equal :secondary_preferred, parser.readpreference
231
+ end
232
+
233
+ def test_read_preference_option_nearest
234
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=nearest")
235
+ assert_equal :nearest, parser.readpreference
236
+ end
237
+
238
+ def test_read_preference_option_with_invalid
239
+ assert_raise_error MongoArgumentError do
240
+ Mongo::URIParser.new("mongodb://localhost:27018?readPreference=invalid")
241
+ end
242
+ end
243
+
244
+ def test_read_preference_connection_options
245
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?replicaset=test&readPreference=nearest")
246
+ assert_equal :nearest, parser.connection_options[:read]
247
+ end
248
+
249
+ def test_read_preference_connection_options_with_no_replica_set
250
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=nearest")
251
+ assert_equal :nearest, parser.connection_options[:read]
252
+ end
253
+
254
+ def test_read_preference_connection_options_prefers_preference_over_slaveok
255
+ parser = Mongo::URIParser.new("mongodb://localhost:27018?replicaset=test&readPreference=nearest&slaveok=true")
256
+ assert_equal :nearest, parser.connection_options[:read]
257
+ end
258
+
259
+ def test_connection_when_sharded_with_no_options
260
+ parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018")
261
+ client = parser.connection({}, false, true)
262
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
263
+ assert_true client.mongos?
264
+ end
265
+
266
+ def test_connection_when_sharded_with_options
267
+ parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018")
268
+ client = parser.connection({ :refresh_interval => 10 }, false, true)
269
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
270
+ assert_equal 10, client.refresh_interval
271
+ assert_true client.mongos?
272
+ end
273
+
274
+ def test_connection_when_sharded_with_uri_options
275
+ parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018?readPreference=nearest")
276
+ client = parser.connection({}, false, true)
277
+ assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
278
+ assert_equal :nearest, client.read
279
+ assert_true client.mongos?
280
+ end
281
+
282
+ def test_auth_source
283
+ parser = Mongo::URIParser.new("mongodb://user:pass@localhost?authSource=foobar")
284
+ assert_equal 'foobar', parser.authsource
285
+ end
286
+
287
+ def test_auth_mechanism
288
+ parser = Mongo::URIParser.new("mongodb://user@localhost?authMechanism=MONGODB-X509")
289
+ assert_equal 'MONGODB-X509', parser.authmechanism
290
+
291
+ assert_raise_error MongoArgumentError do
292
+ Mongo::URIParser.new("mongodb://user@localhost?authMechanism=INVALID")
293
+ end
294
+ end
295
+
296
+ def test_sasl_plain
297
+ parser = Mongo::URIParser.new("mongodb://user:pass@localhost?authMechanism=PLAIN")
298
+ assert_equal 'PLAIN', parser.auths.first[:mechanism]
299
+ assert_equal 'user', parser.auths.first[:username]
300
+ assert_equal 'pass', parser.auths.first[:password]
301
+ assert_equal 'admin', parser.auths.first[:source]
302
+
303
+ parser = Mongo::URIParser.new("mongodb://foo%2Fbar%40example.net:pass@localhost/some_db?authMechanism=PLAIN")
304
+ assert_equal 'PLAIN', parser.auths.first[:mechanism]
305
+ assert_equal 'foo/bar@example.net', parser.auths.first[:username]
306
+ assert_equal 'pass', parser.auths.first[:password]
307
+ assert_equal 'some_db', parser.auths.first[:source]
308
+
309
+ assert_raise_error MongoArgumentError do
310
+ Mongo::URIParser.new("mongodb://user@localhost/some_db?authMechanism=PLAIN")
311
+ end
312
+ end
313
+
314
+ def test_gssapi
315
+ uri = "mongodb://foo%2Fbar%40example.net@localhost?authMechanism=GSSAPI;"
316
+ parser = Mongo::URIParser.new(uri)
317
+ assert_equal 'GSSAPI', parser.auths.first[:mechanism]
318
+ assert_equal 'foo/bar@example.net', parser.auths.first[:username]
319
+
320
+
321
+ uri = "mongodb://foo%2Fbar%40example.net@localhost?authMechanism=GSSAPI;" +
322
+ "gssapiServiceName=mongodb;canonicalizeHostName=true"
323
+ parser = Mongo::URIParser.new(uri)
324
+ assert_equal 'GSSAPI', parser.auths.first[:mechanism]
325
+ assert_equal 'foo/bar@example.net', parser.auths.first[:username]
326
+ assert_equal 'mongodb', parser.auths.first[:extra][:gssapi_service_name]
327
+ assert_equal true, parser.auths.first[:extra][:canonicalize_host_name]
328
+ end
329
+
330
+ end