~ubuntu-branches/ubuntu/trusty/ruby-curb/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/tc_curl_download.rb

  • Committer: Package Import Robot
  • Author(s): Praveen Arimbrathodiyil
  • Date: 2013-05-15 23:07:43 UTC
  • Revision ID: package-import@ubuntu.com-20130515230743-0d8zw826n6hjrk55
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
 
2
 
 
3
class TestCurbCurlDownload < Test::Unit::TestCase
 
4
  include TestServerMethods 
 
5
 
 
6
  def setup
 
7
    server_setup
 
8
  end
 
9
  
 
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")
 
13
 
 
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)
 
17
  ensure
 
18
    File.unlink(dl_path) if File.exist?(dl_path)
 
19
  end
 
20
 
 
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')
 
25
 
 
26
    curb = Curl::Easy.download(dl_url, io)
 
27
    assert io.closed?
 
28
    assert File.exist?(dl_path)
 
29
    assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
 
30
  ensure
 
31
    File.unlink(dl_path) if File.exist?(dl_path)
 
32
  end
 
33
 
 
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
 
38
 
 
39
    # Write to local file
 
40
    fork do
 
41
      begin
 
42
        writer.close
 
43
        File.open(dl_path, 'wb') { |file| file << reader.read }
 
44
      ensure
 
45
        reader.close rescue IOError # if the stream has already been closed
 
46
      end
 
47
    end
 
48
 
 
49
    # Download remote source
 
50
    begin
 
51
      reader.close
 
52
      curb = Curl::Easy.download(dl_url, writer)
 
53
      Process.wait
 
54
    ensure
 
55
      writer.close rescue IOError # if the stream has already been closed, which occurs in Easy::download
 
56
    end
 
57
 
 
58
    assert File.exist?(dl_path)
 
59
    assert_equal File.read(File.join(File.dirname(__FILE__), '..','ext','curb_easy.c')), File.read(dl_path)
 
60
  ensure
 
61
    File.unlink(dl_path) if File.exist?(dl_path)
 
62
  end
 
63
 
 
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")
 
67
 
 
68
    curb = Curl::Easy.download(dl_url, dl_path)
 
69
    assert_equal Curl::Easy, curb.class
 
70
    assert_equal 404, curb.response_code
 
71
  ensure
 
72
    File.unlink(dl_path) if File.exist?(dl_path)
 
73
  end
 
74
 
 
75
end