diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c52b41a..c419984f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - updated Addressable::Template to use RFC 6570, level 4 - fixed compatibility problems with some versions of Ruby - moved unicode tables into a data file for performance reasons +- removing support for multiple query value notations # Addressable 2.2.8 - fixed issues with dot segment removal code diff --git a/lib/addressable/uri.rb b/lib/addressable/uri.rb index 2f74b530..79ba484c 100644 --- a/lib/addressable/uri.rb +++ b/lib/addressable/uri.rb @@ -120,7 +120,11 @@ def self.parse(uri) user = userinfo.strip[/^([^:]*):?/, 1] password = userinfo.strip[/:(.*)$/, 1] end - host = authority.gsub(/^([^\[\]]*)@/, EMPTY_STR).gsub(/:([^:@\[\]]*?)$/, EMPTY_STR) + host = authority.gsub( + /^([^\[\]]*)@/, EMPTY_STR + ).gsub( + /:([^:@\[\]]*?)$/, EMPTY_STR + ) port = authority[/:([^:@\[\]]*?)$/, 1] end if port == EMPTY_STR @@ -365,7 +369,7 @@ class << self # @param [String, Addressable::URI, #to_str] uri # The URI or component to unencode. # - # @param [Class] returning + # @param [Class] return_type # The type of object to return. # This value may only be set to String or # Addressable::URI. All other values are invalid. Defaults @@ -373,8 +377,9 @@ class << self # # @return [String, Addressable::URI] # The unencoded component or URI. - # The return type is determined by the returning parameter. - def self.unencode(uri, returning=String) + # The return type is determined by the return_type + # parameter. + def self.unencode(uri, return_type=String) return nil if uri.nil? begin @@ -382,18 +387,18 @@ def self.unencode(uri, returning=String) rescue NoMethodError, TypeError raise TypeError, "Can't convert #{uri.class} into String." end if !uri.is_a? String - if ![String, ::Addressable::URI].include?(returning) + if ![String, ::Addressable::URI].include?(return_type) raise TypeError, "Expected Class (String or Addressable::URI), " + - "got #{returning.inspect}" + "got #{return_type.inspect}" end result = uri.gsub(/%[0-9a-f]{2}/i) do |sequence| sequence[1..3].to_i(16).chr end result.force_encoding("utf-8") if result.respond_to?(:force_encoding) - if returning == String + if return_type == String return result - elsif returning == ::Addressable::URI + elsif return_type == ::Addressable::URI return ::Addressable::URI.parse(result) end end @@ -478,7 +483,7 @@ def self.normalize_component(component, character_class= # @param [String, Addressable::URI, #to_str] uri # The URI to encode. # - # @param [Class] returning + # @param [Class] return_type # The type of object to return. # This value may only be set to String or # Addressable::URI. All other values are invalid. Defaults @@ -486,8 +491,9 @@ def self.normalize_component(component, character_class= # # @return [String, Addressable::URI] # The encoded URI. - # The return type is determined by the returning parameter. - def self.encode(uri, returning=String) + # The return type is determined by the return_type + # parameter. + def self.encode(uri, return_type=String) return nil if uri.nil? begin @@ -496,10 +502,10 @@ def self.encode(uri, returning=String) raise TypeError, "Can't convert #{uri.class} into String." end if !uri.is_a? String - if ![String, ::Addressable::URI].include?(returning) + if ![String, ::Addressable::URI].include?(return_type) raise TypeError, "Expected Class (String or Addressable::URI), " + - "got #{returning.inspect}" + "got #{return_type.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri) encoded_uri = Addressable::URI.new( @@ -514,9 +520,9 @@ def self.encode(uri, returning=String) :fragment => self.encode_component(uri_object.fragment, Addressable::URI::CharacterClasses::FRAGMENT) ) - if returning == String + if return_type == String return encoded_uri.to_s - elsif returning == ::Addressable::URI + elsif return_type == ::Addressable::URI return encoded_uri end end @@ -532,7 +538,7 @@ class << self # @param [String, Addressable::URI, #to_str] uri # The URI to encode. # - # @param [Class] returning + # @param [Class] return_type # The type of object to return. # This value may only be set to String or # Addressable::URI. All other values are invalid. Defaults @@ -540,18 +546,19 @@ class << self # # @return [String, Addressable::URI] # The encoded URI. - # The return type is determined by the returning parameter. - def self.normalized_encode(uri, returning=String) + # The return type is determined by the return_type + # parameter. + def self.normalized_encode(uri, return_type=String) begin uri = uri.to_str rescue NoMethodError, TypeError raise TypeError, "Can't convert #{uri.class} into String." end if !uri.is_a? String - if ![String, ::Addressable::URI].include?(returning) + if ![String, ::Addressable::URI].include?(return_type) raise TypeError, "Expected Class (String or Addressable::URI), " + - "got #{returning.inspect}" + "got #{return_type.inspect}" end uri_object = uri.kind_of?(self) ? uri : self.parse(uri) components = { @@ -591,9 +598,9 @@ def self.normalized_encode(uri, returning=String) :fragment => self.encode_component(components[:fragment], Addressable::URI::CharacterClasses::FRAGMENT) ) - if returning == String + if return_type == String return encoded_uri.to_s - elsif returning == ::Addressable::URI + elsif return_type == ::Addressable::URI return encoded_uri end end @@ -1097,8 +1104,11 @@ def authority=(new_authority) new_user = new_userinfo.strip[/^([^:]*):?/, 1] new_password = new_userinfo.strip[/:(.*)$/, 1] end - new_host = - new_authority.gsub(/^([^\[\]]*)@/, EMPTY_STR).gsub(/:([^:@\[\]]*?)$/, EMPTY_STR) + new_host = new_authority.gsub( + /^([^\[\]]*)@/, EMPTY_STR + ).gsub( + /:([^:@\[\]]*?)$/, EMPTY_STR + ) new_port = new_authority[/:([^:@\[\]]*?)$/, 1] end @@ -1399,114 +1409,72 @@ def query=(new_query) ## # Converts the query component to a Hash value. # - # @option [Symbol] notation - # May be one of :flat, :dot, or - # :subscript. The :dot notation is not - # supported for assignment. Default value is :subscript. + # @param [Class] return_type The return type desired. Value must be either + # `Hash` or `Array`. # # @return [Hash, Array] The query string parsed as a Hash or Array object. # # @example # Addressable::URI.parse("?one=1&two=2&three=3").query_values # #=> {"one" => "1", "two" => "2", "three" => "3"} - # Addressable::URI.parse("?one[two][three]=four").query_values - # #=> {"one" => {"two" => {"three" => "four"}}} - # Addressable::URI.parse("?one.two.three=four").query_values( - # :notation => :dot - # ) - # #=> {"one" => {"two" => {"three" => "four"}}} - # Addressable::URI.parse("?one[two][three]=four").query_values( - # :notation => :flat - # ) - # #=> {"one[two][three]" => "four"} - # Addressable::URI.parse("?one.two.three=four").query_values( - # :notation => :flat - # ) - # #=> {"one.two.three" => "four"} - # Addressable::URI.parse( - # "?one[two][three][]=four&one[two][three][]=five" - # ).query_values - # #=> {"one" => {"two" => {"three" => ["four", "five"]}}} - # Addressable::URI.parse( - # "?one=two&one=three").query_values(:notation => :flat_array) - # #=> [['one', 'two'], ['one', 'three']] - def query_values(options={}) - defaults = {:notation => :subscript} - options = defaults.merge(options) - if ![:flat, :dot, :subscript, :flat_array].include?(options[:notation]) - raise ArgumentError, - "Invalid notation. Must be one of: " + - "[:flat, :dot, :subscript, :flat_array]." - end - dehash = lambda do |hash| - hash.each do |(key, value)| - if value.kind_of?(Hash) - hash[key] = dehash.call(value) - end - end - if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ } - hash.sort.inject([]) do |accu, (_, value)| - accu << value; accu - end - else - hash - end + # Addressable::URI.parse("?one=two&one=three").query_values(Array) + # #=> [["one", "two"], ["one", "three"]] + # Addressable::URI.parse("?one=two&one=three").query_values(Hash) + # #=> {"one" => "three"} + def query_values(return_type=Hash) + empty_accumulator = Array == return_type ? [] : {} + if return_type != Hash && return_type != Array + raise ArgumentError, "Invalid return type. Must be Hash or Array." end return nil if self.query == nil - empty_accumulator = :flat_array == options[:notation] ? [] : {} - return ((self.query.split("&").map do |pair| + split_query = (self.query.split("&").map do |pair| pair.split("=", 2) if pair && !pair.empty? - end).compact.inject(empty_accumulator.dup) do |accumulator, (key, value)| - value = true if value.nil? - key = URI.unencode_component(key) - if value != true - value = URI.unencode_component(value.gsub(/\+/, " ")) + end).compact + return split_query.inject(empty_accumulator.dup) do |accu, pair| + # I'd rather use key/value identifiers instead of array lookups, + # but in this case I really want to maintain the exact pair structure, + # so it's best to make all changes in-place. + pair[0] = URI.unencode_component(pair[0]) + # This looks weird, but it's correct. Handles query values like: + # ?data=1&flag&color=blue + # In this case, `flag` would evaluate to `true`, which is what you + # want. Its absence implies that `flag` resolves to `false`. + # value = true if value.nil? + if pair[1].respond_to?(:to_str) + # I loathe the fact that I have to do this. Stupid HTML 4.01. + # Treating '+' as a space was just an unbelievably bad idea. + # There was nothing wrong with '%20'! + # If it ain't broke, don't fix it! + pair[1] = URI.unencode_component(pair[1].to_str.gsub(/\+/, " ")) end - if options[:notation] == :flat - if accumulator[key] - raise ArgumentError, "Key was repeated: #{key.inspect}" - end - accumulator[key] = value - elsif options[:notation] == :flat_array - accumulator << [key, value] + if return_type == Hash + accu[pair[0]] = pair[1] else - if options[:notation] == :dot - array_value = false - subkeys = key.split(".") - elsif options[:notation] == :subscript - array_value = !!(key =~ /\[\]$/) - subkeys = key.split(/[\[\]]+/) - end - current_hash = accumulator - for i in 0...(subkeys.size - 1) - subkey = subkeys[i] - current_hash[subkey] = {} unless current_hash[subkey] - current_hash = current_hash[subkey] - end - if array_value - current_hash[subkeys.last] = [] unless current_hash[subkeys.last] - current_hash[subkeys.last] << value - else - current_hash[subkeys.last] = value - end + accu << pair end - accumulator - end).inject(empty_accumulator.dup) do |accumulator, (key, value)| - if options[:notation] == :flat_array - accumulator << [key, value] - else - accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value - end - accumulator + accu end end ## # Sets the query component for this URI from a Hash object. - # This method produces a query string using the :subscript notation. - # An empty Hash will result in a nil query. + # An empty Hash or Array will result in an empty query string. # # @param [Hash, #to_hash, Array] new_query_values The new query values. + # + # @example + # uri.query_values = {:a => "a", :b => ["c", "d", "e"]} + # uri.query + # # => "a=a&b=c&b=d&b=e" + # uri.query_values = [['a', 'a'], ['b', 'c'], ['b', 'd'], ['b', 'e']] + # uri.query + # # => "a=a&b=c&b=d&b=e" + # uri.query_values = [['a', 'a'], ['b', ['c', 'd', 'e']]] + # uri.query + # # => "a=a&b=c&b=d&b=e" + # uri.query_values = [['flag'], ['key', 'value']] + # uri.query + # # => "flag&key=value" def query_values=(new_query_values) if new_query_values == nil self.query = nil @@ -1528,55 +1496,28 @@ def query_values=(new_query_values) new_query_values.sort! end - ## - # Joins and converts parent and value into a properly encoded and - # ordered URL query. - # - # @private - # @param [String] parent an URI encoded component. - # @param [Array, Hash, Symbol, #to_str] value - # - # @return [String] a properly escaped and ordered URL query. - to_query = lambda do |parent, value| - if value.is_a?(Hash) - value = value.map do |key, val| - [ - URI.encode_component(key, CharacterClasses::UNRESERVED), - val - ] - end - value.sort! - buffer = "" - value.each do |key, val| - new_parent = "#{parent}[#{key}]" - buffer << "#{to_query.call(new_parent, val)}&" - end - return buffer.chop - elsif value.is_a?(Array) - buffer = "" - value.each_with_index do |val, i| - new_parent = "#{parent}[#{i}]" - buffer << "#{to_query.call(new_parent, val)}&" + # new_query_values have form [['key1', 'value1'], ['key2', 'value2']] + buffer = "" + new_query_values.each do |key, value| + encoded_key = URI.encode_component( + key, CharacterClasses::UNRESERVED + ) + if value == nil || value == true + buffer << "#{encoded_key}&" + elsif value.kind_of?(Array) + value.each do |sub_value| + encoded_value = URI.encode_component( + sub_value, CharacterClasses::UNRESERVED + ) + buffer << "#{encoded_key}=#{encoded_value}&" end - return buffer.chop - elsif value == true - return parent else encoded_value = URI.encode_component( value, CharacterClasses::UNRESERVED ) - return "#{parent}=#{encoded_value}" + buffer << "#{encoded_key}=#{encoded_value}&" end end - - # new_query_values have form [['key1', 'value1'], ['key2', 'value2']] - buffer = "" - new_query_values.each do |parent, value| - encoded_parent = URI.encode_component( - parent, CharacterClasses::UNRESERVED - ) - buffer << "#{to_query.call(encoded_parent, value)}&" - end self.query = buffer.chop end diff --git a/spec/addressable/uri_spec.rb b/spec/addressable/uri_spec.rb index eb4cea91..92e22c9e 100644 --- a/spec/addressable/uri_spec.rb +++ b/spec/addressable/uri_spec.rb @@ -2833,11 +2833,11 @@ def to_s end it "should have the correct query string after hash assignment" do - @uri.query_values = {"?uestion mark"=>"=sign", "hello"=>"g\xC3\xBCnther"} + @uri.query_values = {"?uestion mark" => "=sign", "hello" => "g\xC3\xBCnther"} @uri.query.split("&").should include("%3Fuestion%20mark=%3Dsign") @uri.query.split("&").should include("hello=g%C3%BCnther") @uri.query_values.should == { - "?uestion mark"=>"=sign", "hello"=>"g\xC3\xBCnther" + "?uestion mark" => "=sign", "hello" => "g\xC3\xBCnther" } end @@ -2846,8 +2846,9 @@ def to_s @uri.query.split("&").should include("flag%3F1") @uri.query.split("&").should include("fl%3Dag2") @uri.query.split("&").should include("flag3") - @uri.query_values.should == { - 'flag?1' => true, 'fl=ag2' => true, 'flag3' => true + @uri.query_values(Array).sort.should == [["fl=ag2"], ["flag3"], ["flag?1"]] + @uri.query_values(Hash).should == { + 'flag?1' => nil, 'fl=ag2' => nil, 'flag3' => nil } end @@ -3036,7 +3037,7 @@ def to_s end it "should have query_values of {'q' => true, 'x' => 'b'}" do - @uri.query_values.should == {'q' => true, 'x' => 'b'} + @uri.query_values.should == {'q' => nil, 'x' => 'b'} end end @@ -3545,17 +3546,10 @@ def to_s @uri = Addressable::URI.parse("?") end - it "should have the correct subscript notation query values" do + it "should have the correct return type" do @uri.query_values.should == {} - @uri.query_values(:notation => :subscript).should == {} - end - - it "should have the correct dot notation query values" do - @uri.query_values(:notation => :dot).should == {} - end - - it "should have the correct flat notation query values" do - @uri.query_values(:notation => :flat).should == {} + @uri.query_values(Hash).should == {} + @uri.query_values(Array).should == [] end it "should have a 'null' origin" do @@ -3572,14 +3566,14 @@ def to_s @uri.query_values.should == {"one" => "1", "two" => "2", "three" => "3"} end - it "should raise an error for invalid query value notations" do + it "should raise an error for invalid return type values" do (lambda do - @uri.query_values(:notation => :bogus) + @uri.query_values(Fixnum) end).should raise_error(ArgumentError) end - it "should have the correct flat array notation query values" do - @uri.query_values(:notation => :flat_array).should == [ + it "should have the correct array query values" do + @uri.query_values(Array).should == [ ["one", "1"], ["two", "2"], ["three", "3"] ] end @@ -3598,8 +3592,8 @@ def to_s @uri.query_values.should == {"one" => "1=uno", "two" => "2=dos"} end - it "should have the correct flat array notation query values" do - @uri.query_values(:notation => :flat_array).should == [ + it "should have the correct array query values" do + @uri.query_values(Array).should == [ ["one", "1=uno"], ["two", "2=dos"] ] end @@ -3611,17 +3605,11 @@ def to_s end it "should have the correct query values" do - @uri.query_values.should == {"one" => {"two" => {"three" => "four"}}} - end - - it "should have the correct flat notation query values" do - @uri.query_values(:notation => :flat).should == { - "one[two][three]" => "four" - } + @uri.query_values.should == {"one[two][three]" => "four"} end - it "should have the correct flat array notation query values" do - @uri.query_values(:notation => :flat_array).should == [ + it "should have the correct array query values" do + @uri.query_values(Array).should == [ ["one[two][three]", "four"] ] end @@ -3632,20 +3620,14 @@ def to_s @uri = Addressable::URI.parse("?one.two.three=four") end - it "should have the correct dot notation query values" do - @uri.query_values(:notation => :dot).should == { - "one" => {"two" => {"three" => "four"}} - } - end - - it "should have the correct flat notation query values" do - @uri.query_values(:notation => :flat).should == { + it "should have the correct query values" do + @uri.query_values.should == { "one.two.three" => "four" } end - it "should have the correct flat array notation query values" do - @uri.query_values(:notation => :flat_array).should == [ + it "should have the correct array query values" do + @uri.query_values(Array).should == [ ["one.two.three", "four"] ] end @@ -3657,21 +3639,14 @@ def to_s @uri = Addressable::URI.parse("?one[two][three]=four&one[two][five]=six") end - it "should have the correct dot notation query values" do - @uri.query_values(:notation => :subscript).should == { - "one" => {"two" => {"three" => "four", "five" => "six"}} - } - end - - it "should have the correct flat notation query values" do - @uri.query_values(:notation => :flat).should == { - "one[two][three]" => "four", - "one[two][five]" => "six" + it "should have the correct query values" do + @uri.query_values.should == { + "one[two][three]" => "four", "one[two][five]" => "six" } end - it "should have the correct flat array notation query values" do - @uri.query_values(:notation => :flat_array).should == [ + it "should have the correct array query values" do + @uri.query_values(Array).should == [ ["one[two][three]", "four"], ["one[two][five]", "six"] ] end @@ -3683,21 +3658,14 @@ def to_s @uri = Addressable::URI.parse("?one.two.three=four&one.two.five=six") end - it "should have the correct dot notation query values" do - @uri.query_values(:notation => :dot).should == { - "one" => {"two" => {"three" => "four", "five" => "six"}} - } - end - - it "should have the correct flat notation query values" do - @uri.query_values(:notation => :flat).should == { - "one.two.three" => "four", - "one.two.five" => "six" + it "should have the correct query values" do + @uri.query_values.should == { + "one.two.three" => "four", "one.two.five" => "six" } end - it "should have the correct flat array notation query values" do - @uri.query_values(:notation => :flat_array).should == [ + it "should have the correct array query values" do + @uri.query_values(Array).should == [ ["one.two.three", "four"], ["one.two.five", "six"] ] end @@ -3711,8 +3679,8 @@ def to_s ) end - it "should have correct flat_array notation query values" do - @uri.query_values(:notation => :flat_array).should == + it "should have correct array query values" do + @uri.query_values(Array).should == [['one', 'two'], ['one', 'three']] end end @@ -3725,23 +3693,14 @@ def to_s ) end - it "should have the correct subscript notation query values" do - @uri.query_values(:notation => :subscript).should == { - "one" => {"two" => {"three" => ["four", "five"]}} - } + it "should have correct query values" do + @uri.query_values(Hash).should == {"one[two][three][]" => "five"} end - it "should raise an error if a key is repeated in the flat notation" do - (lambda do - @uri.query_values(:notation => :flat) - end).should raise_error(ArgumentError) - end - - it "should not raise an error if a key is " + - "repeated in the flat array notation" do - (lambda do - @uri.query_values(:notation => :flat_array) - end).should_not raise_error + it "should have correct array query values" do + @uri.query_values(Array).should == [ + ["one[two][three][]", "four"], ["one[two][three][]", "five"] + ] end end @@ -3753,9 +3712,9 @@ def to_s ) end - it "should have the correct subscript notation query values" do - @uri.query_values(:notation => :subscript).should == { - "one" => {"two" => {"three" => ["four", "five"]}} + it "should have the correct query values" do + @uri.query_values.should == { + "one[two][three][0]" => "four", "one[two][three][1]" => "five" } end end @@ -3768,9 +3727,9 @@ def to_s ) end - it "should have the correct subscript notation query values" do - @uri.query_values(:notation => :subscript).should == { - "one" => {"two" => {"three" => ["five", "four"]}} + it "should have the correct query values" do + @uri.query_values.should == { + "one[two][three][1]" => "four", "one[two][three][0]" => "five" } end end @@ -3783,9 +3742,9 @@ def to_s ) end - it "should have the correct subscript notation query values" do - @uri.query_values(:notation => :subscript).should == { - "one" => {"two" => {"three" => ["five", "four"]}} + it "should have the correct query values" do + @uri.query_values.should == { + "one[two][three][2]" => "four", "one[two][three][1]" => "five" } end end @@ -4856,51 +4815,58 @@ def to_str it "should correctly assign {:a => 'a', :b => ['c', 'd', 'e']}" do @uri.query_values = {:a => "a", :b => ["c", "d", "e"]} - @uri.query.should == "a=a&b[0]=c&b[1]=d&b[2]=e" + @uri.query.should == "a=a&b=c&b=d&b=e" end - it "should correctly assign {'a' => {'b' => [ 'c']}}" do - @uri.query_values = { 'a' => {'b' => [ 'c'] } } - @uri.query.should == "a[b][0]=c" + it "should raise an error attempting to assign {'a' => {'b' => ['c']}}" do + (lambda do + @uri.query_values = { 'a' => {'b' => ['c'] } } + end).should raise_error(TypeError) end - it "should correctly assign {:b => '2', :a => {:c => '1'}}" do - @uri.query_values = {:b => '2', :a => { :c => '1' }} - @uri.query.should == "a[c]=1&b=2" + it "should raise an error attempting to assign " + + "{:b => '2', :a => {:c => '1'}}" do + (lambda do + @uri.query_values = {:b => '2', :a => {:c => '1'}} + end).should raise_error(TypeError) end - it "should correctly assign " + + it "should raise an error attempting to assign " + "{:a => 'a', :b => [{:c => 'c', :d => 'd'}, " + "{:e => 'e', :f => 'f'}]}" do - @uri.query_values = { - :a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}] - } - @uri.query.should == "a=a&b[0][c]=c&b[0][d]=d&b[1][e]=e&b[1][f]=f" + (lambda do + @uri.query_values = { + :a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}] + } + end).should raise_error(TypeError) end - it "should correctly assign " + + it "should raise an error attempting to assign " + "{:a => 'a', :b => [{:c => true, :d => 'd'}, " + "{:e => 'e', :f => 'f'}]}" do - @uri.query_values = { - :a => 'a', :b => [{:c => true, :d => 'd'}, {:e => 'e', :f => 'f'}] - } - @uri.query.should == "a=a&b[0][c]&b[0][d]=d&b[1][e]=e&b[1][f]=f" + (lambda do + @uri.query_values = { + :a => 'a', :b => [{:c => true, :d => 'd'}, {:e => 'e', :f => 'f'}] + } + end).should raise_error(TypeError) end - it "should correctly assign " + + it "should raise an error attempting to assign " + "{:a => 'a', :b => {:c => true, :d => 'd'}}" do - @uri.query_values = { - :a => 'a', :b => {:c => true, :d => 'd'} - } - @uri.query.should == "a=a&b[c]&b[d]=d" + (lambda do + @uri.query_values = { + :a => 'a', :b => {:c => true, :d => 'd'} + } + end).should raise_error(TypeError) end - it "should correctly assign " + + it "should raise an error attempting to assign " + "{:a => 'a', :b => {:c => true, :d => 'd'}}" do - @uri.query_values = { - :a => 'a', :b => {:c => true, :d => 'd'} - } - @uri.query.should == "a=a&b[c]&b[d]=d" + (lambda do + @uri.query_values = { + :a => 'a', :b => {:c => true, :d => 'd'} + } + end).should raise_error(TypeError) end it "should correctly assign {:a => 1, :b => 1.5}" do @@ -4908,18 +4874,16 @@ def to_str @uri.query.should == "a=1&b=1.5" end - it "should correctly assign " + + it "should raise an error attempting to assign " + "{:z => 1, :f => [2, {999.1 => [3,'4']}, ['h', 'i']], " + ":a => {:b => ['c', 'd'], :e => true, :y => 0.5}}" do - @uri.query_values = { - :z => 1, - :f => [ 2, {999.1 => [3,'4']}, ['h', 'i'] ], - :a => { :b => ['c', 'd'], :e => true, :y => 0.5 } - } - @uri.query.should == ( - "a[b][0]=c&a[b][1]=d&a[e]&a[y]=0.5&f[0]=2&" + - "f[1][999.1][0]=3&f[1][999.1][1]=4&f[2][0]=h&f[2][1]=i&z=1" - ) + (lambda do + @uri.query_values = { + :z => 1, + :f => [ 2, {999.1 => [3,'4']}, ['h', 'i'] ], + :a => { :b => ['c', 'd'], :e => true, :y => 0.5 } + } + end).should raise_error(TypeError) end it "should correctly assign {}" do @@ -4948,7 +4912,7 @@ def to_str query_string = (('a'..'z').to_a.reverse.map { |e| "#{e}=#{e}" }).join("&") @uri.query = query_string original_uri = @uri.to_s - @uri.query_values = @uri.query_values(:notation => :flat_array) + @uri.query_values = @uri.query_values(Array) @uri.to_s.should == original_uri end end