File: serializers.t

package info (click to toggle)
libdancer2-perl 0.400001%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,580 kB
  • sloc: perl: 8,461; makefile: 9
file content (74 lines) | stat: -rw-r--r-- 1,727 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;

{
    package App::CBOR; ## no critic
    use Dancer2;

    # postpone
    sub setup {
        set serializer => 'CBOR';
        post '/' => sub {
            ::is_deeply( +{ params() }, +{}, 'Empty parameters' );
            ::is( request->data, 'Foo', 'Correct data using request->data' );
            return 'ok';
        };
    }
}

subtest 'Testing with CBOR' => sub {
    eval { require CBOR::XS; 1; }
        or plan skip_all => 'CBOR::XS is needed for this test';

    eval { require Dancer2::Serializer::CBOR; 1; }
        or plan skip_all => 'Dancer2::Serializer::CBOR is needed for this test';

    App::CBOR->setup;
    my $app = Plack::Test->create( App::CBOR->to_app );
    my $res = $app->request(
        POST '/',
        Content => CBOR::XS::encode_cbor('Foo'),
    );

    ok( $res->is_success, 'Successful response' );
    is(
        $res->content,
        CBOR::XS::encode_cbor('ok'),
        'Correct response',
    );
};

{
    package App::JSON; ## no critic
    use Dancer2;
    set serializer => 'JSON';
    post '/' => sub {
        ::is_deeply( +{ params() }, +{}, 'Empty parameters' );
        ::is_deeply(
            request->data,
            [ qw<foo bar> ],
            'Correct data using request->data',
        );
        return [ qw<foo bar> ];
    };
}

subtest 'Testing with JSON' => sub {
    my $app = Plack::Test->create( App::JSON->to_app );
    my $res = $app->request(
        POST '/',
        Content => '["foo","bar"]',
    );

    ok( $res->is_success, 'Successful response' );
    is(
        $res->content,
        '["foo","bar"]',
        'Correct response',
    );
};

done_testing();