mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-10-06 00:02:18 -04:00
Replaces my original attempt in #319 in a way that doesn't depend on `URI.encode_www_form` which doesn't exist in 1.8.7. This should hopefully get us the best of all worlds. Caveats around use of `+` instead of `%20` as detailed in #319 still apply. Fixes #286.
76 lines
2.0 KiB
Ruby
76 lines
2.0 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 "#url_encode should prepare strings for HTTP" do
|
|
assert_equal "foo", Stripe::Util.url_encode("foo")
|
|
assert_equal "foo", Stripe::Util.url_encode(:foo)
|
|
assert_equal "foo%2B", Stripe::Util.url_encode("foo+")
|
|
assert_equal "foo%26", Stripe::Util.url_encode("foo&")
|
|
assert_equal "foo[bar]", Stripe::Util.url_encode("foo[bar]")
|
|
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
|