Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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