1use strict; 2use warnings; 3use Test::More; 4use Plack::Test; 5use HTTP::Request::Common; 6 7subtest 'request data basic' => sub { 8 { 9 package App::Body::Str; ## no critic 10 use Dancer2; 11 12 post '/' => sub { 13 my $data = request_data; 14 ::is( 15 $data, 16 'a string body', 17 'string content ok' 18 ); 19 }; 20 } 21 22 my $app = Plack::Test->create( App::Body::Str->to_app ); 23 my $res = $app->request( POST '/', Content_Type => 'text/plain', Content => "a string body" ); 24 ok( $res->is_success, 'Successful request' ); 25}; 26 27subtest 'request data serialized' => sub { 28 { 29 package App::Body::JSON; ## no critic 30 use Dancer2; 31 32 set serializer => 'JSON'; 33 34 post '/' => sub { 35 my $data = request_data; 36 ::is_deeply( 37 $data, 38 { body => { is => [ "json" ] } }, 39 'json content ok' 40 ); 41 42 return +{ ok => 1 }; 43 }; 44 } 45 46 my $app = Plack::Test->create( App::Body::JSON->to_app ); 47 my $res = $app->request( POST '/', Content_Type => 'application/json', Content => '{"body":{"is":["json"]}}' ); 48 ok( $res->is_success, 'Successful request' ); 49}; 50 51done_testing(); 52