1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

[#533] Prevent gsub errors with different encodings

This commit is contained in:
Rick Sullivan 2017-05-17 10:33:50 -07:00
parent 1c9b41140c
commit 2f8093871b
2 changed files with 11 additions and 1 deletions

View file

@ -101,6 +101,9 @@ module HTTParty
return nil if body.nil?
return nil if body == "null"
return nil if body.valid_encoding? && body.strip.empty?
if body.valid_encoding? && body.encoding == Encoding::UTF_8
body.gsub!(/\A#{UTF8_BOM}/, '')
end
if supports_format?
parse_supported_format
else
@ -117,7 +120,7 @@ module HTTParty
UTF8_BOM = "\xEF\xBB\xBF".freeze
def json
JSON.parse(body.gsub(/\A#{UTF8_BOM}/, ''), :quirks_mode => true, :allow_nan => true)
JSON.parse(body, :quirks_mode => true, :allow_nan => true)
end
def csv

View file

@ -108,6 +108,13 @@ RSpec.describe HTTParty::Parser do
allow(@parser).to receive_messages(body: "\xEF\xBB\xBF\{\"hi\":\"yo\"\}")
expect(@parser.parse).to eq({"hi"=>"yo"})
end
it "parses ascii 8bit encoding" do
allow(@parser).to receive_messages(
body: "{\"currency\":\"\xE2\x82\xAC\"}".force_encoding('ASCII-8BIT')
)
expect(@parser.parse).to eq({"currency" => ""})
end
end
describe "#supports_format?" do