mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-15 00:01:36 -04:00
Fix passing ActiveSupport::SafeBuffer to param encoders
Simply re-use `Faraday::Utils.escape/unescape` instead of reimplementing them for each encoder.
This commit is contained in:
parent
df9d5c31c5
commit
731f49039d
@ -1,15 +1,10 @@
|
||||
require "forwardable"
|
||||
|
||||
module Faraday
|
||||
module NestedParamsEncoder
|
||||
ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/
|
||||
|
||||
def self.escape(s)
|
||||
s.to_s.gsub(ESCAPE_RE) {|match|
|
||||
'%' + match.unpack('H2' * match.bytesize).join('%').upcase
|
||||
}.tr(' ', '+')
|
||||
end
|
||||
|
||||
def self.unescape(s)
|
||||
CGI.unescape(s.to_s)
|
||||
class << self
|
||||
extend Forwardable
|
||||
def_delegators :'Faraday::Utils', :escape, :unescape
|
||||
end
|
||||
|
||||
def self.encode(params)
|
||||
@ -117,16 +112,9 @@ module Faraday
|
||||
end
|
||||
|
||||
module FlatParamsEncoder
|
||||
ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/
|
||||
|
||||
def self.escape(s)
|
||||
return s.to_s.gsub(ESCAPE_RE) {
|
||||
'%' + $&.unpack('H2' * $&.bytesize).join('%').upcase
|
||||
}.tr(' ', '+')
|
||||
end
|
||||
|
||||
def self.unescape(s)
|
||||
CGI.unescape(s.to_s)
|
||||
class << self
|
||||
extend Forwardable
|
||||
def_delegators :'Faraday::Utils', :escape, :unescape
|
||||
end
|
||||
|
||||
def self.encode(params)
|
||||
|
24
test/parameters_test.rb
Normal file
24
test/parameters_test.rb
Normal file
@ -0,0 +1,24 @@
|
||||
require File.expand_path("../helper", __FILE__)
|
||||
|
||||
class TestParameters < Faraday::TestCase
|
||||
# emulates ActiveSupport::SafeBuffer#gsub
|
||||
FakeSafeBuffer = Struct.new(:string) do
|
||||
def to_s() self end
|
||||
def gsub(regex)
|
||||
string.gsub(regex) {
|
||||
match, = $&, '' =~ /a/
|
||||
yield(match)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def test_escaping_safe_buffer_nested
|
||||
monies = FakeSafeBuffer.new("$32,000.00")
|
||||
assert_equal "a=%2432%2C000.00", Faraday::NestedParamsEncoder.encode("a" => monies)
|
||||
end
|
||||
|
||||
def test_escaping_safe_buffer_flat
|
||||
monies = FakeSafeBuffer.new("$32,000.00")
|
||||
assert_equal "a=%2432%2C000.00", Faraday::FlatParamsEncoder.encode("a" => monies)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user