Skip to content

Commit c18b440

Browse files
committed
Reuse Rack::Utils#build_nested_query
The rack package already provides a Rack::Utils#build_nested_query. So the rack-test doesn't need to override or re-implement it. By reusing the one from rack, this package will receive improvements as they are made there. For example, the upstream method has improved URL encoding since commit: rack/rack@10864b6
1 parent 28dbe49 commit c18b440

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

lib/rack/test/utils.rb

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ module Utils # :nodoc:
66
include Rack::Utils
77
extend self
88

9-
# Build a query string for the given value and prefix. The value
10-
# can be an array or hash of parameters.
11-
def build_nested_query(value, prefix = nil)
12-
case value
13-
when Array
14-
if value.empty?
15-
"#{prefix}[]="
9+
if Rack.release < '3.1.0'
10+
# Build a query string for the given value and prefix. The value
11+
# can be an array or hash of parameters.
12+
def build_nested_query(value, prefix = nil)
13+
case value
14+
when Array
15+
value.map { |v|
16+
build_nested_query(v, "#{prefix}[]")
17+
}.join("&")
18+
when Hash
19+
value.map { |k, v|
20+
build_nested_query(v, prefix ? "#{prefix}[#{k}]" : k)
21+
}.delete_if(&:empty?).join('&')
22+
when nil
23+
escape(prefix)
1624
else
17-
prefix += "[]" unless unescape(prefix).end_with?('[]')
18-
value.map do |v|
19-
build_nested_query(v, prefix.to_s)
20-
end.join('&')
25+
raise ArgumentError, "value must be a Hash" if prefix.nil?
26+
"#{escape(prefix)}=#{escape(value)}"
2127
end
22-
when Hash
23-
value.map do |k, v|
24-
build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
25-
end.join('&')
26-
when NilClass
27-
prefix.to_s
28-
else
29-
"#{prefix}=#{escape(value)}"
3028
end
3129
end
3230

spec/rack/test/utils_spec.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
describe "Rack::Test::Utils#build_nested_query" do
66
include Rack::Test::Utils
77

8-
it 'converts empty strings to =' do
9-
build_nested_query('').must_equal '='
8+
it 'raises an error if string' do
9+
proc do
10+
build_nested_query('')
11+
end.must_raise(ArgumentError, 'value must be a Hash')
1012
end
1113

1214
it 'converts nil to an empty string' do
@@ -27,36 +29,36 @@
2729
end
2830

2931
it 'converts empty arrays' do
30-
build_nested_query(a: []).must_equal 'a[]='
32+
build_nested_query(a: []).must_equal ''
3133
end
3234

3335
it 'converts arrays with one element' do
34-
build_nested_query(a: [1]).must_equal 'a[]=1'
36+
build_nested_query(a: [1]).must_equal 'a%5B%5D=1'
3537
end
3638

3739
it 'converts arrays with multiple elements' do
38-
build_nested_query(a: [1, 2]).must_equal 'a[]=1&a[]=2'
40+
build_nested_query(a: [1, 2]).must_equal 'a%5B%5D=1&a%5B%5D=2'
3941
end
4042

4143
it "converts arrays with brackets '[]' in the name" do
42-
build_nested_query('a[]' => [1, 2]).must_equal 'a%5B%5D=1&a%5B%5D=2'
44+
build_nested_query('a[]' => [1, 2]).must_equal 'a%5B%5D%5B%5D=1&a%5B%5D%5B%5D=2'
4345
end
4446

4547
it 'converts nested hashes' do
46-
build_nested_query(a: { b: 1 }).must_equal 'a[b]=1'
48+
build_nested_query(a: { b: 1 }).must_equal 'a%5Bb%5D=1'
4749
end
4850

4951
it 'converts arrays nested in a hash' do
50-
build_nested_query(a: { b: [1, 2] }).must_equal 'a[b][]=1&a[b][]=2'
52+
build_nested_query(a: { b: [1, 2] }).must_equal 'a%5Bb%5D%5B%5D=1&a%5Bb%5D%5B%5D=2'
5153
end
5254

5355
it 'converts arrays of hashes' do
54-
build_nested_query(a: [{ b: 2 }, { c: 3 }]).must_equal 'a[][b]=2&a[][c]=3'
56+
build_nested_query(a: [{ b: 2 }, { c: 3 }]).must_equal 'a%5B%5D%5Bb%5D=2&a%5B%5D%5Bc%5D=3'
5557
end
5658

5759
it 'supports hash keys with empty arrays' do
5860
input = { collection: [] }
59-
build_nested_query(input).must_equal 'collection[]='
61+
build_nested_query(input).must_equal ''
6062
end
6163
end
6264

spec/rack/test_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192

193193
it 'accepts params and builds url encoded params for POST requests' do
194194
request '/foo', method: :post, params: { foo: { bar: '1' } }
195-
last_request.env['rack.input'].read.must_equal 'foo[bar]=1'
195+
last_request.env['rack.input'].read.must_equal 'foo%5Bbar%5D=1'
196196
end
197197

198198
it 'accepts raw input in params for POST requests' do

0 commit comments

Comments
 (0)