Merge pull request #103 from seamusabshere/ruby_18_url_decoding

Since `URI.decode_www_form` is Ruby 1.9-only, use `CGI.parse` instead
This commit is contained in:
Tyler Brock 2012-06-04 20:58:47 -07:00
commit c0a4ae6b44
2 changed files with 21 additions and 7 deletions

View File

@ -16,6 +16,8 @@
# limitations under the License.
# ++
require 'cgi'
module Mongo
class URIParser
@ -260,8 +262,12 @@ module Mongo
return if string_opts.empty? && extra_opts.empty?
opts = string_opts.split(/&|;/).inject({}) do |memo, kv|
key, value = kv.split('=')
if string_opts.include?(';') and string_opts.include?('&')
raise MongoArgumentError, "must not mix URL separators ; and &"
end
opts = CGI.parse(string_opts).inject({}) do |memo, (key, value)|
value = value.first
memo[key.downcase.to_sym] = value.strip.downcase
memo
end

View File

@ -79,11 +79,19 @@ class URITest < Test::Unit::TestCase
assert parser.safe
end
#def test_opts_made_invalid_by_mixed_separators
# assert_raise_error ArgumentError, "invalid data of application/x-www-form-urlencoded (replicaset=foo;bar&slaveok=true&safe=true)" do
# Mongo::URIParser.new('mongodb://localhost:27018?replicaset=foo;bar&slaveok=true&safe=true')
# end
#end
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
def test_opts_safe
parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;journal=true;fsync=true;wtimeoutMS=200')