Skip to content

Commit

Permalink
Add HTTP::Response#content_length
Browse files Browse the repository at this point in the history
  • Loading branch information
janko authored and ixti committed Aug 1, 2016
1 parent 7a78d93 commit fb1e90c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def flush
self
end

# Value of the Content-Length header
#
# @return [Integer]
def content_length
Integer(headers[Headers::CONTENT_LENGTH]) if headers[Headers::CONTENT_LENGTH]
end

# Parsed Content-Type header
#
# @return [HTTP::ContentType]
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/http/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@
end
end

describe "#content_length" do
subject { response.content_length }

context "without Content-Length header" do
it { is_expected.to be_nil }
end

context "with Content-Length: 5" do
let(:headers) { {"Content-Length" => "5"} }
it { is_expected.to eq 5 }
end

context "with Content-Length not an integer" do
let(:headers) { {"Content-Length" => "foo"} }

it "raises an error" do
expect { subject }.to raise_error(ArgumentError)
end
end
end

describe "mime_type" do
subject { response.mime_type }

Expand Down

0 comments on commit fb1e90c

Please sign in to comment.