diff --git a/lib/httparty/parser.rb b/lib/httparty/parser.rb index 4e1198d..e447e57 100644 --- a/lib/httparty/parser.rb +++ b/lib/httparty/parser.rb @@ -98,7 +98,9 @@ module HTTParty # @return [Object] the parsed body # @return [nil] when the response body is nil, an empty string, spaces only or "null" def parse - return nil if body.nil? || body.strip.empty? || body == "null" + return nil if body.nil? + return nil if body == "null" + return nil if body.valid_encoding? && body.strip.empty? if supports_format? parse_supported_format else diff --git a/spec/httparty/parser_spec.rb b/spec/httparty/parser_spec.rb index 3c7f413..931c08b 100644 --- a/spec/httparty/parser_spec.rb +++ b/spec/httparty/parser_spec.rb @@ -97,6 +97,12 @@ RSpec.describe HTTParty::Parser do allow(@parser).to receive_messages(body: " ") expect(@parser.parse).to be_nil end + + it "does not raise exceptions for bodies with invalid encodings" do + allow(@parser).to receive_messages(body: "\x80") + allow(@parser).to receive_messages(supports_format?: false) + expect(@parser.parse).to_not be_nil + end end describe "#supports_format?" do