RUBY-404 make uri option paramaters case insensitive, test included

This commit is contained in:
Tyler Brock 2012-01-30 12:06:20 -05:00
parent 6ae5dbd37c
commit a193c055ab
2 changed files with 31 additions and 24 deletions

View File

@ -35,7 +35,7 @@ module Mongo
MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]" MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"
SPEC_ATTRS = [:nodes, :auths] SPEC_ATTRS = [:nodes, :auths]
OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connectTimeoutMS, :socketTimeoutMS, :wtimeoutMS] OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connecttimeoutms, :sockettimeoutms, :wtimeoutms]
OPT_VALID = {:connect => lambda {|arg| ['direct', 'replicaset'].include?(arg)}, OPT_VALID = {:connect => lambda {|arg| ['direct', 'replicaset'].include?(arg)},
:replicaset => lambda {|arg| arg.length > 0}, :replicaset => lambda {|arg| arg.length > 0},
@ -45,9 +45,9 @@ module Mongo
:wtimeout => lambda {|arg| arg =~ /^\d+$/ }, :wtimeout => lambda {|arg| arg =~ /^\d+$/ },
:fsync => lambda {|arg| ['true', 'false'].include?(arg)}, :fsync => lambda {|arg| ['true', 'false'].include?(arg)},
:journal => lambda {|arg| ['true', 'false'].include?(arg)}, :journal => lambda {|arg| ['true', 'false'].include?(arg)},
:connectTimeoutMS => lambda {|arg| arg =~ /^\d+$/ }, :connecttimeoutms => lambda {|arg| arg =~ /^\d+$/ },
:socketTimeoutMS => lambda {|arg| arg =~ /^\d+$/ }, :sockettimeoutms => lambda {|arg| arg =~ /^\d+$/ },
:wtimeoutMS => lambda {|arg| arg =~ /^\d+$/ } :wtimeoutms => lambda {|arg| arg =~ /^\d+$/ }
} }
OPT_ERR = {:connect => "must be 'direct' or 'replicaset'", OPT_ERR = {:connect => "must be 'direct' or 'replicaset'",
@ -58,9 +58,9 @@ module Mongo
:wtimeout => "must be an integer specifying milliseconds", :wtimeout => "must be an integer specifying milliseconds",
:fsync => "must be 'true' or 'false'", :fsync => "must be 'true' or 'false'",
:journal => "must be 'true' or 'false'", :journal => "must be 'true' or 'false'",
:connectTimeoutMS => "must be an integer specifying milliseconds", :connecttimeoutms => "must be an integer specifying milliseconds",
:socketTimeoutMS => "must be an integer specifying milliseconds", :sockettimeoutms => "must be an integer specifying milliseconds",
:wtimeoutMS => "must be an integer specifying milliseconds" :wtimeoutms => "must be an integer specifying milliseconds"
} }
OPT_CONV = {:connect => lambda {|arg| arg}, OPT_CONV = {:connect => lambda {|arg| arg},
@ -71,12 +71,12 @@ module Mongo
:wtimeout => lambda {|arg| arg.to_i}, :wtimeout => lambda {|arg| arg.to_i},
:fsync => lambda {|arg| arg == 'true' ? true : false}, :fsync => lambda {|arg| arg == 'true' ? true : false},
:journal => lambda {|arg| arg == 'true' ? true : false}, :journal => lambda {|arg| arg == 'true' ? true : false},
:connectTimeoutMS => lambda {|arg| arg.to_f / 1000 }, # stored as seconds :connecttimeoutms => lambda {|arg| arg.to_f / 1000 }, # stored as seconds
:socketTimeoutMS => lambda {|arg| arg.to_f / 1000 }, # stored as seconds :sockettimeoutms => lambda {|arg| arg.to_f / 1000 }, # stored as seconds
:wtimeoutMS => lambda {|arg| arg.to_i } :wtimeoutms => lambda {|arg| arg.to_i }
} }
attr_reader :nodes, :auths, :connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connectTimeoutMS, :socketTimeoutMS, :wtimeoutMS attr_reader :nodes, :auths, :connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connecttimeoutms, :sockettimeoutms, :wtimeoutms
# Parse a MongoDB URI. This method is used by Connection.from_uri. # Parse a MongoDB URI. This method is used by Connection.from_uri.
# Returns an array of nodes and an array of db authorizations, if applicable. # Returns an array of nodes and an array of db authorizations, if applicable.
@ -100,12 +100,12 @@ module Mongo
def connection_options def connection_options
opts = {} opts = {}
if (@w || @journal || @wtimeout || @fsync || @wtimeoutMS) && !@safe if (@w || @journal || @wtimeout || @fsync || @wtimeoutms) && !@safe
raise MongoArgumentError, "Safe must be true if w, journal, wtimeoutMS, or fsync is specified" raise MongoArgumentError, "Safe must be true if w, journal, wtimeoutMS, or fsync is specified"
end end
if @safe if @safe
if @w || @journal || @wtimeout || @fsync || @wtimeoutMS if @w || @journal || @wtimeout || @fsync || @wtimeoutms
safe_opts = {} safe_opts = {}
safe_opts[:w] = @w if @w safe_opts[:w] = @w if @w
safe_opts[:j] = @journal if @journal safe_opts[:j] = @journal if @journal
@ -115,8 +115,8 @@ module Mongo
safe_opts[:wtimeout] = @wtimeout safe_opts[:wtimeout] = @wtimeout
end end
if @wtimeoutMS if @wtimeoutms
safe_opts[:wtimeout] = @wtimeoutMS safe_opts[:wtimeout] = @wtimeoutms
end end
safe_opts[:fsync] = @fsync if @fsync safe_opts[:fsync] = @fsync if @fsync
@ -127,12 +127,12 @@ module Mongo
opts[:safe] = safe_opts opts[:safe] = safe_opts
end end
if @connectTimeoutMS if @connecttimeoutms
opts[:connect_timeout] = @connectTimeoutMS opts[:connect_timeout] = @connecttimeoutms
end end
if @socketTimeoutMS if @sockettimeoutms
opts[:op_timeout] = @socketTimeoutMS opts[:op_timeout] = @sockettimeoutms
end end
if @slaveok if @slaveok
@ -200,7 +200,7 @@ module Mongo
separator = opts.include?('&') ? '&' : ';' separator = opts.include?('&') ? '&' : ';'
opts.split(separator).each do |attr| opts.split(separator).each do |attr|
key, value = attr.split('=') key, value = attr.split('=')
key = key.to_sym key = key.to_sym.downcase
value = value.strip.downcase value = value.strip.downcase
if !OPT_ATTRS.include?(key) if !OPT_ATTRS.include?(key)
raise MongoArgumentError, "Invalid Mongo URI option #{key}" raise MongoArgumentError, "Invalid Mongo URI option #{key}"

View File

@ -1,6 +1,6 @@
require './test/test_helper' require './test/test_helper'
class TestThreading < Test::Unit::TestCase class URITest < Test::Unit::TestCase
include Mongo include Mongo
def test_uri_without_port def test_uri_without_port
@ -84,13 +84,13 @@ class TestThreading < Test::Unit::TestCase
assert_equal 200, parser.wtimeout assert_equal 200, parser.wtimeout
assert parser.fsync assert parser.fsync
assert parser.journal assert parser.journal
assert_equal 200, parser.wtimeoutMS assert_equal 200, parser.wtimeoutms
end end
def test_opts_nonsafe_timeout def test_opts_nonsafe_timeout
parser = Mongo::URIParser.new('mongodb://localhost:27018?connectTimeoutMS=5500&socketTimeoutMS=500') parser = Mongo::URIParser.new('mongodb://localhost:27018?connectTimeoutMS=5500&socketTimeoutMS=500')
assert_equal 5.5, parser.connectTimeoutMS assert_equal 5.5, parser.connecttimeoutms
assert_equal 0.5, parser.socketTimeoutMS assert_equal 0.5, parser.sockettimeoutms
end end
def test_opts_replica_set def test_opts_replica_set
@ -101,4 +101,11 @@ class TestThreading < Test::Unit::TestCase
assert_equal 'foo', parser.replicaset assert_equal 'foo', parser.replicaset
assert_equal 'replicaset', parser.connect assert_equal 'replicaset', parser.connect
end end
def test_case_insensitivity
parser = Mongo::URIParser.new('mongodb://localhost:27018?wtimeoutms=500&JOURNAL=true&SaFe=true')
assert_equal 500, parser.wtimeoutms
assert_equal true, parser.journal
assert_equal true, parser.safe
end
end end