diff --git a/lib/http/response.rb b/lib/http/response.rb index f49b7fc8..b57fba36 100644 --- a/lib/http/response.rb +++ b/lib/http/response.rb @@ -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] diff --git a/spec/lib/http/response_spec.rb b/spec/lib/http/response_spec.rb index 2842ba4b..52348baa 100644 --- a/spec/lib/http/response_spec.rb +++ b/spec/lib/http/response_spec.rb @@ -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 }