File: 02callbacks.t

package info (click to toggle)
libwww-curl-perl 4.17-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 584 kB
  • sloc: perl: 1,909; makefile: 5
file content (41 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (9)
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
#!perl

use strict;
use warnings;
use Test::More tests => 7;
use File::Temp qw/tempfile/;

BEGIN { use_ok( 'WWW::Curl::Easy' ); }

my $url = $ENV{CURL_TEST_URL} || "http://www.google.com";

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

$curl->setopt(CURLOPT_NOPROGRESS, 1);
$curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
$curl->setopt(CURLOPT_TIMEOUT, 30);

my $head = tempfile();
$curl->setopt(CURLOPT_WRITEHEADER, $head);

my $body = tempfile();
$curl->setopt(CURLOPT_FILE,$body);

$curl->setopt(CURLOPT_URL, $url);

my $header_called = 0;
sub header_callback { $header_called = 1; return length($_[0]) };
my $body_called = 0;
sub body_callback { $body_called++;return length($_[0]) };



ok (! $curl->setopt(CURLOPT_HEADERFUNCTION, \&header_callback), "CURLOPT_HEADERFUNCTION set");
ok (! $curl->setopt(CURLOPT_WRITEFUNCTION, \&body_callback), "CURLOPT_WRITEFUNCTION set");

$curl->perform();
ok($header_called, "CURLOPT_HEADERFUNCTION callback was used");
ok($body_called, "CURLOPT_WRITEFUNCTION callback was used");