Encoding POST bodies ourselves, instead of letting rest-client

It's a bit unfortunate we have to do this, but this avoids an even
more unfortunate interaction between rest-client and hashery that
prevents passing a hash as the payload.

Also update the tests appropriately. This is also a bit messy, but I
didn't want to pull in Rack just for a handful of tests.

Fixes #38
This commit is contained in:
Evan Broder 2012-09-14 20:10:43 -07:00
parent ae850157d7
commit e8adad2c34
2 changed files with 23 additions and 10 deletions

View File

@ -121,7 +121,7 @@ module Stripe
end
payload = nil
else
payload = params
payload = Util.flatten_params(params).collect{|(key, value)| "#{key}=#{Util.url_encode(value)}"}.join('&')
end
begin
@ -135,7 +135,8 @@ module Stripe
headers = {
:user_agent => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
:authorization => "Bearer #{api_key}"
:authorization => "Bearer #{api_key}",
:content_type => 'application/x-www-form-urlencoded'
}.merge(headers)
opts = {
:method => method,

View File

@ -177,7 +177,9 @@ class TestStripeRuby < Test::Unit::TestCase
end.returns(test_response({ :count => 1, :data => [test_charge] }))
c = Stripe::Charge.all(:count => nil, :offset => 5, :sad => false)
@mock.expects(:post).with('https://api.stripe.com/v1/charges', nil, { :amount => 50, :currency => 'usd', :card => {} }).returns(test_response({ :count => 1, :data => [test_charge] }))
@mock.expects(:post).with do |url, api_key, params|
url == 'https://api.stripe.com/v1/charges' && api_key.nil? && CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
end.returns(test_response({ :count => 1, :data => [test_charge] }))
c = Stripe::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
end
@ -201,7 +203,9 @@ class TestStripeRuby < Test::Unit::TestCase
should "making a POST request with parameters should have a body and no query string" do
params = { :amount => 100, :currency => 'usd', :card => 'sc_token' }
@mock.expects(:post).once.with { |url, get, post| get.nil? and post == params }.returns(test_response(test_charge))
@mock.expects(:post).once.with do |url, get, post|
get.nil? && CGI.parse(post) == {'amount' => ['100'], 'currency' => ['usd'], 'card' => ['sc_token']}
end.returns(test_response(test_charge))
c = Stripe::Charge.create(params)
end
@ -228,7 +232,9 @@ class TestStripeRuby < Test::Unit::TestCase
end
should "updating an object should issue a POST request with only the changed properties" do
@mock.expects(:post).with("https://api.stripe.com/v1/customers/c_test_customer", nil, {:mnemonic => 'another_mn'}).once.returns(test_response(test_customer))
@mock.expects(:post).with do |url, api_key, params|
url == "https://api.stripe.com/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'mnemonic' => ['another_mn']}
end.once.returns(test_response(test_customer))
c = Stripe::Customer.construct_from(test_customer)
c.mnemonic = "another_mn"
c.save
@ -321,10 +327,14 @@ class TestStripeRuby < Test::Unit::TestCase
end
should "execute should return a new, fully executed charge when passed correct parameters" do
@mock.expects(:post).with('https://api.stripe.com/v1/charges', nil, {
:currency => 'usd', :amount => 100,
:card => {:exp_year => 2012, :number => '4242424242424242', :exp_month => 11}
}).once.returns(test_response(test_charge))
@mock.expects(:post).with do |url, api_key, params|
url == 'https://api.stripe.com/v1/charges' && api_key.nil? && CGI.parse(params) == {
'currency' => ['usd'], 'amount' => ['100'],
'card[exp_year]' => ['2012'],
'card[number]' => ['4242424242424242'],
'card[exp_month]' => ['11']
}
end.once.returns(test_response(test_charge))
c = Stripe::Charge.create({
:amount => 100,
@ -382,7 +392,9 @@ class TestStripeRuby < Test::Unit::TestCase
@mock.expects(:get).once.returns(test_response(test_customer))
c = Stripe::Customer.retrieve("test_customer")
@mock.expects(:post).once.with("https://api.stripe.com/v1/customers/c_test_customer/subscription", nil, {:plan => 'silver'}).returns(test_response(test_subscription('silver')))
@mock.expects(:post).once.with do |url, api_key, params|
url == "https://api.stripe.com/v1/customers/c_test_customer/subscription" && api_key.nil? && CGI.parse(params) == {'plan' => ['silver']}
end.returns(test_response(test_subscription('silver')))
s = c.update_subscription({:plan => 'silver'})
assert_equal 'subscription', s.object