1#!perl
2
3use strict;
4use warnings;
5use lib 'inc';
6use Test::More;
7use Test::HTTP::Server;
8use File::Temp qw/tempfile/;
9use Net::Curl::Easy qw(:constants);
10
11BEGIN {
12plan skip_all => "CURLOPT_XFERINFOFUNCTION is not available untill version 7.32.0"
13    if Net::Curl::LIBCURL_VERSION_NUM() < 0x072000;
14}
15my $server = Test::HTTP::Server->new;
16plan skip_all => "Could not run http server\n" unless $server;
17plan tests => 17;
18
19# Init the curl session
20my $curl = Net::Curl::Easy->new();
21ok($curl, 'Curl session initialize returns something');
22ok(ref($curl) eq 'Net::Curl::Easy', 'Curl session looks like an object from the Net::Curl::Easy module');
23
24ok(! $curl->setopt(CURLOPT_NOPROGRESS, 0), "Setting CURLOPT_NOPROGRESS");
25ok(! $curl->setopt(CURLOPT_FOLLOWLOCATION, 1), "Setting CURLOPT_FOLLOWLOCATION");
26ok(! $curl->setopt(CURLOPT_TIMEOUT, 30), "Setting CURLOPT_TIMEOUT");
27
28my $head = tempfile();
29ok(! $curl->setopt(CURLOPT_WRITEHEADER, $head), "Setting CURLOPT_WRITEHEADER");
30
31my $body = tempfile();
32ok(! $curl->setopt(CURLOPT_FILE,$body), "Setting CURLOPT_FILE");
33
34ok(! $curl->setopt(CURLOPT_URL, $server->uri), "Setting CURLOPT_URL");
35
36my @myheaders;
37$myheaders[0] = "Server: www";
38$myheaders[1] = "User-Agent: Perl interface for libcURL";
39ok(! $curl->setopt(CURLOPT_HTTPHEADER, \@myheaders), "Setting CURLOPT_HTTPHEADER");
40
41ok(! $curl->setopt(CURLOPT_XFERINFODATA, "xferinfo data"), "Setting CURLOPT_XFERINFODATA");
42
43my $xferinfo_called = 0;
44my $xferinfo_data = '';
45my $last_dlnow = 0;
46sub prog_callb
47{
48    my ($clientp, $dltotal, $dlnow, $ultotal, $ulnow, $data)=@_;
49    $last_dlnow=$dlnow;
50    $xferinfo_called++;
51    $xferinfo_data = $data;
52    return 0;
53}
54
55ok (! $curl->setopt(CURLOPT_XFERINFOFUNCTION, \&prog_callb), "Setting CURLOPT_XFERINFOFUNCTION");
56
57ok (! $curl->setopt(CURLOPT_XFERINFODATA, "test-data"), "Setting CURLOPT_XFERINFODATA");
58
59ok (! $curl->setopt(CURLOPT_NOPROGRESS, 0), "Turning xferinfo meter back on");
60
61eval { $curl->perform() };
62ok (!$@, "Performing perform");
63
64ok ($xferinfo_called, "Progress callback called");
65
66ok ($xferinfo_data eq "test-data", "CURLOPT_XFERINFODATA is used correctly");
67
68ok ($last_dlnow, "Last downloaded chunk non-zero");
69