From d89f9cb75615e4f3dc39fb274ce64b6bee320e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Sat, 23 Jul 2016 12:50:06 +0800 Subject: [PATCH] Add HTTP::Response#content_length --- lib/http/response.rb | 7 +++++++ spec/lib/http/response_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) 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 }