1
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
3
class TestCurbCurlDownload < Test::Unit::TestCase
4
include TestServerMethods
10
def test_download_url_to_file_via_string
11
dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
12
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
14
curb = Curl::Easy.download(dl_url, dl_path)
15
assert File.exist?(dl_path)
16
assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
18
File.unlink(dl_path) if File.exist?(dl_path)
21
def test_download_url_to_file_via_file_io
22
dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
23
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
24
io = File.open(dl_path, 'wb')
26
curb = Curl::Easy.download(dl_url, io)
28
assert File.exist?(dl_path)
29
assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
31
File.unlink(dl_path) if File.exist?(dl_path)
34
def test_download_url_to_file_via_io
35
dl_url = "http://127.0.0.1:9129/ext/curb_easy.c"
36
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
37
reader, writer = IO.pipe
43
File.open(dl_path, 'wb') { |file| file << reader.read }
45
reader.close rescue IOError # if the stream has already been closed
49
# Download remote source
52
curb = Curl::Easy.download(dl_url, writer)
55
writer.close rescue IOError # if the stream has already been closed, which occurs in Easy::download
58
assert File.exist?(dl_path)
59
assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
61
File.unlink(dl_path) if File.exist?(dl_path)
64
def test_download_bad_url_gives_404
65
dl_url = "http://127.0.0.1:9129/this_file_does_not_exist.html"
66
dl_path = File.join(Dir::tmpdir, "dl_url_test.file")
68
curb = Curl::Easy.download(dl_url, dl_path)
69
assert_equal Curl::Easy, curb.class
70
assert_equal 404, curb.response_code
72
File.unlink(dl_path) if File.exist?(dl_path)