From d8f94e6f635278924eeeb8626a555e9ce8eabc34 Mon Sep 17 00:00:00 2001 From: Seamus Abshere Date: Mon, 4 Jun 2012 10:03:49 -0500 Subject: [PATCH] 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 --- lib/mongo/util/uri_parser.rb | 10 ++++++++-- test/uri_test.rb | 18 +++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/mongo/util/uri_parser.rb b/lib/mongo/util/uri_parser.rb index de04534..24b3d02 100644 --- a/lib/mongo/util/uri_parser.rb +++ b/lib/mongo/util/uri_parser.rb @@ -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[0] memo[key.downcase.to_sym] = value.strip.downcase memo end diff --git a/test/uri_test.rb b/test/uri_test.rb index 2a7f279..b5eb154 100644 --- a/test/uri_test.rb +++ b/test/uri_test.rb @@ -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')