Net-Curl

 view release on metacpan or  search on metacpan

t/old-01basic.t  view on Meta::CPAN

#!perl

use strict;
use warnings;
use lib 'inc';
use Test::More;
use Test::HTTP::Server;
use File::Temp qw/tempfile/;

use Net::Curl::Easy qw(:constants);

local $ENV{no_proxy} = '*';

my $server = Test::HTTP::Server->new;
plan skip_all => "Could not run http server\n" unless $server;
my $proxyheader_tests = Net::Curl::LIBCURL_VERSION_NUM() < 0x072500 ? 0 : 2;
plan tests => 18 + $proxyheader_tests;


# Init the curl session
my $curl = Net::Curl::Easy->new();
ok($curl, 'Curl session initialize returns something');
ok(ref($curl) eq 'Net::Curl::Easy', 'Curl session looks like an object from the Net::Curl::Easy module');

ok(! $curl->setopt(CURLOPT_NOPROGRESS, 1), "Setting CURLOPT_NOPROGRESS");
ok(! $curl->setopt(CURLOPT_FOLLOWLOCATION, 1), "Setting CURLOPT_FOLLOWLOCATION");
ok(! $curl->setopt(CURLOPT_TIMEOUT, 30), "Setting CURLOPT_TIMEOUT");
ok(! $curl->setopt(CURLOPT_ENCODING, undef), "Setting CURLOPT_ENCODING to undef");
ok(! $curl->setopt(CURLOPT_RESUME_FROM_LARGE, 0), "Setting CURLOPT_RESUME_FROM_LARGE to 0");
$curl->setopt(CURLOPT_HEADER, 1);

my $head = tempfile();
ok(! $curl->setopt(CURLOPT_WRITEHEADER, $head), "Setting CURLOPT_WRITEHEADER");

my $body = tempfile();
ok(! $curl->setopt(CURLOPT_WRITEDATA,$body), "Setting CURLOPT_WRITEDATA");

ok(! $curl->setopt(CURLOPT_URL, $server->uri . "cookie/3" ), "Setting CURLOPT_URL");

my @myheaders;
$myheaders[0] = "Server: www";
$myheaders[1] = "User-Agent: Perl interface for libcURL";
ok(! $curl->setopt(CURLOPT_HTTPHEADER, \@myheaders), "Setting CURLOPT_HTTPHEADER");
ok(! $curl->pushopt(CURLOPT_HTTPHEADER, ["Random: header"]));

if ($proxyheader_tests) {
    ok(! $curl->setopt(CURLOPT_PROXYHEADER(), \@myheaders), "Setting CURLOPT_PROXYHEADER");
    ok(! $curl->pushopt(CURLOPT_PROXYHEADER(), ["Random: header"]));
}

$curl->setopt(CURLOPT_COOKIEFILE, "");
eval { $curl->perform(); };

ok( !$@, "Curl return code ok");
diag("An error happened: $@ -- ".$curl->error."\n") if ($@);
my $bytes = $curl->getinfo(CURLINFO_SIZE_DOWNLOAD);
ok( $bytes, "getinfo returns non-zero number of bytes");
my $realurl = $curl->getinfo(CURLINFO_EFFECTIVE_URL);
ok( $realurl, "getinfo returns CURLINFO_EFFECTIVE_URL");
my $httpcode = $curl->getinfo(CURLINFO_HTTP_CODE);
ok( $httpcode, "getinfo returns CURLINFO_HTTP_CODE");

my $cookies = $curl->getinfo( CURLINFO_COOKIELIST );
is(ref $cookies, "ARRAY", "Returned array reference");
ok(@$cookies == 3, "Got 3 cookies");

#diag ("Bytes: $bytes");
#diag ("realurl: $realurl");
#diag ("httpcode: $httpcode");



( run in 2.555 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )