1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
class TestCurbCurlDownload < Test::Unit::TestCase
include TestServerMethods
def setup
server_setup
end
def test_download_url_to_file_via_string
dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
curb = Curl::Easy.download(dl_url, dl_path)
assert File.exist?(dl_path)
assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
ensure
File.unlink(dl_path) if File.exist?(dl_path)
end
def test_download_url_to_file_via_file_io
dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
io = File.open(dl_path, 'wb')
curb = Curl::Easy.download(dl_url, io)
assert io.closed?
assert File.exist?(dl_path)
assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
ensure
File.unlink(dl_path) if File.exist?(dl_path)
end
def test_download_url_to_file_via_io
dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
reader, writer = IO.pipe
# Write to local file
fork do
begin
writer.close
File.open(dl_path, 'wb') { |file| file << reader.read }
ensure
reader.close rescue IOError # if the stream has already been closed
end
end
# Download remote source
begin
reader.close
curb = Curl::Easy.download(dl_url, writer)
Process.wait
ensure
writer.close rescue IOError # if the stream has already been closed, which occurs in Easy::download
end
assert File.exist?(dl_path)
assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
ensure
File.unlink(dl_path) if File.exist?(dl_path)
end
def test_download_bad_url_gives_404
dl_url = "http://127.0.0.1:9129/this_file_does_not_exist.html"
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
curb = Curl::Easy.download(dl_url, dl_path)
assert_equal Curl::Easy, curb.class
assert_equal 404, curb.response_code
ensure
File.unlink(dl_path) if File.exist?(dl_path)
end
end
|