Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Handle a nested array of hashes in OAuth::Helper.normalize #221

Merged
merged 2 commits into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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