| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More tests => 12; |
| 5 |
use Plack::Test; |
| 6 |
use HTTP::Request; |
| 7 |
use Ref::Util qw<is_coderef>; |
| 8 |
|
| 9 |
use Dancer2; |
| 10 |
|
| 11 |
my %method = ( |
| 12 |
get => 'GET', |
| 13 |
post => 'POST', |
| 14 |
del => 'DELETE', |
| 15 |
patch => 'PATCH', |
| 16 |
put => 'PUT', |
| 17 |
options => 'OPTIONS', |
| 18 |
); |
| 19 |
|
| 20 |
my $app = __PACKAGE__->to_app; |
| 21 |
ok( is_coderef($app), 'Got app' ); |
| 22 |
|
| 23 |
test_psgi $app, sub { |
| 24 |
my $cb = shift; |
| 25 |
|
| 26 |
while ( my ( $method, $http ) = each %method ) { |
| 27 |
eval "$method '/' => sub { '$method' }"; |
| 28 |
is( |
| 29 |
$cb->( HTTP::Request->new( $http => '/' ) )->content, |
| 30 |
$method, |
| 31 |
"$http /", |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
eval "get '/head' => sub {'HEAD'}"; |
| 36 |
|
| 37 |
my $res = $cb->( HTTP::Request->new( HEAD => '/head' ) ); |
| 38 |
is( $res->content, '', 'HEAD /' ); # HEAD requests have no content |
| 39 |
is( $res->headers->content_length, 4, 'Content-Length for HEAD' ); |
| 40 |
|
| 41 |
# Testing invalid HTTP methods. |
| 42 |
{ |
| 43 |
my $req = HTTP::Request->new( "ILLEGAL" => '/' ); |
| 44 |
my $res = $cb->( $req ); |
| 45 |
ok( !$res->is_success, "Response->is_success is false when using illegal HTTP method" ); |
| 46 |
is( $res->code, 405, "Illegal method should return 405 code" ); |
| 47 |
like( $res->content, qr<Method Not Allowed>, q<Illegal method should have "Method Not Allowed" in the content> ); |
| 48 |
} |
| 49 |
}; |