From bad70fb1d8aa059b6ef62694a1a80ebf2fe8da89 Mon Sep 17 00:00:00 2001 From: Andy Brody Date: Tue, 26 Jan 2016 23:02:07 -0500 Subject: [PATCH] Add more tests for Utils.encode_query_string. These tests, which follow the yard-doc examples, exercise the new ParamsArray behavior. --- lib/restclient/utils.rb | 2 +- spec/unit/utils_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/restclient/utils.rb b/lib/restclient/utils.rb index 958c953..9ed66e6 100644 --- a/lib/restclient/utils.rb +++ b/lib/restclient/utils.rb @@ -154,7 +154,7 @@ module RestClient # => 'foo[string]=&foo[empty]' # # @example Multiple fields with the same name using ParamsArray - # >> encode_query_string(RestClient::ParamsArray.new([[:foo, 1], [:foo, 2], [:foo, 3]]) + # >> encode_query_string(RestClient::ParamsArray.new([[:foo, 1], [:foo, 2], [:foo, 3]])) # => 'foo=1&foo=2&foo=3' # # @example Nested ParamsArray diff --git a/spec/unit/utils_spec.rb b/spec/unit/utils_spec.rb index 06ab531..687bd2c 100644 --- a/spec/unit/utils_spec.rb +++ b/spec/unit/utils_spec.rb @@ -118,5 +118,30 @@ describe RestClient::Utils do RestClient::Utils.encode_query_string(input).should eq expected end end + + it 'handles deep nesting' do + { + {coords: [{x: 1, y: 0}, {x: 2}, {x: 3}]} => 'coords[][x]=1&coords[][y]=0&coords[][x]=2&coords[][x]=3', + }.each_pair do |input, expected| + RestClient::Utils.encode_query_string(input).should eq expected + end + end + + it 'handles multiple fields with the same name using ParamsArray' do + { + RestClient::ParamsArray.new([[:foo, 1], [:foo, 2], [:foo, 3]]) => 'foo=1&foo=2&foo=3', + }.each_pair do |input, expected| + RestClient::Utils.encode_query_string(input).should eq expected + end + end + + it 'handles nested ParamsArrays' do + { + {foo: RestClient::ParamsArray.new([[:a, 1], [:a, 2]])} => 'foo[a]=1&foo[a]=2', + RestClient::ParamsArray.new([[:foo, {a: 1}], [:foo, {a: 2}]]) => 'foo[a]=1&foo[a]=2', + }.each_pair do |input, expected| + RestClient::Utils.encode_query_string(input).should eq expected + end + end end end