mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-10-11 00:01:46 -04:00
Merge pull request #323 from stripe/brandur-cgi-escape
Replace deprecated `URI.escape` with a customized `CGI.escape`
This commit is contained in:
commit
dd22228f43
@ -1,3 +1,5 @@
|
|||||||
|
require "cgi"
|
||||||
|
|
||||||
module Stripe
|
module Stripe
|
||||||
module Util
|
module Util
|
||||||
def self.objects_to_ids(h)
|
def self.objects_to_ids(h)
|
||||||
@ -98,17 +100,24 @@ module Stripe
|
|||||||
# `&`).
|
# `&`).
|
||||||
def self.encode_parameters(params)
|
def self.encode_parameters(params)
|
||||||
Util.flatten_params(params).
|
Util.flatten_params(params).
|
||||||
map { |k,v| "#{k}=#{Util.url_encode(v)}" }.join('&')
|
map { |k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Encodes a string in a way that makes it suitable for use in a set of
|
||||||
|
# query parameters in a URI or in a set of form parameters in a request
|
||||||
|
# body.
|
||||||
def self.url_encode(key)
|
def self.url_encode(key)
|
||||||
URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
CGI.escape(key.to_s).
|
||||||
|
# Don't use strict form encoding by changing the square bracket control
|
||||||
|
# characters back to their literals. This is fine by the server, and
|
||||||
|
# makes these parameter strings easier to read.
|
||||||
|
gsub('%5B', '[').gsub('%5D', ']')
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.flatten_params(params, parent_key=nil)
|
def self.flatten_params(params, parent_key=nil)
|
||||||
result = []
|
result = []
|
||||||
params.each do |key, value|
|
params.each do |key, value|
|
||||||
calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
|
calculated_key = parent_key ? "#{parent_key}[#{key}]" : "#{key}"
|
||||||
if value.is_a?(Hash)
|
if value.is_a?(Hash)
|
||||||
result += flatten_params(value, calculated_key)
|
result += flatten_params(value, calculated_key)
|
||||||
elsif value.is_a?(Array)
|
elsif value.is_a?(Array)
|
||||||
|
@ -68,7 +68,7 @@ module Stripe
|
|||||||
|
|
||||||
@mock.expects(:post).
|
@mock.expects(:post).
|
||||||
once.
|
once.
|
||||||
with('https://api.stripe.com/v1/accounts/acct_foo', nil, 'legal_entity[address][line1]=2%20Three%20Four&legal_entity[first_name]=Bob').
|
with('https://api.stripe.com/v1/accounts/acct_foo', nil, 'legal_entity[address][line1]=2+Three+Four&legal_entity[first_name]=Bob').
|
||||||
returns(make_response(resp))
|
returns(make_response(resp))
|
||||||
|
|
||||||
a = Stripe::Account.retrieve('acct_foo')
|
a = Stripe::Account.retrieve('acct_foo')
|
||||||
|
@ -230,7 +230,7 @@ module Stripe
|
|||||||
|
|
||||||
should "urlencode values in GET params" do
|
should "urlencode values in GET params" do
|
||||||
response = make_response(make_charge_array)
|
response = make_response(make_charge_array)
|
||||||
@mock.expects(:get).with("#{Stripe.api_base}/v1/charges?customer=test%20customer", nil, nil).returns(response)
|
@mock.expects(:get).with("#{Stripe.api_base}/v1/charges?customer=test+customer", nil, nil).returns(response)
|
||||||
charges = Stripe::Charge.list(:customer => 'test customer').data
|
charges = Stripe::Charge.list(:customer => 'test customer').data
|
||||||
assert charges.kind_of? Array
|
assert charges.kind_of? Array
|
||||||
end
|
end
|
||||||
|
@ -21,9 +21,7 @@ module Stripe
|
|||||||
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%2B", Stripe::Util.url_encode("foo+")
|
||||||
assert_equal "foo%26", Stripe::Util.url_encode("foo&")
|
assert_equal "foo%26", Stripe::Util.url_encode("foo&")
|
||||||
# Actually, we're going to alter the behavior of #url_encode slightly to
|
assert_equal "foo[bar]", Stripe::Util.url_encode("foo[bar]")
|
||||||
# simplify the form encoding path. This does not yet succeed.
|
|
||||||
# assert_equal "foo[bar]", Stripe::Util.url_encode("foo[bar]")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "#flatten_params should encode parameters according to Rails convention" do
|
should "#flatten_params should encode parameters according to Rails convention" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user