mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Use non-destructive gsub, fix test stubbing
This commit is contained in:
parent
2f8093871b
commit
7198a89da1
2 changed files with 31 additions and 26 deletions
|
@ -102,7 +102,7 @@ module HTTParty
|
||||||
return nil if body == "null"
|
return nil if body == "null"
|
||||||
return nil if body.valid_encoding? && body.strip.empty?
|
return nil if body.valid_encoding? && body.strip.empty?
|
||||||
if body.valid_encoding? && body.encoding == Encoding::UTF_8
|
if body.valid_encoding? && body.encoding == Encoding::UTF_8
|
||||||
body.gsub!(/\A#{UTF8_BOM}/, '')
|
@body = body.gsub(/\A#{UTF8_BOM}/, '')
|
||||||
end
|
end
|
||||||
if supports_format?
|
if supports_format?
|
||||||
parse_supported_format
|
parse_supported_format
|
||||||
|
|
|
@ -63,57 +63,62 @@ RSpec.describe HTTParty::Parser do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#parse" do
|
describe "#parse" do
|
||||||
before do
|
|
||||||
@parser = HTTParty::Parser.new('body', :json)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "attempts to parse supported formats" do
|
it "attempts to parse supported formats" do
|
||||||
allow(@parser).to receive_messages(supports_format?: true)
|
parser = HTTParty::Parser.new('body', :json)
|
||||||
expect(@parser).to receive(:parse_supported_format)
|
allow(parser).to receive_messages(supports_format?: true)
|
||||||
@parser.parse
|
|
||||||
|
expect(parser).to receive(:parse_supported_format)
|
||||||
|
parser.parse
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the unparsed body when the format is unsupported" do
|
it "returns the unparsed body when the format is unsupported" do
|
||||||
allow(@parser).to receive_messages(supports_format?: false)
|
parser = HTTParty::Parser.new('body', :json)
|
||||||
expect(@parser.parse).to eq(@parser.body)
|
allow(parser).to receive_messages(supports_format?: false)
|
||||||
|
|
||||||
|
expect(parser.parse).to eq(parser.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil for an empty body" do
|
it "returns nil for an empty body" do
|
||||||
allow(@parser).to receive_messages(body: '')
|
parser = HTTParty::Parser.new('', :json)
|
||||||
expect(@parser.parse).to be_nil
|
expect(parser.parse).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil for a nil body" do
|
it "returns nil for a nil body" do
|
||||||
allow(@parser).to receive_messages(body: nil)
|
parser = HTTParty::Parser.new(nil, :json)
|
||||||
expect(@parser.parse).to be_nil
|
expect(parser.parse).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil for a 'null' body" do
|
it "returns nil for a 'null' body" do
|
||||||
allow(@parser).to receive_messages(body: "null")
|
parser = HTTParty::Parser.new("null", :json)
|
||||||
expect(@parser.parse).to be_nil
|
expect(parser.parse).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil for a body with spaces only" do
|
it "returns nil for a body with spaces only" do
|
||||||
allow(@parser).to receive_messages(body: " ")
|
parser = HTTParty::Parser.new(" ", :json)
|
||||||
expect(@parser.parse).to be_nil
|
expect(parser.parse).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not raise exceptions for bodies with invalid encodings" do
|
it "does not raise exceptions for bodies with invalid encodings" do
|
||||||
allow(@parser).to receive_messages(body: "\x80")
|
parser = HTTParty::Parser.new("\x80", :invalid_format)
|
||||||
allow(@parser).to receive_messages(supports_format?: false)
|
expect(parser.parse).to_not be_nil
|
||||||
expect(@parser.parse).to_not be_nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "ignores utf-8 bom" do
|
it "ignores utf-8 bom" do
|
||||||
allow(@parser).to receive_messages(body: "\xEF\xBB\xBF\{\"hi\":\"yo\"\}")
|
parser = HTTParty::Parser.new("\xEF\xBB\xBF\{\"hi\":\"yo\"\}", :json)
|
||||||
expect(@parser.parse).to eq({"hi"=>"yo"})
|
expect(parser.parse).to eq({"hi"=>"yo"})
|
||||||
end
|
end
|
||||||
|
|
||||||
it "parses ascii 8bit encoding" do
|
it "parses ascii 8bit encoding" do
|
||||||
allow(@parser).to receive_messages(
|
parser = HTTParty::Parser.new(
|
||||||
body: "{\"currency\":\"\xE2\x82\xAC\"}".force_encoding('ASCII-8BIT')
|
"{\"currency\":\"\xE2\x82\xAC\"}".force_encoding('ASCII-8BIT'),
|
||||||
|
:json
|
||||||
)
|
)
|
||||||
expect(@parser.parse).to eq({"currency" => "€"})
|
expect(parser.parse).to eq({"currency" => "€"})
|
||||||
|
end
|
||||||
|
|
||||||
|
it "parses frozen strings" do
|
||||||
|
parser = HTTParty::Parser.new('{"a":1}'.freeze, :json)
|
||||||
|
expect(parser.parse).to eq("a" => 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue