| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Plack::Test; |
| 5 |
use HTTP::Request::Common; |
| 6 |
use JSON::MaybeXS; |
| 7 |
|
| 8 |
{ |
| 9 |
package App; |
| 10 |
use Dancer2; |
| 11 |
set serializer => 'JSON'; |
| 12 |
|
| 13 |
post '/' => sub { |
| 14 |
my $post_params = params('body'); |
| 15 |
|
| 16 |
# should work even with empty post body |
| 17 |
my $foo = $post_params->{'foo'}; |
| 18 |
return { foo => $foo }; |
| 19 |
}; |
| 20 |
} |
| 21 |
|
| 22 |
my $test = Plack::Test->create( App->to_app ); |
| 23 |
my %headers; |
| 24 |
|
| 25 |
subtest 'Basic response failing' => sub { |
| 26 |
TODO: { |
| 27 |
local $TODO = '500 when deserializing bad input'; |
| 28 |
my $res = $test->request( POST '/', { foo => 'bar' }, %headers ); |
| 29 |
is( $res->code, 500, '[POST /] Failed when sending regular params' ); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
subtest 'Basic response' => sub { |
| 34 |
my $res = $test->request( |
| 35 |
POST '/', |
| 36 |
%headers, |
| 37 |
Content => encode_json { foo => 'bar' } |
| 38 |
); |
| 39 |
|
| 40 |
is( $res->code, 200, '[POST /] Correct response code' ); |
| 41 |
|
| 42 |
my $response_data = decode_json( $res->decoded_content ); |
| 43 |
is($response_data->{foo}, 'bar', "[POST /] Correct response data"); |
| 44 |
}; |
| 45 |
|
| 46 |
subtest 'Empty POST' => sub { |
| 47 |
my $res = $test->request( POST '/', {}, %headers ); |
| 48 |
is( |
| 49 |
$res->code, |
| 50 |
200, |
| 51 |
'[POST /] Correct response code with empty post body', |
| 52 |
); |
| 53 |
}; |
| 54 |
|
| 55 |
done_testing(); |
| 56 |
|