Blame t/request.t

Packit a09cf7
# Test extra HTTP::Request methods.  Basic operation is tested in the
Packit a09cf7
# message.t test suite.
Packit a09cf7
Packit a09cf7
use strict;
Packit a09cf7
use warnings;
Packit a09cf7
Packit a09cf7
use Test::More;
Packit a09cf7
plan tests => 35;
Packit a09cf7
Packit a09cf7
use HTTP::Request;
Packit a09cf7
use Try::Tiny qw( catch try );
Packit a09cf7
Packit a09cf7
my $req = HTTP::Request->new( GET => "http://www.example.com" );
Packit a09cf7
$req->accept_decodable;
Packit a09cf7
Packit a09cf7
is( $req->method, "GET" );
Packit a09cf7
is( $req->uri,    "http://www.example.com" );
Packit a09cf7
like( $req->header("Accept-Encoding"), qr/\bgzip\b/ )
Packit a09cf7
    ;    # assuming IO::Uncompress::Gunzip is there
Packit a09cf7
Packit a09cf7
$req->dump( prefix => "# " );
Packit a09cf7
Packit a09cf7
is( $req->method("DELETE"), "GET" );
Packit a09cf7
is( $req->method,           "DELETE" );
Packit a09cf7
Packit a09cf7
is( $req->uri("http:"), "http://www.example.com" );
Packit a09cf7
is( $req->uri,          "http:" );
Packit a09cf7
Packit a09cf7
$req->protocol("HTTP/1.1");
Packit a09cf7
Packit a09cf7
my $r2 = HTTP::Request->parse( $req->as_string );
Packit a09cf7
is( $r2->method,                    "DELETE" );
Packit a09cf7
is( $r2->uri,                       "http:" );
Packit a09cf7
is( $r2->protocol,                  "HTTP/1.1" );
Packit a09cf7
is( $r2->header("Accept-Encoding"), $req->header("Accept-Encoding") );
Packit a09cf7
Packit a09cf7
# Test objects which are accepted as URI-like
Packit a09cf7
{
Packit a09cf7
    package Foo::URI;
Packit a09cf7
Packit a09cf7
    use strict;
Packit a09cf7
    use warnings;
Packit a09cf7
Packit a09cf7
    sub new { return bless {}, shift; }
Packit a09cf7
    sub clone  { return shift }
Packit a09cf7
    sub scheme { }
Packit a09cf7
Packit a09cf7
    1;
Packit a09cf7
Packit a09cf7
    package Foo::URI::WithCanonical;
Packit a09cf7
Packit a09cf7
    sub new { return bless {}, shift; }
Packit a09cf7
    sub clone     { return shift }
Packit a09cf7
    sub scheme    { }
Packit a09cf7
    sub canonical { }
Packit a09cf7
Packit a09cf7
    1;
Packit a09cf7
Packit a09cf7
    package Foo::URI::WithoutScheme;
Packit a09cf7
Packit a09cf7
    sub new { return bless {}, shift; }
Packit a09cf7
    sub clone     { return shift }
Packit a09cf7
Packit a09cf7
    1;
Packit a09cf7
Packit a09cf7
    package main;
Packit a09cf7
Packit a09cf7
    ok( Foo::URI->new->can('scheme'), 'Object can scheme()' );
Packit a09cf7
    ok(
Packit a09cf7
        !do {
Packit a09cf7
            try {
Packit a09cf7
                HTTP::Request->new( GET => Foo::URI->new );
Packit a09cf7
                return 1;
Packit a09cf7
            }
Packit a09cf7
            catch { return 0 };
Packit a09cf7
        },
Packit a09cf7
        'Object without canonical method triggers an exception'
Packit a09cf7
    );
Packit a09cf7
Packit a09cf7
    ok(
Packit a09cf7
        Foo::URI::WithCanonical->new->can('canonical'),
Packit a09cf7
        'Object can canonical()'
Packit a09cf7
    );
Packit a09cf7
Packit a09cf7
    ok(
Packit a09cf7
        do {
Packit a09cf7
            try {
Packit a09cf7
                HTTP::Request->new( GET => Foo::URI::WithCanonical->new );
Packit a09cf7
                return 1;
Packit a09cf7
            }
Packit a09cf7
            catch { return 0 };
Packit a09cf7
        },
Packit a09cf7
        'Object with canonical method does not trigger an exception'
Packit a09cf7
    );
Packit a09cf7
Packit a09cf7
    ok( !Foo::URI::WithoutScheme->new->can('scheme'), 'Object cannot scheme()' );
Packit a09cf7
    ok(
Packit a09cf7
        !do {
Packit a09cf7
            try {
Packit a09cf7
                HTTP::Request->new( GET => Foo::URI::WithoutScheme->new );
Packit a09cf7
                return 1;
Packit a09cf7
            }
Packit a09cf7
            catch { return 0 };
Packit a09cf7
        },
Packit a09cf7
        'Object without scheme method triggers an exception'
Packit a09cf7
    );
Packit a09cf7
}
Packit a09cf7
Packit a09cf7
eval { $req->uri ({ foo => 'bar'}); };
Packit a09cf7
like($@, qr/A URI can't be a HASH reference/);
Packit a09cf7
eval { $req->uri (['foo']); };
Packit a09cf7
like($@, qr/A URI can't be a ARRAY reference/);
Packit a09cf7
Packit a09cf7
$req = HTTP::Request->new;
Packit a09cf7
is($req->as_string, "- -\n\n");
Packit a09cf7
is($req->dump, <
Packit a09cf7
- -
Packit a09cf7
Packit a09cf7
(no content)
Packit a09cf7
EOT
Packit a09cf7
$req->protocol("HTTP/1.1");
Packit a09cf7
is($req->dump, <
Packit a09cf7
- - HTTP/1.1
Packit a09cf7
Packit a09cf7
(no content)
Packit a09cf7
EOT
Packit a09cf7
Packit a09cf7
{
Packit a09cf7
	my @warn;
Packit a09cf7
	local $SIG{__WARN__} = sub { push @warn, @_ };
Packit a09cf7
	local $^W = 0;
Packit a09cf7
	$r2 = HTTP::Request->parse( undef );
Packit a09cf7
	is($#warn, -1);
Packit a09cf7
	local $^W = 1;
Packit a09cf7
	$r2 = HTTP::Request->parse( undef );
Packit a09cf7
	is($#warn, 0);
Packit a09cf7
	like($warn[0], qr/Undefined argument to parse\(\)/);
Packit a09cf7
}
Packit a09cf7
is( $r2->method,                    undef );
Packit a09cf7
is( $r2->uri,                       undef );
Packit a09cf7
is( $r2->protocol,                  undef );
Packit a09cf7
is( $r2->header("Accept-Encoding"), $req->header("Accept-Encoding") );
Packit a09cf7
Packit a09cf7
$r2 = HTTP::Request->parse('methonly');
Packit a09cf7
is( $r2->method,   'methonly' );
Packit a09cf7
is( $r2->uri,      undef );
Packit a09cf7
is( $r2->protocol, undef );
Packit a09cf7
Packit a09cf7
$r2 = HTTP::Request->parse('methonly http://www.example.com/');
Packit a09cf7
is( $r2->method,   'methonly' );
Packit a09cf7
is( $r2->uri,      'http://www.example.com/' );
Packit a09cf7
is( $r2->protocol, undef );