Merge pull request #455 from stripe/brandur-header-like-headers

Move away from rest-client's "symbol header names"
This commit is contained in:
Brandur 2016-08-26 10:19:00 -07:00 committed by GitHub
commit d61c1b1211
4 changed files with 23 additions and 17 deletions

View File

@ -303,24 +303,24 @@ module Stripe
def self.request_headers(api_key, method)
headers = {
:user_agent => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
:authorization => "Bearer #{api_key}",
:content_type => 'application/x-www-form-urlencoded'
'User-Agent' => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
'Authorization' => "Bearer #{api_key}",
'Content-Type' => 'application/x-www-form-urlencoded'
}
# It is only safe to retry network failures on post and delete
# requests if we add an Idempotency-Key header
if [:post, :delete].include?(method) && self.max_network_retries > 0
headers[:idempotency_key] ||= SecureRandom.uuid
headers['Idempotency-Key'] ||= SecureRandom.uuid
end
headers[:stripe_version] = api_version if api_version
headers[:stripe_account] = stripe_account if stripe_account
headers['Stripe-Version'] = api_version if api_version
headers['Stripe-Account'] = stripe_account if stripe_account
begin
headers.update(:x_stripe_client_user_agent => JSON.generate(user_agent))
headers.update('X-Stripe-Client-User-Agent' => JSON.generate(user_agent))
rescue => e
headers.update(:x_stripe_client_raw_user_agent => user_agent.inspect,
headers.update('X-Stripe-Client-Raw-User-Agent' => user_agent.inspect,
:error => "#{e} (#{e.class})")
end
end

View File

@ -174,7 +174,7 @@ module Stripe
context "with no global API key set" do
should "use the per-object credential when creating" do
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:authorization] == 'Bearer sk_test_local'
opts[:headers]['Authorization'] == 'Bearer sk_test_local'
end.returns(make_response(make_charge))
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
@ -193,7 +193,7 @@ module Stripe
should "use the per-object credential when creating" do
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:authorization] == 'Bearer local'
opts[:headers]['Authorization'] == 'Bearer local'
end.returns(make_response(make_charge))
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
@ -203,11 +203,11 @@ module Stripe
should "use the per-object credential when retrieving and making other calls" do
Stripe.expects(:execute_request).with do |opts|
opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge" &&
opts[:headers][:authorization] == 'Bearer local'
opts[:headers]['Authorization'] == 'Bearer local'
end.returns(make_response(make_charge))
Stripe.expects(:execute_request).with do |opts|
opts[:url] == "#{Stripe.api_base}/v1/charges/ch_test_charge/refunds" &&
opts[:headers][:authorization] == 'Bearer local'
opts[:headers]['Authorization'] == 'Bearer local'
end.returns(make_response(make_refund))
ch = Stripe::Charge.retrieve('ch_test_charge', 'local')
@ -405,7 +405,7 @@ module Stripe
should "updating should use the supplied api_key" do
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:authorization] == 'Bearer sk_test_local'
opts[:headers]['Authorization'] == 'Bearer sk_test_local'
end.returns(make_response(make_customer))
c = Stripe::Customer.new
c.save({}, { :api_key => 'sk_test_local' })
@ -725,7 +725,7 @@ module Stripe
should 'ensure there is always an idempotency_key on POST requests' do
SecureRandom.expects(:uuid).at_least_once.returns("random_key")
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:idempotency_key] == "random_key"
opts[:headers]['Idempotency-Key'] == "random_key"
end.returns(make_response({"id" => "myid"}))
Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
@ -734,7 +734,7 @@ module Stripe
should 'ensure there is always an idempotency_key on DELETE requests' do
SecureRandom.expects(:uuid).at_least_once.returns("random_key")
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:idempotency_key] == "random_key"
opts[:headers]['Idempotency-Key'] == "random_key"
end.returns(make_response({"id" => "myid"}))
c = Stripe::Customer.construct_from(make_customer)
@ -742,6 +742,12 @@ module Stripe
end
should 'not override a provided idempotency_key' do
# Note that this expectation looks like `:idempotency_key` instead of
# the header `Idempotency-Key` because it's user provided as seen
# below. The ones injected by the library itself look like headers
# (`Idempotency-Key`), but rest-client does allow this symbol
# formatting and will properly override the system generated one as
# expected.
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:idempotency_key] == "provided_key"
end.returns(make_response({"id" => "myid"}))

View File

@ -38,7 +38,7 @@ module Stripe
Stripe.expects(:execute_request).with do |opts|
opts[:url] == "#{Stripe.api_base}/v1/invoices/in_test_invoice/pay" &&
opts[:method] == :post &&
opts[:headers][:authorization] == 'Bearer foobar'
opts[:headers]['Authorization'] == 'Bearer foobar'
end.returns(make_response(make_paid_invoice))
i.pay

View File

@ -43,7 +43,7 @@ class StripeTest < Test::Unit::TestCase
Stripe.stripe_account = 'acct_1234'
Stripe.expects(:execute_request).with(
has_entry(:headers, has_entry(:stripe_account, 'acct_1234')),
has_entry(:headers, has_entry('Stripe-Account', 'acct_1234')),
).returns(make_response(response))
Stripe.request(:post, '/v1/account', 'sk_live12334566')