Debian Codegraph @

t/classes/Dancer2-Core-Request/serializers.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Plack::Test;
5 use HTTP::Request::Common;
6
7 {
8 package App::CBOR; ## no critic
9 use Dancer2;
10
11 # postpone
12 sub setup {
13 set serializer => 'CBOR';
14 post '/' => sub {
15 ::is_deeply( +{ params() }, +{}, 'Empty parameters' );
16 ::is( request->data, 'Foo', 'Correct data using request->data' );
17 return 'ok';
18 };
19 }
20 }
21
22 subtest 'Testing with CBOR' => sub {
23 eval { require CBOR::XS; 1; }
24 or plan skip_all => 'CBOR::XS is needed for this test';
25
26 eval { require Dancer2::Serializer::CBOR; 1; }
27 or plan skip_all => 'Dancer2::Serializer::CBOR is needed for this test';
28
29 App::CBOR->setup;
30 my $app = Plack::Test->create( App::CBOR->to_app );
31 my $res = $app->request(
32 POST '/',
33 Content => CBOR::XS::encode_cbor('Foo'),
34 );
35
36 ok( $res->is_success, 'Successful response' );
37 is(
38 $res->content,
39 CBOR::XS::encode_cbor('ok'),
40 'Correct response',
41 );
42 };
43
44 {
45 package App::JSON; ## no critic
46 use Dancer2;
47 set serializer => 'JSON';
48 post '/' => sub {
49 ::is_deeply( +{ params() }, +{}, 'Empty parameters' );
50 ::is_deeply(
51 request->data,
52 [ qw<foo bar> ],
53 'Correct data using request->data',
54 );
55 return [ qw<foo bar> ];
56 };
57 }
58
59 subtest 'Testing with JSON' => sub {
60 my $app = Plack::Test->create( App::JSON->to_app );
61 my $res = $app->request(
62 POST '/',
63 Content => '["foo","bar"]',
64 );
65
66 ok( $res->is_success, 'Successful response' );
67 is(
68 $res->content,
69 '["foo","bar"]',
70 'Correct response',
71 );
72 };
73
74 done_testing();