mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Actually allow top level params to be ParamsArray.
Because of some idiosyncratic behavior in Request#process_url_params, we would previously only handle params that were Hash objects. This is really confusing behavior that stems from mixing options and headers. Unfortunately we won't be able to clean that up until rest-client 3.0, but at least allow ParamsArray or Hash for now.
This commit is contained in:
parent
2dc559a4bf
commit
43846066c5
2 changed files with 31 additions and 5 deletions
|
@ -224,18 +224,38 @@ module RestClient
|
|||
end
|
||||
|
||||
# Extract the query parameters and append them to the url
|
||||
def process_url_params url, headers
|
||||
url_params = {}
|
||||
#
|
||||
# Look through the headers hash for a :params option (case-insensitive,
|
||||
# may be string or symbol). If present and the value is a Hash or
|
||||
# RestClient::ParamsArray, *delete* the key/value pair from the headers
|
||||
# hash and encode the value into a query string. Append this query string
|
||||
# to the URL and return the resulting URL.
|
||||
#
|
||||
# @param [String] url
|
||||
# @param [Hash] headers An options/headers hash to process. Mutation
|
||||
# warning: the params key may be removed if present!
|
||||
#
|
||||
# @return [String] resulting url with query string
|
||||
#
|
||||
def process_url_params(url, headers)
|
||||
url_params = nil
|
||||
|
||||
# find and extract/remove "params" key if the value is a Hash/ParamsArray
|
||||
headers.delete_if do |key, value|
|
||||
if 'params' == key.to_s.downcase && value.is_a?(Hash)
|
||||
url_params.merge! value
|
||||
if key.to_s.downcase == 'params' &&
|
||||
(value.is_a?(Hash) || value.is_a?(RestClient::ParamsArray))
|
||||
if url_params
|
||||
raise ArgumentError.new("Multiple 'params' options passed")
|
||||
end
|
||||
url_params = value
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
unless url_params.empty?
|
||||
# build resulting URL with query string
|
||||
if url_params && !url_params.empty?
|
||||
query_string = RestClient::Utils.encode_query_string(url_params)
|
||||
|
||||
if url.include?('?')
|
||||
|
|
|
@ -1173,5 +1173,11 @@ describe RestClient::Request, :include_helpers do
|
|||
'&nested[key+%2B+escaped]=value+%2B+escaped&nested[other]' \
|
||||
'&nested[arr][]=1&nested[arr][]=2'
|
||||
end
|
||||
|
||||
it 'should handle ParamsArray objects' do
|
||||
@request.process_url_params('https://example.com/',
|
||||
params: RestClient::ParamsArray.new([[:foo, 1], [:foo, 2]])
|
||||
).should eq 'https://example.com/?foo=1&foo=2'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue