From fe5b4d7f0492f6175da0d1c32cc4193c9cd0e9dc Mon Sep 17 00:00:00 2001 From: lulalala Date: Sun, 10 May 2015 14:30:40 +0800 Subject: [PATCH] Add reader for request header content-length --- README.textile | 2 ++ lib/fastimage.rb | 4 +++- test/test.rb | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.textile b/README.textile index 3b40c56..6dde249 100644 --- a/README.textile +++ b/README.textile @@ -51,6 +51,8 @@ FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_135066 => FastImage::ImageFetchFailure: FastImage::ImageFetchFailure FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>2.0) => [9545, 6623] +FastImage.new("http://stephensykes.com/images/pngimage").content_length +=> 432 h2. Installation diff --git a/lib/fastimage.rb b/lib/fastimage.rb index 59efd8e..132de4c 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -48,7 +48,7 @@ require 'pathname' require 'zlib' class FastImage - attr_reader :size, :type + attr_reader :size, :type, :content_length attr_reader :bytes_read @@ -252,6 +252,8 @@ class FastImage end end + @content_length = res.content_length + parse_packets FiberStream.new(read_fiber) break # needed to actively quit out of the fetch diff --git a/test/test.rb b/test/test.rb index 980e0dd..e344a2f 100644 --- a/test/test.rb +++ b/test/test.rb @@ -297,4 +297,18 @@ class FastImageTest < Test::Unit::TestCase ensure %x{rm -f shell_test} end + + def test_content_length + url = "#{TestUrl}with_content_length.gif" + FakeWeb.register_uri(:get, url, :body => File.join(FixturePath, "test.jpg"), :content_length => 52) + + assert_equal 52, FastImage.new(url).content_length + end + + def test_content_length_not_provided + url = "#{TestUrl}without_content_length.gif" + FakeWeb.register_uri(:get, url, :body => File.join(FixturePath, "test.jpg")) + + assert_equal nil, FastImage.new(url).content_length + end end