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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
print "1..19\n";
use HTTP::Request::Common;
$r = GET 'http://www.sn.no/';
print $r->as_string;
print "not " unless $r->method eq "GET" and $r->url eq "http://www.sn.no/";
print "ok 1\n";
$r = HEAD "http://www.sn.no/",
If_Match => 'abc',
From => 'aas@sn.no';
print $r->as_string;
print "not " unless $r->method eq "HEAD" and $r->url->eq("http://www.sn.no");
print "ok 2\n";
print "not " unless $r->header('If-Match') eq "abc" and $r->header("from") eq "aas\@sn.no";
print "ok 3\n";
$r = PUT "http://www.sn.no",
Content => 'foo';
print $r->as_string;
print "not " unless $r->method eq "PUT" and $r->url->netloc eq "www.sn.no";
print "ok 4\n";
print "not " if defined($r->header("Content"));
print "ok 5\n";
print "not " unless ${$r->content_ref} eq "foo" and
$r->content eq "foo";
print "ok 6\n";
#--- Test POST requests ---
$r = POST "http://www.sn.no", [foo => 'bar',
baz => [qw(a b c)],
foo => 'zoo=&',
"space " => " + ",
],
bar => 'foo';
print $r->as_string;
print "not " unless $r->method eq "POST" and
$r->content_type eq "application/x-www-form-urlencoded" and
$r->content_length == 58 and
$r->header("bar") eq "foo";
print "ok 7\n";
print "not " unless $r->content eq "foo=bar&baz=a&baz=b&baz=c&foo=zoo%3D%26&space%20=%20%2B%20";
print "ok 8\n";
$r = POST "mailto:gisle\@aas.no",
Subject => "Heisan",
Content_Type => "text/plain",
Content => "Howdy\n";
print $r->as_string;
print "not " unless $r->method eq "POST" and
$r->header("Subject") eq "Heisan" and
$r->content eq "Howdy\n" and
$r->content_type eq "text/plain";
print "ok 9\n";
#
# POST for File upload
#
$file = "test-$$";
open(FILE, ">$file") or die "Can't create $file: $!";
print FILE "foo\nbar\nbaz\n";
close(FILE);
$r = POST 'http://www.perl.org/survey.cgi',
Content_Type => 'form-data',
Content => [ name => 'Gisle Aas',
email => 'gisle@aas.no',
gender => 'm',
born => '1964',
file => [$file],
];
print $r->as_string;
unlink($file) or warn "Can't unlink $file: $!";
print "not " unless $r->method eq "POST" and
$r->url->path eq "/survey.cgi" and
$r->content_type eq "multipart/form-data" and
$r->header(Content_type) =~ /boundary="?([^"]+)"?/;
print "ok 10\n";
$boundary = $1;
$c = $r->content;
$c =~ s/\r//g;
@c = split(/--\Q$boundary/, $c);
print "$c[5]\n";
print "not " unless @c == 7 and $c[6] =~ /^--\n/; # 5 parts + header & trailer
print "ok 11\n";
print "not " unless $c[2] =~ /^Content-Disposition:\s*form-data;\s*name="email"/m and
$c[2] =~ /^gisle\@aas.no$/m;
print "ok 12\n";
print "not " unless $c[5] =~ /^Content-Disposition:\s*form-data;\s*name="file";\s*filename="$file"/m and
$c[5] =~ /^Content-Type:\s*text\/plain$/m and
$c[5] =~ /^foo\nbar\nbaz/m;
print "ok 13\n";
$r = POST 'http://www.perl.org/survey.cgi',
[ file => [ undef, "xxx", Content_type => "text/html", Content => "<h1>Hello, world!</h1>" ]],
Content_type => 'multipart/form-data';
print $r->as_string;
print "not " unless $r->content =~ /^--\S+\015\012Content-Disposition:\s*form-data;\s*name="file";\s*filename="xxx"/m and
$r->content =~ /^Content-Type: text\/html/m and
$r->content =~ /^<h1>Hello, world/m;
print "ok 14\n";
$r = POST 'http://www.perl.org/survey.cgi',
Content_type => 'multipart/form-data',
Content => [ file => [ undef, undef, Content => "foo"]];
print $r->as_string;
print "not " if $r->content =~ /filename=/;
print "ok 15\n";
# The POST routine can now also take a hash reference.
my %hash = (foo => 42, bar => 24);
$r = POST 'http://www.perl.org/survey.cgi', \%hash;
print $r->as_string;
print "not " unless $r->content =~ /foo=42/ &&
$r->content =~ /bar=24/ &&
$r->content_type eq "application/x-www-form-urlencoded" &&
$r->content_length == 13;
print "ok 16\n";
#
# POST for File upload
#
use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
$file = "test-$$";
open(FILE, ">$file") or die "Can't create $file: $!";
for (1..1000) {
print FILE "a" .. "z";
}
close(FILE);
$DYNAMIC_FILE_UPLOAD++;
$r = POST 'http://www.perl.org/survey.cgi',
Content_Type => 'form-data',
Content => [ name => 'Gisle Aas',
email => 'gisle@aas.no',
gender => 'm',
born => '1964',
file => [$file],
];
print $r->as_string;
print "not " unless $r->method eq "POST" and
$r->url->path eq "/survey.cgi" and
$r->content_type eq "multipart/form-data" and
$r->header(Content_type) =~ /boundary="?([^"]+)"?/ and
ref($r->content) eq "CODE";
print "ok 17\n";
$boundary = $1;
print "not " unless length($boundary) > 10;
print "ok 18\n";
$code = $r->content;
my $chunk;
my @chunks;
while (defined($chunk = &$code) && length $chunk) {
push(@chunks, $chunk);
}
unlink($file) or warn "Can't unlink $file: $!";
$_ = join("", @chunks);
print int(@chunks), " chunks, total size is ", length($_), " bytes\n";
# should be close to expected size and number of chunks
print "not " unless abs(@chunks - 15 < 3) and
abs(length($_) - 26589) < 20;
print "ok 19\n";
|