diff --git a/CHANGELOG.md b/CHANGELOG.md index b4c0b5c7..14d20978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added CODE_OF_CONDUCT.md (#217, #218 by @pboling) * Added FUNDING.yml (#217, #218 by @pboling) * Added Client Certificate Options: :ssl_client_cert and :ssl_client_key (#136, #220 by @pboling) +* Handle a nested array of hashes in OAuth::Helper.normalize (#80, #221 by @pboling) ### Changed diff --git a/lib/oauth/helper.rb b/lib/oauth/helper.rb index defb8c7f..1b04bd72 100644 --- a/lib/oauth/helper.rb +++ b/lib/oauth/helper.rb @@ -47,8 +47,12 @@ def normalize(params) # make sure the array has an element so we don't lose the key values << nil if values.empty? # multiple values were provided for a single key - values.sort.collect do |v| - [escape(k),escape(v)] * "=" + if values[0].is_a?(Hash) + normalize_nested_query(values, k) + else + values.sort.collect do |v| + [escape(k),escape(v)] * "=" + end end elsif values.is_a?(Hash) normalize_nested_query(values, k) @@ -58,7 +62,7 @@ def normalize(params) end * "&" end - #Returns a string representation of the Hash like in URL query string + # Returns a string representation of the Hash like in URL query string # build_nested_query({:level_1 => {:level_2 => ['value_1','value_2']}}, 'prefix')) # #=> ["prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_1", "prefix%5Blevel_1%5D%5Blevel_2%5D%5B%5D=value_2"] def normalize_nested_query(value, prefix = nil) diff --git a/test/units/test_oauth_helper.rb b/test/units/test_oauth_helper.rb index 5ddcbcc1..bb4852f3 100644 --- a/test/units/test_oauth_helper.rb +++ b/test/units/test_oauth_helper.rb @@ -82,6 +82,20 @@ def test_normalize assert_equal("oauth_consumer_key=vince_clortho&oauth_nonce=nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1240004133&oauth_token=token_value&oauth_version=1.0&weight%5Bvalue%5D=65", OAuth::Helper.normalize(params)) end + def test_normalize_with_nested_array_of_hashes + params = { + "oauth_nonce" => "nonce", + "weight" => { :value => "65" }, + "items" => [{"a" => 1}, {"b" => 2}], + "oauth_signature_method" => "HMAC-SHA1", + "oauth_timestamp" => "1240004133", + "oauth_consumer_key" => "vince_clortho", + "oauth_token" => "token_value", + "oauth_version" => "1.0" + } + assert_equal("items%5B%5D%5Ba%5D=1&items%5B%5D%5Bb%5D=2&oauth_consumer_key=vince_clortho&oauth_nonce=nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1240004133&oauth_token=token_value&oauth_version=1.0&weight%5Bvalue%5D=65", OAuth::Helper.normalize(params)) + end + def test_normalize_nested_query assert_equal([], OAuth::Helper.normalize_nested_query({})) assert_equal(["foo=bar"], OAuth::Helper.normalize_nested_query({:foo => "bar"}))