From db3059a3c066487b3802a1d2af74135c48a40ed6 Mon Sep 17 00:00:00 2001 From: Niels Ganser Date: Fri, 18 Mar 2016 13:30:36 +0100 Subject: [PATCH] Allow options (headers) to be passed into .save in the same manner as is already possible for .create. --- lib/stripe/api_operations/update.rb | 8 ++++++-- test/stripe/api_resource_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/stripe/api_operations/update.rb b/lib/stripe/api_operations/update.rb index 621d89110..5d24d7492 100644 --- a/lib/stripe/api_operations/update.rb +++ b/lib/stripe/api_operations/update.rb @@ -13,7 +13,11 @@ module Update # and includes them in the create or update. If +:req_url:+ is included # in the list, it overrides the update URL used for the create or # update. - def save(params={}) + # * +opts+ - A Hash of additional options (separate from the params / + # object values) to be added to the request. E.g. to allow for an + # idempotency_key to be passed in the request headers, or for the + # api_key to be overwritten. See {APIOperations::Request.request}. + def save(params={}, opts={}) # We started unintentionally (sort of) allowing attributes sent to # +save+ to override values used during the update. So as not to break # the API, this makes that official here. @@ -28,7 +32,7 @@ def save(params={}) # generated a uri for this object with an identifier baked in values.delete(:id) - response, opts = request(:post, save_url, values) + response, opts = request(:post, save_url, values, opts) initialize_from(response, opts) self diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index caf9f5eb4..09a6ebfa4 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -387,6 +387,31 @@ class ApiResourceTest < Test::Unit::TestCase assert_equal false, c.livemode end + should "updating should send along the idempotency-key header" do + Stripe.expects(:execute_request).with do |opts| + opts[:headers][:idempotency_key] == 'bar' + end.returns(make_response(make_customer)) + c = Stripe::Customer.new + c.save({}, { :idempotency_key => 'bar' }) + assert_equal false, c.livemode + end + + should "updating should fail if api_key is overwritten with nil" do + c = Stripe::Customer.new + assert_raises TypeError do + c.save({}, { :api_key => nil }) + end + end + + should "updating should use the supplied api_key" do + Stripe.expects(:execute_request).with do |opts| + opts[:headers][:authorization] == 'Bearer sk_test_local' + end.returns(make_response(make_customer)) + c = Stripe::Customer.new + c.save({}, { :api_key => 'sk_test_local' }) + assert_equal false, c.livemode + end + should "deleting should send no props and result in an object that has no props other deleted" do @mock.expects(:get).never @mock.expects(:post).never