Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP::Response#content_length #365

Closed
wants to merge 1 commit into from
Closed
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
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