Add reader for request header content-length

This commit is contained in:
lulalala 2015-05-10 14:30:40 +08:00
parent 88355932a9
commit fe5b4d7f04
3 changed files with 19 additions and 1 deletions

View File

@ -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
</code></pre>
h2. Installation

View File

@ -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

View File

@ -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