Blame t/common-req.t

Packit a09cf7
use strict;
Packit a09cf7
use warnings;
Packit a09cf7
Packit a09cf7
use Test::More;
Packit a09cf7
plan tests => 64;
Packit a09cf7
Packit a09cf7
use HTTP::Request::Common;
Packit a09cf7
Packit a09cf7
my $r = GET 'http://www.sn.no/';
Packit a09cf7
note $r->as_string;
Packit a09cf7
Packit a09cf7
is($r->method, "GET");
Packit a09cf7
is($r->uri, "http://www.sn.no/");
Packit a09cf7
Packit a09cf7
$r = HEAD "http://www.sn.no/",
Packit a09cf7
     If_Match => 'abc',
Packit a09cf7
     From => 'aas@sn.no';
Packit a09cf7
note $r->as_string;
Packit a09cf7
Packit a09cf7
is($r->method, "HEAD");
Packit a09cf7
ok($r->uri->eq("http://www.sn.no"));
Packit a09cf7
Packit a09cf7
is($r->header('If-Match'), "abc");
Packit a09cf7
is($r->header("from"), "aas\@sn.no");
Packit a09cf7
Packit a09cf7
$r = HEAD "http://www.sn.no/",
Packit a09cf7
	Content => 'foo';
Packit a09cf7
is($r->content, 'foo');
Packit a09cf7
Packit a09cf7
$r = HEAD "http://www.sn.no/",
Packit a09cf7
	Content => 'foo',
Packit a09cf7
	'Content-Length' => 50;
Packit a09cf7
is($r->content, 'foo');
Packit a09cf7
is($r->content_length, 50);
Packit a09cf7
Packit a09cf7
$r = PUT "http://www.sn.no",
Packit a09cf7
     Content => 'foo';
Packit a09cf7
note $r->as_string, "\n";
Packit a09cf7
Packit a09cf7
is($r->method, "PUT");
Packit a09cf7
is($r->uri->host, "www.sn.no");
Packit a09cf7
Packit a09cf7
ok(!defined($r->header("Content")));
Packit a09cf7
Packit a09cf7
is(${$r->content_ref}, "foo");
Packit a09cf7
is($r->content, "foo");
Packit a09cf7
is($r->content_length, 3);
Packit a09cf7
Packit a09cf7
$r = PUT "http://www.sn.no",
Packit a09cf7
     { foo => "bar" };
Packit a09cf7
is($r->content, "foo=bar");
Packit a09cf7
Packit a09cf7
$r = PATCH "http://www.sn.no",
Packit a09cf7
     { foo => "bar" };
Packit a09cf7
is($r->content, "foo=bar");
Packit a09cf7
Packit a09cf7
#--- Test POST requests ---
Packit a09cf7
Packit a09cf7
$r = POST "http://www.sn.no", [foo => 'bar;baz',
Packit a09cf7
                               baz => [qw(a b c)],
Packit a09cf7
                               foo => 'zoo=&',
Packit a09cf7
                               "space " => " + ",
Packit a09cf7
			       "nl" => "a\nb\r\nc\n",
Packit a09cf7
                              ],
Packit a09cf7
                              bar => 'foo';
Packit a09cf7
note $r->as_string, "\n";
Packit a09cf7
Packit a09cf7
is($r->method, "POST");
Packit a09cf7
is($r->content_type, "application/x-www-form-urlencoded");
Packit a09cf7
is($r->content_length, 83);
Packit a09cf7
is($r->header("bar"), "foo");
Packit a09cf7
is($r->content, "foo=bar%3Bbaz&baz=a&baz=b&baz=c&foo=zoo%3D%26&space+=+%2B+&nl=a%0D%0Ab%0D%0Ac%0D%0A");
Packit a09cf7
Packit a09cf7
$r = POST "http://example.com";
Packit a09cf7
is($r->content_length, 0);
Packit a09cf7
is($r->content, "");
Packit a09cf7
Packit a09cf7
$r = POST "http://example.com", [];
Packit a09cf7
is($r->content_length, 0);
Packit a09cf7
is($r->content, "");
Packit a09cf7
Packit a09cf7
$r = POST "mailto:gisle\@aas.no",
Packit a09cf7
     Subject => "Heisan",
Packit a09cf7
     Content_Type => "text/plain",
Packit a09cf7
     Content => "Howdy\n";
Packit a09cf7
#note $r->as_string;
Packit a09cf7
Packit a09cf7
is($r->method, "POST");
Packit a09cf7
is($r->header("Subject"), "Heisan");
Packit a09cf7
is($r->content, "Howdy\n");
Packit a09cf7
is($r->content_type, "text/plain");
Packit a09cf7
Packit a09cf7
{
Packit a09cf7
    my @warnings;
Packit a09cf7
    local $SIG{__WARN__} = sub { push @warnings, @_ };
Packit a09cf7
    $r = POST 'http://unf.ug/', [];
Packit a09cf7
    is( "@warnings", '', 'empty POST' );
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
#
Packit a09cf7
# POST for File upload
Packit a09cf7
#
Packit a09cf7
my $file = "test-$$";
Packit a09cf7
open(FILE, ">$file") or die "Can't create $file: $!";
Packit a09cf7
print FILE "foo\nbar\nbaz\n";
Packit a09cf7
close(FILE);
Packit a09cf7
Packit a09cf7
$r = POST 'http://www.perl.org/survey.cgi',
Packit a09cf7
       Content_Type => 'form-data',
Packit a09cf7
       Content      => [ name  => 'Gisle Aas',
Packit a09cf7
                         email => 'gisle@aas.no',
Packit a09cf7
                         gender => 'm',
Packit a09cf7
                         born   => '1964',
Packit a09cf7
                         file   => [$file],
Packit a09cf7
                       ];
Packit a09cf7
#note $r->as_string;
Packit a09cf7
Packit a09cf7
unlink($file) or warn "Can't unlink $file: $!";
Packit a09cf7
Packit a09cf7
is($r->method, "POST");
Packit a09cf7
is($r->uri->path, "/survey.cgi");
Packit a09cf7
is($r->content_type, "multipart/form-data");
Packit a09cf7
ok($r->header('Content_type') =~ /boundary="?([^"]+)"?/);
Packit a09cf7
my $boundary = $1;
Packit a09cf7
Packit a09cf7
my $c = $r->content;
Packit a09cf7
$c =~ s/\r//g;
Packit a09cf7
my @c = split(/--\Q$boundary/, $c);
Packit a09cf7
note "$c[5]\n";
Packit a09cf7
Packit a09cf7
is(@c, 7);
Packit a09cf7
like($c[6], qr/^--\n/);  # 5 parts + header & trailer
Packit a09cf7
Packit a09cf7
ok($c[2] =~ /^Content-Disposition:\s*form-data;\s*name="email"/m);
Packit a09cf7
ok($c[2] =~ /^gisle\@aas.no$/m);
Packit a09cf7
Packit a09cf7
ok($c[5] =~ /^Content-Disposition:\s*form-data;\s*name="file";\s*filename="$file"/m);
Packit a09cf7
ok($c[5] =~ /^Content-Type:\s*text\/plain$/m);
Packit a09cf7
ok($c[5] =~ /^foo\nbar\nbaz/m);
Packit a09cf7
Packit a09cf7
$r = POST 'http://www.perl.org/survey.cgi',
Packit a09cf7
      [ file => [ undef, "xxy\"", Content_type => "text/html", Content => "

Hello, world!

" ]],
Packit a09cf7
      Content_type => 'multipart/form-data';
Packit a09cf7
#note $r->as_string;
Packit a09cf7
Packit a09cf7
ok($r->content =~ /^--\S+\015\012Content-Disposition:\s*form-data;\s*name="file";\s*filename="xxy\\"/m);
Packit a09cf7
ok($r->content =~ /^Content-Type: text\/html/m);
Packit a09cf7
ok($r->content =~ /^

Hello, world/m);

Packit a09cf7
Packit a09cf7
$r = POST 'http://www.perl.org/survey.cgi',
Packit a09cf7
      Content_type => 'multipart/form-data',
Packit a09cf7
      Content => [ file => [ undef, undef, Content => "foo"]];
Packit a09cf7
#note $r->as_string;
Packit a09cf7
Packit a09cf7
unlike($r->content, qr/filename=/);
Packit a09cf7
Packit a09cf7
Packit a09cf7
# The POST routine can now also take a hash reference.
Packit a09cf7
my %hash = (foo => 42, bar => 24);
Packit a09cf7
$r = POST 'http://www.perl.org/survey.cgi', \%hash;
Packit a09cf7
#note $r->as_string, "\n";
Packit a09cf7
like($r->content, qr/foo=42/);
Packit a09cf7
like($r->content, qr/bar=24/);
Packit a09cf7
is($r->content_type, "application/x-www-form-urlencoded");
Packit a09cf7
is($r->content_length, 13);
Packit a09cf7
Packit a09cf7
 
Packit a09cf7
#
Packit a09cf7
# POST for File upload
Packit a09cf7
#
Packit a09cf7
use HTTP::Request::Common qw($DYNAMIC_FILE_UPLOAD);
Packit a09cf7
Packit a09cf7
$file = "test-$$";
Packit a09cf7
open(FILE, ">$file") or die "Can't create $file: $!";
Packit a09cf7
for (1..1000) {
Packit a09cf7
   print FILE "a" .. "z";
Packit a09cf7
}
Packit a09cf7
close(FILE);
Packit a09cf7
Packit a09cf7
$DYNAMIC_FILE_UPLOAD++;
Packit a09cf7
$r = POST 'http://www.perl.org/survey.cgi',
Packit a09cf7
       Content_Type => 'form-data',
Packit a09cf7
       Content      => [ name  => 'Gisle Aas',
Packit a09cf7
                         email => 'gisle@aas.no',
Packit a09cf7
                         gender => 'm',
Packit a09cf7
                         born   => '1964',
Packit a09cf7
                         file   => [$file],
Packit a09cf7
                       ];
Packit a09cf7
#note $r->as_string, "\n";
Packit a09cf7
Packit a09cf7
is($r->method, "POST");
Packit a09cf7
is($r->uri->path, "/survey.cgi");
Packit a09cf7
is($r->content_type, "multipart/form-data");
Packit a09cf7
ok($r->header('Content_type') =~ qr/boundary="?([^"]+)"?/);
Packit a09cf7
$boundary = $1;
Packit a09cf7
is(ref($r->content), "CODE");
Packit a09cf7
Packit a09cf7
cmp_ok(length($boundary), '>', 10);
Packit a09cf7
Packit a09cf7
my $code = $r->content;
Packit a09cf7
my $chunk;
Packit a09cf7
my @chunks;
Packit a09cf7
while (defined($chunk = &$code) && length $chunk) {
Packit a09cf7
   push(@chunks, $chunk);
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
unlink($file) or warn "Can't unlink $file: $!";
Packit a09cf7
Packit a09cf7
$_ = join("", @chunks);
Packit a09cf7
Packit a09cf7
#note int(@chunks), " chunks, total size is ", length($_), " bytes\n";
Packit a09cf7
Packit a09cf7
# should be close to expected size and number of chunks
Packit a09cf7
cmp_ok(abs(@chunks - 15), '<', 3);
Packit a09cf7
cmp_ok(abs(length($_) - 26589), '<', 20);
Packit a09cf7
Packit a09cf7
$r = POST 'http://www.example.com';
Packit a09cf7
is($r->as_string, <
Packit a09cf7
POST http://www.example.com
Packit a09cf7
Content-Length: 0
Packit a09cf7
Content-Type: application/x-www-form-urlencoded
Packit a09cf7
Packit a09cf7
EOT
Packit a09cf7
Packit a09cf7
$r = POST 'http://www.example.com', Content_Type => 'form-data', Content => [];
Packit a09cf7
is($r->as_string, <
Packit a09cf7
POST http://www.example.com
Packit a09cf7
Content-Length: 0
Packit a09cf7
Content-Type: multipart/form-data; boundary=none
Packit a09cf7
Packit a09cf7
EOT
Packit a09cf7
Packit a09cf7
$r = POST 'http://www.example.com', Content_Type => 'form-data';
Packit a09cf7
#note $r->as_string;
Packit a09cf7
is($r->as_string, <
Packit a09cf7
POST http://www.example.com
Packit a09cf7
Content-Length: 0
Packit a09cf7
Content-Type: multipart/form-data
Packit a09cf7
Packit a09cf7
EOT
Packit a09cf7
Packit a09cf7
$r = HTTP::Request::Common::DELETE 'http://www.example.com';
Packit a09cf7
is($r->method, "DELETE");
Packit a09cf7
Packit a09cf7
$r = HTTP::Request::Common::PUT 'http://www.example.com',
Packit a09cf7
    'Content-Type' => 'application/octet-steam',
Packit a09cf7
    'Content' => 'foobarbaz',
Packit a09cf7
    'Content-Length' => 12;   # a slight lie
Packit a09cf7
is($r->header('Content-Length'), 9);
Packit a09cf7
Packit a09cf7
$r = HTTP::Request::Common::PATCH 'http://www.example.com',
Packit a09cf7
    'Content-Type' => 'application/octet-steam',
Packit a09cf7
    'Content' => 'foobarbaz',
Packit a09cf7
    'Content-Length' => 12;   # a slight lie
Packit a09cf7
is($r->header('Content-Length'), 9);