diff --git a/lib/stripe/api_operations/request.rb b/lib/stripe/api_operations/request.rb index 6abe838f0..579da7c95 100644 --- a/lib/stripe/api_operations/request.rb +++ b/lib/stripe/api_operations/request.rb @@ -8,6 +8,8 @@ def request(method, url, params = {}, opts = {}) warn_on_opts_in_params(params) opts = Util.normalize_opts(opts) + error_on_non_string_user_opts(opts) + opts[:client] ||= StripeClient.active_client headers = opts.clone @@ -31,10 +33,24 @@ def request(method, url, params = {}, opts = {}) [resp, opts_to_persist] end + private def error_on_non_string_user_opts(opts) + Util::OPTS_USER_SPECIFIED.each do |opt| + next unless opts.key?(opt) + + val = opts[opt] + next if val.nil? + next if val.is_a?(String) + + raise ArgumentError, + "request option '#{opt}' should be a string value " \ + "(was a #{val.class})" + end + end + private def warn_on_opts_in_params(params) Util::OPTS_USER_SPECIFIED.each do |opt| if params.key?(opt) - warn("WARNING: #{opt} should be in opts instead of params.") + warn("WARNING: '#{opt}' should be in opts instead of params.") end end end diff --git a/test/stripe/api_resource_test.rb b/test/stripe/api_resource_test.rb index 82b4a3bca..c0dfbc163 100644 --- a/test/stripe/api_resource_test.rb +++ b/test/stripe/api_resource_test.rb @@ -227,6 +227,23 @@ class NestedTestAPIResource < APIResource end end + should "error if a user-specified opt is given a non-nil non-string value" do + stub_request(:post, "#{Stripe.api_base}/v1/charges") + .to_return(body: JSON.generate(charge_fixture)) + + # Works fine if not included or a string. + Stripe::Charge.create({ amount: 100, currency: "usd" }, {}) + Stripe::Charge.create({ amount: 100, currency: "usd" }, idempotency_key: "12345") + + # Errors on a non-string. + e = assert_raises(ArgumentError) do + Stripe::Charge.create({ amount: 100, currency: "usd" }, idempotency_key: :foo) + end + assert_equal "request option 'idempotency_key' should be a string value " \ + "(was a Symbol)", + e.message + end + should "requesting with a unicode ID should result in a request" do stub_request(:get, "#{Stripe.api_base}/v1/customers/%E2%98%83") .to_return(body: JSON.generate(make_missing_id_error), status: 404)