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

Allow empty parameters. #155

Merged
merged 2 commits into from Jan 20, 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
10 changes: 8 additions & 2 deletions lib/oauth/client/helper.rb
Expand Up @@ -27,7 +27,7 @@ def timestamp
end

def oauth_parameters
{
out = {
'oauth_body_hash' => options[:body_hash],
'oauth_callback' => options[:oauth_callback],
'oauth_consumer_key' => options[:consumer].key,
Expand All @@ -38,7 +38,13 @@ def oauth_parameters
'oauth_verifier' => options[:oauth_verifier],
'oauth_version' => (options[:oauth_version] || '1.0'),
'oauth_session_handle' => options[:oauth_session_handle]
}.reject { |k,v| v.to_s == "" }
}
allowed_empty_params = options[:allow_empty_params]
if allowed_empty_params != true && !allowed_empty_params.kind_of?(Array)
allowed_empty_params = allowed_empty_params == false ? [] : [allowed_empty_params]
end
out.select! { |k,v| v.to_s != '' || allowed_empty_params == true || allowed_empty_params.include?(k) }
out
end

def signature(extra_options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/oauth/version.rb
@@ -1,3 +1,3 @@
module OAuth
VERSION = "0.5.4"
VERSION = "0.5.5"
end
147 changes: 147 additions & 0 deletions test/units/test_client_helper.rb
@@ -0,0 +1,147 @@
require File.expand_path('../../test_helper', __FILE__)

require 'oauth/client'

class ClientHelperTest < Minitest::Test

def setup
@consumer=OAuth::Consumer.new(
'consumer_key_86cad9', '5888bf0345e5d237',
{
:site=>"http://blabla.bla",
:proxy=>"http://user:password@proxy.bla:8080",
:request_token_path=>"/oauth/example/request_token.php",
:access_token_path=>"/oauth/example/access_token.php",
:authorize_path=>"/oauth/example/authorize.php",
:scheme=>:header,
:http_method=>:get
})
end

def test_oauth_parameters_allow_empty_params_default
helper = OAuth::Client::Helper.new(nil, {
:consumer => @consumer
})
helper.stub :timestamp, '0' do
helper.stub :nonce, 'nonce' do
expected = {
"oauth_consumer_key"=>"consumer_key_86cad9",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_timestamp"=>"0",
"oauth_nonce"=>"nonce",
"oauth_version"=>"1.0"
}
assert_equal expected, helper.oauth_parameters
end
end
end

def test_oauth_parameters_allow_empty_params_true
input = true
helper = OAuth::Client::Helper.new(nil, {
:consumer => @consumer,
:allow_empty_params => input
})
helper.stub :timestamp, '0' do
helper.stub :nonce, 'nonce' do
expected = {
"oauth_body_hash"=>nil,
"oauth_callback"=>nil,
"oauth_consumer_key"=>"consumer_key_86cad9",
"oauth_token"=>"",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_timestamp"=>"0",
"oauth_nonce"=>"nonce",
"oauth_verifier"=>nil,
"oauth_version"=>"1.0",
"oauth_session_handle"=>nil
}
assert_equal expected, helper.oauth_parameters
end
end
end

def test_oauth_parameters_allow_empty_params_false
input = false
helper = OAuth::Client::Helper.new(nil, {
:consumer => @consumer,
:allow_empty_params => input
})
helper.stub :timestamp, '0' do
helper.stub :nonce, 'nonce' do
expected = {
"oauth_consumer_key"=>"consumer_key_86cad9",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_timestamp"=>"0",
"oauth_nonce"=>"nonce",
"oauth_version"=>"1.0"
}
assert_equal expected, helper.oauth_parameters
end
end
end

def test_oauth_parameters_allow_empty_params_only_oauth_token_as_string
input = 'oauth_token'
helper = OAuth::Client::Helper.new(nil, {
:consumer => @consumer,
:allow_empty_params => input
})
helper.stub :timestamp, '0' do
helper.stub :nonce, 'nonce' do
expected = {
"oauth_consumer_key"=>"consumer_key_86cad9",
"oauth_token"=>"",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_timestamp"=>"0",
"oauth_nonce"=>"nonce",
"oauth_version"=>"1.0",
}
assert_equal expected, helper.oauth_parameters
end
end
end

def test_oauth_parameters_allow_empty_params_only_oauth_token_as_array
input = ['oauth_token']
helper = OAuth::Client::Helper.new(nil, {
:consumer => @consumer,
:allow_empty_params => input
})
helper.stub :timestamp, '0' do
helper.stub :nonce, 'nonce' do
expected = {
"oauth_consumer_key"=>"consumer_key_86cad9",
"oauth_token"=>"",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_timestamp"=>"0",
"oauth_nonce"=>"nonce",
"oauth_version"=>"1.0",
}
assert_equal expected, helper.oauth_parameters
end
end
end

def test_oauth_parameters_allow_empty_params_oauth_token_and_oauth_session_handle
input = ['oauth_token', 'oauth_session_handle']
helper = OAuth::Client::Helper.new(nil, {
:consumer => @consumer,
:allow_empty_params => input
})
helper.stub :timestamp, '0' do
helper.stub :nonce, 'nonce' do
expected = {
"oauth_consumer_key"=>"consumer_key_86cad9",
"oauth_token"=>"",
"oauth_signature_method"=>"HMAC-SHA1",
"oauth_timestamp"=>"0",
"oauth_nonce"=>"nonce",
"oauth_version"=>"1.0",
"oauth_session_handle"=>nil
}
assert_equal expected, helper.oauth_parameters
end
end
end
end