Skip to content

Commit

Permalink
🔀 Merge pull request #221 from oauth-xx/issue/80-handle-nested-array-…
Browse files Browse the repository at this point in the history
…of-hashes-in-params

✨ Handle a nested array of hashes in OAuth::Helper.normalize
  • Loading branch information
pboling committed Nov 1, 2021
2 parents 99a8d0d + 7d38441 commit 60cc822
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions lib/oauth/helper.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions test/units/test_oauth_helper.rb
Expand Up @@ -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"}))
Expand Down

0 comments on commit 60cc822

Please sign in to comment.