Fix #3910: Corrupted images during upload.

Use a fresh tempfile for each download attempt instead of reusing the same
file (and having to rewind/truncate it after each failed attempt).
This commit is contained in:
evazion
2018-09-18 10:01:03 -05:00
parent d3c135ec72
commit 9cdfbba6c2
2 changed files with 16 additions and 8 deletions

View File

@@ -6,7 +6,6 @@ module Downloads
setup do
@source = "http://www.google.com/intl/en_ALL/images/logo.gif"
@download = Downloads::File.new(@source)
@tempfile = Tempfile.new("danbooru-test")
end
context "that fails" do
@@ -14,11 +13,22 @@ module Downloads
HTTParty.expects(:get).times(3).raises(Errno::ETIMEDOUT)
assert_raises(Errno::ETIMEDOUT) { @download.download! }
end
should "return an uncorrupted file on the second try" do
bomb = stub("bomb")
bomb.expects(:size).raises(IOError)
resp = stub("resp", success?: true)
HTTParty.expects(:get).twice.multiple_yields("a", bomb, "b", "c").then.multiple_yields("a", "b", "c").returns(resp)
tempfile, _ = @download.download!
assert_equal("abc", tempfile.read)
end
end
should "throw an exception when the file is larger than the maximum" do
assert_raise(Downloads::File::Error) do
@download.http_get_streaming(@source, @tempfile, {}, max_size: 1)
@download.download!(max_size: 1)
end
end