mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-12-15 00:01:08 -05:00
fix: Update logging to coerce ASCII-8BIT into UTF-8. (#1076)
This commit is contained in:
parent
33c820096f
commit
9092e9dbab
@ -416,6 +416,15 @@ module Stripe
|
||||
# Hopefully val is a string, but protect in case it's not.
|
||||
val = val.to_s
|
||||
|
||||
# Some values returned by the server are encoded in ASCII-8BIT before
|
||||
# being parsed as UTF-8 by Marshal. If we don't transform these here, then
|
||||
# puts will fail as it tries to render UTF-8 characters as ASCII-8BIT
|
||||
# which is not valid.
|
||||
if val && val.encoding == Encoding::ASCII_8BIT
|
||||
# Dup the string as it is a frozen literal.
|
||||
val = val.dup.force_encoding("UTF-8")
|
||||
end
|
||||
|
||||
if %r{[^\w\-/]} =~ val
|
||||
# If the string contains any special characters, escape any double
|
||||
# quotes it has, remove newlines, and wrap the whole thing in quotes.
|
||||
|
||||
@ -397,6 +397,13 @@ module Stripe
|
||||
should "not error if given a non-string" do
|
||||
assert_equal "true", Util.send(:wrap_logfmt_value, true)
|
||||
end
|
||||
|
||||
should "handle UTF-8 characters encoded in ASCII-8BIT" do
|
||||
expected_utf8_str = "\"é\"".dup.force_encoding(Encoding::UTF_8.name)
|
||||
ascii_8bit_str = "é".dup.force_encoding(Encoding::ASCII_8BIT.name)
|
||||
|
||||
assert_equal expected_utf8_str, Util.send(:wrap_logfmt_value, ascii_8bit_str)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user