stripe-ruby/test/stripe/util_test.rb
Brandur a48fd12c70 Add regression suite for testing parameter encoding
Pulls the test suite out of #319 so that we can get some coverage around
parameter encoding. This should prevent any recurrence of #318.

Also includes a little bit of refactoring.
2015-10-06 14:04:02 -07:00

68 lines
1.7 KiB
Ruby

require File.expand_path('../../test_helper', __FILE__)
module Stripe
class UtilTest < Test::Unit::TestCase
should "#encode_parameters should prepare parameters for an HTTP request" do
params = {
:a => 3,
:b => "+foo?",
:c => "bar&baz",
:d => { :a => "a", :b => "b" },
:e => [0, 1],
}
assert_equal(
"a=3&b=%2Bfoo%3F&c=bar%26baz&d[a]=a&d[b]=b&e[]=0&e[]=1",
Stripe::Util.encode_parameters(params)
)
end
should "#flatten_params should encode parameters according to Rails convention" do
params = {
:a => 3,
:b => "foo?",
:c => "bar&baz",
:d => { :a => "a", :b => "b" },
:e => [0, 1],
}
assert_equal([
["a", 3],
["b", "foo?"],
["c", "bar&baz"],
["d[a]", "a"],
["d[b]", "b"],
["e[]", 0],
["e[]", 1],
], Stripe::Util.flatten_params(params))
end
should "#symbolize_names should convert names to symbols" do
start = {
'foo' => 'bar',
'array' => [{ 'foo' => 'bar' }],
'nested' => {
1 => 2,
:symbol => 9,
'string' => nil
}
}
finish = {
:foo => 'bar',
:array => [{ :foo => 'bar' }],
:nested => {
1 => 2,
:symbol => 9,
:string => nil
}
}
symbolized = Stripe::Util.symbolize_names(start)
assert_equal(finish, symbolized)
end
should "#normalize_opts should reject nil keys" do
assert_raise { Stripe::Util.normalize_opts(nil) }
assert_raise { Stripe::Util.normalize_opts(:api_key => nil) }
end
end
end