Updated with Tizen:Base source codes
[toolchains/perl-WWW-Curl.git] / t / 16formpost.t
1 #!perl
2
3 use Test::More skip_all => "Not performing POST";
4
5 ######################### We start with some black magic to print on failure.
6
7 # Change 1..1 below to 1..last_test_to_print .
8 use strict;
9
10 END {print "not ok 1\n" unless $::loaded;}
11 use WWW::Curl::Easy;
12
13 $::loaded = 1;
14
15 ######################### End of black magic.
16
17 my $count=0;
18
19 use ExtUtils::MakeMaker qw(prompt);
20
21 # Read URL to get, defaulting to environment variable if supplied
22 my $defurl=$ENV{CURL_TEST_URL} || "http://www.google.com/";
23 my $url = prompt("# Please enter an URL to fetch",$defurl);
24 if (!$url) {
25     print "1..0 # No test URL supplied - skipping test\n";
26     exit;
27 }
28 print "1..6\n";
29 print "ok ".++$count."\n";
30
31 # Init the curl session
32 my $curl = WWW::Curl::Easy->new();
33 if ($curl == 0) {
34     print "not ";
35 }
36 print "ok ".++$count."\n";
37
38 $curl->setopt(CURLOPT_NOPROGRESS, 1);
39 $curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
40 $curl->setopt(CURLOPT_TIMEOUT, 30);
41
42 open HEAD, ">head.out";
43 $curl->setopt(CURLOPT_WRITEHEADER, *HEAD);
44 print "ok ".++$count."\n";
45
46 open BODY, ">body.out";
47 $curl->setopt(CURLOPT_FILE,*BODY);
48 print "ok ".++$count."\n";
49
50 $curl->setopt(CURLOPT_URL, $url);
51
52 print "ok ".++$count."\n";
53
54 my $read_max=1000;
55
56 sub read_callb
57 {
58     my ($maxlen,$sv)=@_;
59 #    print STDERR "\nperl read_callback has been called!\n";
60 #    print STDERR "max data size: $maxlen - need $read_max bytes\n";
61         if ($read_max > 0) {
62                 my $len=int($read_max/3)+1;
63                 my $data = chr(ord('A')+rand(26))x$len;
64 #               print STDERR "generated max/3=", int($read_max/3)+1, " characters to be uploaded - $data.\n";
65                 $read_max=$read_max-length($data);
66                 return $data;
67         } else {
68                 return "";
69         }
70 }  
71
72 #
73 # test post/read callback functions - requires a url which accepts posts, or it fails!
74 #
75
76 $curl->setopt(CURLOPT_READFUNCTION,\&read_callb);
77 $curl->setopt(CURLOPT_INFILESIZE,$read_max );
78 $curl->setopt(CURLOPT_UPLOAD,1 );
79 $curl->setopt(CURLOPT_CUSTOMREQUEST,"POST" );
80                                                        
81 if ($curl->perform() != 0) {
82         print "not ";
83 };
84 print "ok ".++$count."\n";