diff options
author | Ivan Ukhov <ivan.ukhov@gmail.com> | 2014-06-16 20:48:12 +0200 |
---|---|---|
committer | Ivan Ukhov <ivan.ukhov@gmail.com> | 2014-06-16 21:50:09 +0200 |
commit | d77bd3ffa135d887fa2fcbae6514f3f919be577d (patch) | |
tree | b325084f0ff52344b67a20f7d6c0c24bf2ddaa3f | |
parent | 93e7d7a9bec530b01c72d8f87761e9e2929d2f8c (diff) | |
download | rack-d77bd3ffa135d887fa2fcbae6514f3f919be577d.tar.gz |
Fixed build_nested_query to handle empty arrays and hashes
-rw-r--r-- | lib/rack/utils.rb | 2 | ||||
-rw-r--r-- | test/spec_utils.rb | 8 |
2 files changed, 9 insertions, 1 deletions
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index 6c2bf907..b5295dea 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -159,7 +159,7 @@ module Rack when Hash value.map { |k, v| build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) - }.join("&") + }.reject(&:empty?).join('&') when String raise ArgumentError, "value must be a Hash" if prefix.nil? "#{prefix}=#{escape(value)}" diff --git a/test/spec_utils.rb b/test/spec_utils.rb index c3867965..e8531369 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -240,6 +240,14 @@ describe Rack::Utils do should.equal "foo[]=" Rack::Utils.build_nested_query("foo" => ["bar"]). should.equal "foo[]=bar" + Rack::Utils.build_nested_query('foo' => []). + should.equal '' + Rack::Utils.build_nested_query('foo' => {}). + should.equal '' + Rack::Utils.build_nested_query('foo' => 'bar', 'baz' => []). + should.equal 'foo=bar' + Rack::Utils.build_nested_query('foo' => 'bar', 'baz' => {}). + should.equal 'foo=bar' # The ordering of the output query string is unpredictable with 1.8's # unordered hash. Test that build_nested_query performs the inverse |