Skip to content

Commit

Permalink
revert #540
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen authored and ixti committed May 26, 2021
1 parent a92306b commit 40d29cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def chunked?
# @param type [#to_s] Parse as given MIME type.
# @raise (see MimeType.[])
# @return [Object]
def parse(type)
MimeType[type].decode to_s
def parse(type = nil)
MimeType[type || mime_type].decode to_s
end

# Inspect a response
Expand Down
27 changes: 20 additions & 7 deletions spec/lib/http/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,32 @@
end

describe "#parse" do
let(:headers) { {"Content-Type" => "application/json"} }
let(:headers) { {"Content-Type" => content_type} }
let(:body) { '{"foo":"bar"}' }

it "fails if MIME type decoder is not found" do
expect { response.parse "text/html" }.to raise_error(HTTP::Error)
context "with known content type" do
let(:content_type) { "application/json" }
it "returns parsed body" do
expect(response.parse).to eq "foo" => "bar"
end
end

it "uses decoder found by given MIME type" do
expect(response.parse("application/json")).to eq("foo" => "bar")
context "with unknown content type" do
let(:content_type) { "application/deadbeef" }
it "raises HTTP::Error" do
expect { response.parse }.to raise_error HTTP::Error
end
end

it "uses decoder found by given MIME type alias" do
expect(response.parse(:json)).to eq("foo" => "bar")
context "with explicitly given mime type" do
let(:content_type) { "application/deadbeef" }
it "ignores mime_type of response" do
expect(response.parse("application/json")).to eq "foo" => "bar"
end

it "supports mime type aliases" do
expect(response.parse(:json)).to eq "foo" => "bar"
end
end
end

Expand Down

0 comments on commit 40d29cc

Please sign in to comment.