Since URI.decode_www_form is Ruby 1.9-only, use CGI.parse instead

Conflicts:
	lib/mongo/util/uri_parser.rb - replaced @TylerBrock's (temp?) fix with this one, which uses the stdlib
	test/uri_test.rb - reintroduced test for conflicting URL separators... and added a new test for URL-encoded parts
This commit is contained in:
Seamus Abshere 2012-06-04 10:03:49 -05:00
parent 9d859d2b5c
commit d8f94e6f63
2 changed files with 21 additions and 7 deletions

View File

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

View File

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