Skip to content

Commit fb1e90c

Browse files
jankoixti
authored andcommitted
Add HTTP::Response#content_length
1 parent 7a78d93 commit fb1e90c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/http/response.rb

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ def flush
8686
self
8787
end
8888

89+
# Value of the Content-Length header
90+
#
91+
# @return [Integer]
92+
def content_length
93+
Integer(headers[Headers::CONTENT_LENGTH]) if headers[Headers::CONTENT_LENGTH]
94+
end
95+
8996
# Parsed Content-Type header
9097
#
9198
# @return [HTTP::ContentType]

spec/lib/http/response_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@
2828
end
2929
end
3030

31+
describe "#content_length" do
32+
subject { response.content_length }
33+
34+
context "without Content-Length header" do
35+
it { is_expected.to be_nil }
36+
end
37+
38+
context "with Content-Length: 5" do
39+
let(:headers) { {"Content-Length" => "5"} }
40+
it { is_expected.to eq 5 }
41+
end
42+
43+
context "with Content-Length not an integer" do
44+
let(:headers) { {"Content-Length" => "foo"} }
45+
46+
it "raises an error" do
47+
expect { subject }.to raise_error(ArgumentError)
48+
end
49+
end
50+
end
51+
3152
describe "mime_type" do
3253
subject { response.mime_type }
3354

0 commit comments

Comments
 (0)