File: with.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 (120 lines) | stat: -rw-r--r-- 3,052 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use strict;
use warnings;
use Test::More tests => 24;

{
    package Handler;
    use Moo;
    with 'Dancer2::Core::Role::StandardResponses';
}

{
    package App;
    use Moo;
    has response => ( is => 'ro', default => sub { Response->new } );
}

{
    package Response;
    use Moo;
    has status => (is => 'ro', reader => '_status');
    has header => (is => 'ro', reader => '_header');
    sub status { shift->_status->(@_) }
    sub header { shift->_header->(@_) }
}

note 'Checking our fake app'; {
    my $app = App->new;
    isa_ok( $app, 'App'      );
    can_ok( $app, 'response' );
    isa_ok( $app->response, 'Response' );
}

note 'Checking our fake response'; {
    my $response = Response->new(
        status => sub {
            my ( $self, $input ) = @_;
            ::isa_ok( $self, 'Response' );
            ::is( $input, 'calling status', 'status called' );
            return 'foo';
        },

        header => sub {
            my ( $self, $input ) = @_;
            ::isa_ok( $self, 'Response' );
            ::is( $input, 'calling header', 'header called' );
            return qw<bar baz>;
        },
    );

    isa_ok( $response, 'Response' );

    is_deeply(
        [ $response->status('calling status') ],
        [ 'foo' ],
        'status() works',
    );

    is_deeply(
        [ $response->header('calling header') ],
        [ qw<bar baz> ],
        'header() works',
    );
}

my $handler = Handler->new;
isa_ok( $handler, 'Handler' );
can_ok( $handler, qw<response standard_response> );

note '->response'; {
    # set up status and header
    my $app = App->new(
        response => Response->new(
            status => sub {
                my ( $self, $code ) = @_;
                ::isa_ok( $self, 'Response' );
                ::is( $code, '400', 'Correct status code' );
            },

            header => sub {
                my ( $self, $hdr_name, $hdr_content ) = @_;
                ::isa_ok( $self, 'Response' );
                ::is( $hdr_name, 'Content-Type', 'Correct header name' );
                ::is( $hdr_content, 'text/plain', 'Correct header value' );
            },
        )
    );

    is(
        $handler->response( $app, 400, 'Some Message' ),
        'Some Message',
        'Correct response created',
    );
}

note '->standard_response'; {
    # set up status and header
    my $app = App->new(
        response => Response->new(
            status => sub {
                my ( $self, $code ) = @_;
                ::isa_ok( $self, 'Response' );
                ::is( $code, '400', 'Correct status code' );
            },

            header => sub {
                my ( $self, $hdr_name, $hdr_content ) = @_;
                ::isa_ok( $self, 'Response' );
                ::is( $hdr_name, 'Content-Type', 'Correct header name' );
                ::is( $hdr_content, 'text/plain', 'Correct header value' );
            },
        )
    );

    is(
        $handler->standard_response( $app, 400 ),
        'Bad Request',
        'Correct response 400 created',
    );
}