| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
|
| 6 |
use File::Spec; |
| 7 |
use File::Temp qw(tempfile); |
| 8 |
use HTTP::Request::Common; |
| 9 |
|
| 10 |
my $r = GET 'http://www.sn.no/'; |
| 11 |
note $r->as_string; |
| 12 |
|
| 13 |
is($r->method, "GET"); |
| 14 |
is($r->uri, "http://www.sn.no/"); |
| 15 |
|
| 16 |
$r = HEAD "http://www.sn.no/", |
| 17 |
If_Match => 'abc', |
| 18 |
From => '[email protected]'; |
| 19 |
note $r->as_string; |
| 20 |
|
| 21 |
is($r->method, "HEAD"); |
| 22 |
ok($r->uri->eq("http://www.sn.no")); |
| 23 |
|
| 24 |
is($r->header('If-Match'), "abc"); |
| 25 |
is($r->header("from"), "aas\@sn.no"); |
| 26 |
|
| 27 |
$r = HEAD "http://www.sn.no/", |
| 28 |
Content => 'foo'; |
| 29 |
is($r->content, 'foo'); |
| 30 |
|
| 31 |
$r = HEAD "http://www.sn.no/", |
| 32 |
Content => 'foo', |
| 33 |
'Content-Length' => 50; |
| 34 |
is($r->content, 'foo'); |
| 35 |
is($r->content_length, 50); |
| 36 |
|
| 37 |
$r = PUT "http://www.sn.no", |
| 38 |
Content => 'foo'; |
| 39 |
note $r->as_string, "\n"; |
| 40 |
|
| 41 |
is($r->method, "PUT"); |
| 42 |
is($r->uri->host, "www.sn.no"); |
| 43 |
|
| 44 |
ok(!defined($r->header("Content"))); |
| 45 |
|
| 46 |
is(${$r->content_ref}, "foo"); |
| 47 |
is($r->content, "foo"); |
| 48 |
is($r->content_length, 3); |
| 49 |
|
| 50 |
$r = PUT "http://www.sn.no", |
| 51 |
{ foo => "bar" }; |
| 52 |
is($r->content, "foo=bar"); |
| 53 |
|
| 54 |
$r = OPTIONS "http://www.sn.no", |
| 55 |
Content => 'foo'; |
| 56 |
note $r->as_string, "\n"; |
| 57 |
|
| 58 |
is($r->method, "OPTIONS"); |
| 59 |
is($r->uri->host, "www.sn.no"); |
| 60 |
|
| 61 |
ok(!defined($r->header("Content"))); |
| 62 |
|
| 63 |
is(${$r->content_ref}, "foo"); |
| 64 |
is($r->content, "foo"); |
| 65 |
is($r->content_length, 3); |
| 66 |
|
| 67 |
$r = OPTIONS "http://www.sn.no", |
| 68 |
{ foo => "bar" }; |
| 69 |
is($r->content, "foo=bar"); |
| 70 |
|
| 71 |
$r = PATCH "http://www.sn.no", |
| 72 |
{ foo => "bar" }; |
| 73 |
is($r->content, "foo=bar"); |
| 74 |
|
| 75 |
#--- Test POST requests --- |
| 76 |
|
| 77 |
$r = POST "http://www.sn.no", [foo => 'bar;baz', |
| 78 |
baz => [qw(a b c)], |
| 79 |
foo => 'zoo=&', |
| 80 |
"space " => " + ", |
| 81 |
"nl" => "a\nb\r\nc\n", |
| 82 |
], |
| 83 |
bar => 'foo'; |
| 84 |
note $r->as_string, "\n"; |
| 85 |
|
| 86 |
is($r->method, "POST"); |
| 87 |
is($r->content_type, "application/x-www-form-urlencoded"); |
| 88 |
is($r->content_length, 77, 'content_length'); |
| 89 |
is($r->header("bar"), "foo", 'bar is foo'); |
| 90 |
is($r->content, 'foo=bar%3Bbaz&baz=a&baz=b&baz=c&foo=zoo%3D%26&space+=+%2B+&nl=a%0Ab%0D%0Ac%0A'); |
| 91 |
|
| 92 |
$r = POST "http://example.com"; |
| 93 |
is($r->content_length, 0); |
| 94 |
is($r->content, ""); |
| 95 |
|
| 96 |
$r = POST "http://example.com", []; |
| 97 |
is($r->content_length, 0); |
| 98 |
is($r->content, ""); |
| 99 |
|
| 100 |
$r = POST "mailto:gisle\@aas.no", |
| 101 |
Subject => "Heisan", |
| 102 |
Content_Type => "text/plain", |
| 103 |
Content => "Howdy\n"; |
| 104 |
#note $r->as_string; |
| 105 |
|
| 106 |
is($r->method, "POST"); |
| 107 |
is($r->header("Subject"), "Heisan"); |
| 108 |
is($r->content, "Howdy\n"); |
| 109 |
is($r->content_type, "text/plain"); |
| 110 |
|
| 111 |
{ |
| 112 |
my @warnings; |
| 113 |
local $SIG{__WARN__} = sub { push @warnings, @_ }; |
| 114 |
$r = POST 'http://unf.ug/', []; |
| 115 |
is( "@warnings", '', 'empty POST' ); |
| 116 |
} |
| 117 |
|
| 118 |
# |
| 119 |
# POST for File upload |
| 120 |
# |
| 121 |
my (undef, $file) = tempfile(); |
| 122 |
my $form_file = (File::Spec->splitpath($file))[-1]; |
| 123 |
open(FILE, ">$file") or die "Can't create $file: $!"; |
| 124 |
print FILE "foo\nbar\nbaz\n"; |
| 125 |
close(FILE); |
| 126 |
|
| 127 |
$r = POST 'http://www.perl.org/survey.cgi', |
| 128 |
Content_Type => 'form-data', |
| 129 |
Content => [ name => 'Gisle Aas', |
| 130 |
email => '[email protected]', |
| 131 |
gender => 'm', |
| 132 |
born => '1964', |
| 133 |
file => [$file], |
| 134 |
]; |
| 135 |
#note $r->as_string; |
| 136 |
|
| 137 |
unlink($file) or warn "Can't unlink $file: $!"; |
| 138 |
|
| 139 |
is($r->method, "POST"); |
| 140 |
is($r->uri->path, "/survey.cgi"); |
| 141 |
is($r->content_type, "multipart/form-data"); |
| 142 |
ok($r->header('Content_type') =~ /boundary="?([^"]+)"?/); |
| 143 |
my $boundary = $1; |
| 144 |
|
| 145 |
my $c = $r->content; |
| 146 |
$c =~ s/\r//g; |
| 147 |
my @c = split(/--\Q$boundary/, $c); |
| 148 |
note "$c[5]\n"; |
| 149 |
|
| 150 |
is(@c, 7); |
| 151 |
like($c[6], qr/^--\n/); # 5 parts + header & trailer |
| 152 |
|
| 153 |
ok($c[2] =~ /^Content-Disposition:\s*form-data;\s*name="email"/m); |
| 154 |
ok($c[2] =~ /^gisle\@aas.no$/m); |
| 155 |
|
| 156 |
ok($c[5] =~ /^Content-Disposition:\s*form-data;\s*name="file";\s*filename="$form_file"/m); |
| 157 |
ok($c[5] =~ /^Content-Type:\s*text\/plain$/m); |
| 158 |
ok($c[5] =~ /^foo\nbar\nbaz/m); |
| 159 |
|
| 160 |
$r = POST 'http://www.perl.org/survey.cgi', |
| 161 |
[ file => [ undef, "xxy\"", Content_type => "text/html", Content => "<h1>Hello, world!</h1>" ]], |
| 162 |
Content_type => 'multipart/form-data'; |
| 163 |
#note $r->as_string; |
| 164 |
|
| 165 |
ok($r->content =~ /^--\S+\015\012Content-Disposition:\s*form-data;\s*name="file";\s*filename="xxy\\"/m); |
| 166 |
ok($r->content =~ /^Content-Type: text\/html/m); |
| 167 |
ok($r->content =~ /^<h1>Hello, world/m); |
| 168 |
|
| 169 |
$r = POST 'http://www.perl.org/survey.cgi', |
| 170 |
Content_type => 'multipart/form-data', |
| 171 |
Content => [ file => [ undef, undef, Content => "foo"]]; |
| 172 |
#note $r->as_string; |
| 173 |
|
| 174 |
unlike($r->content, qr/filename=/); |
| 175 |
|
| 176 |
|
| 177 |
# The POST routine can now also take a hash reference. |
| 178 |
my %hash = (foo => 42, bar => 24); |
| 179 |
$r = POST 'http://www.perl.org/survey.cgi', \%hash; |
| 180 |
#note $r->as_string, "\n"; |
| 181 |
like($r->content, qr/foo=42/); |
| 182 |
like($r->content, qr/bar=24/); |
| 183 |
is($r->content_type, "application/x-www-form-urlencoded"); |
| 184 |
is($r->content_length, 13); |
| 185 |
|
| 186 |
|
| 187 |
# |
| 188 |
# POST for File upload |
| 189 |
# |
| 190 |
use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD); |
| 191 |
|
| 192 |
(undef, $file) = tempfile(); |
| 193 |
open(FILE, ">$file") or die "Can't create $file: $!"; |
| 194 |
for (1..1000) { |
| 195 |
print FILE "a" .. "z"; |
| 196 |
} |
| 197 |
close(FILE); |
| 198 |
|
| 199 |
$DYNAMIC_FILE_UPLOAD++; |
| 200 |
$r = POST 'http://www.perl.org/survey.cgi', |
| 201 |
Content_Type => 'form-data', |
| 202 |
Content => [ name => 'Gisle Aas', |
| 203 |
email => '[email protected]', |
| 204 |
gender => 'm', |
| 205 |
born => '1964', |
| 206 |
file => [$file], |
| 207 |
]; |
| 208 |
#note $r->as_string, "\n"; |
| 209 |
|
| 210 |
is($r->method, "POST"); |
| 211 |
is($r->uri->path, "/survey.cgi"); |
| 212 |
is($r->content_type, "multipart/form-data"); |
| 213 |
ok($r->header('Content_type') =~ qr/boundary="?([^"]+)"?/); |
| 214 |
$boundary = $1; |
| 215 |
is(ref($r->content), "CODE"); |
| 216 |
|
| 217 |
cmp_ok(length($boundary), '>', 10); |
| 218 |
|
| 219 |
my $code = $r->content; |
| 220 |
my $chunk; |
| 221 |
my @chunks; |
| 222 |
while (defined($chunk = &$code) && length $chunk) { |
| 223 |
push(@chunks, $chunk); |
| 224 |
} |
| 225 |
|
| 226 |
unlink($file) or warn "Can't unlink $file: $!"; |
| 227 |
|
| 228 |
$_ = join("", @chunks); |
| 229 |
|
| 230 |
#note int(@chunks), " chunks, total size is ", length($_), " bytes\n"; |
| 231 |
|
| 232 |
# should be close to expected size and number of chunks |
| 233 |
cmp_ok(abs(@chunks - 6), '<', 3); |
| 234 |
cmp_ok(abs(length($_) - 26589), '<', 20); |
| 235 |
|
| 236 |
$r = POST 'http://www.example.com'; |
| 237 |
is($r->as_string, <<EOT); |
| 238 |
POST http://www.example.com |
| 239 |
Content-Length: 0 |
| 240 |
Content-Type: application/x-www-form-urlencoded |
| 241 |
|
| 242 |
EOT |
| 243 |
|
| 244 |
$r = POST 'http://www.example.com', Content_Type => 'form-data', Content => []; |
| 245 |
is($r->as_string, <<EOT); |
| 246 |
POST http://www.example.com |
| 247 |
Content-Length: 0 |
| 248 |
Content-Type: multipart/form-data; boundary=none |
| 249 |
|
| 250 |
EOT |
| 251 |
|
| 252 |
$r = POST 'http://www.example.com', Content_Type => 'form-data'; |
| 253 |
#note $r->as_string; |
| 254 |
is($r->as_string, <<EOT); |
| 255 |
POST http://www.example.com |
| 256 |
Content-Length: 0 |
| 257 |
Content-Type: multipart/form-data |
| 258 |
|
| 259 |
EOT |
| 260 |
|
| 261 |
$r = HTTP::Request::Common::DELETE 'http://www.example.com'; |
| 262 |
is($r->method, "DELETE"); |
| 263 |
|
| 264 |
$r = HTTP::Request::Common::PUT 'http://www.example.com', |
| 265 |
'Content-Type' => 'application/octet-steam', |
| 266 |
'Content' => 'foobarbaz', |
| 267 |
'Content-Length' => 12; # a slight lie |
| 268 |
is($r->header('Content-Length'), 9); |
| 269 |
|
| 270 |
$r = HTTP::Request::Common::PATCH 'http://www.example.com', |
| 271 |
'Content-Type' => 'application/octet-steam', |
| 272 |
'Content' => 'foobarbaz', |
| 273 |
'Content-Length' => 12; # a slight lie |
| 274 |
is($r->header('Content-Length'), 9); |
| 275 |
|
| 276 |
done_testing(); |