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

Raise an error when requests params are invalid #874

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/stripe/api_operations/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module APIOperations
module Request
module ClassMethods
def request(method, url, params = {}, opts = {})
params ||= {}

error_on_invalid_params(params)
warn_on_opts_in_params(params)

opts = Util.normalize_opts(opts)
Expand Down Expand Up @@ -47,6 +50,14 @@ def request(method, url, params = {}, opts = {})
end
end

private def error_on_invalid_params(params)
return if params.nil? || params.is_a?(Hash)

raise ArgumentError,
"request params should be either a Hash or nil " \
"(was a #{params.class})"
end

private def warn_on_opts_in_params(params)
Util::OPTS_USER_SPECIFIED.each do |opt|
if params.key?(opt)
Expand Down
16 changes: 16 additions & 0 deletions test/stripe/api_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ class NestedTestAPIResource < APIResource
end
end

should "error if the params is not a Hash" do
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/capture")
.to_return(body: JSON.generate(charge_fixture))

e = assert_raises(ArgumentError) { Stripe::Charge.capture("ch_123", "sk_test_secret") }

assert_equal "request params should be either a Hash or nil (was a String)", e.message
end

should "allow making a request with params set to nil" do
stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/capture")
.to_return(body: JSON.generate(charge_fixture))

Stripe::Charge.capture("ch_123", nil, "sk_test_secret")
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))
Expand Down