diff options
author | James Tucker <jftucker@gmail.com> | 2014-08-03 14:10:33 -0300 |
---|---|---|
committer | James Tucker <jftucker@gmail.com> | 2014-08-03 14:10:33 -0300 |
commit | 6ee42d625f68b09a2864ddcc6ee7b9bbd69fd31e (patch) | |
tree | eeb09950bc523bdaec1382d6be1cee6f6e1d74ff | |
parent | 60a25f2c26b764aefc882f197f8b2af248b405d2 (diff) | |
parent | d77bd3ffa135d887fa2fcbae6514f3f919be577d (diff) | |
download | rack-6ee42d625f68b09a2864ddcc6ee7b9bbd69fd31e.tar.gz |
Merge pull request #698 from IvanUkhov/build-query-empty-arrays
Utils.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 53303995..691a4a6e 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -178,7 +178,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 4b989db7..4ee2d03f 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -257,6 +257,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 |