Debian Codegraph @

t/classes/Dancer2-Core-Role-StandardResponses/with.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 24;
4
5 {
6 package Handler;
7 use Moo;
8 with 'Dancer2::Core::Role::StandardResponses';
9 }
10
11 {
12 package App;
13 use Moo;
14 has response => ( is => 'ro', default => sub { Response->new } );
15 }
16
17 {
18 package Response;
19 use Moo;
20 has status => (is => 'ro', reader => '_status');
21 has header => (is => 'ro', reader => '_header');
22 sub status { shift->_status->(@_) }
23 sub header { shift->_header->(@_) }
24 }
25
26 note 'Checking our fake app'; {
27 my $app = App->new;
28 isa_ok( $app, 'App' );
29 can_ok( $app, 'response' );
30 isa_ok( $app->response, 'Response' );
31 }
32
33 note 'Checking our fake response'; {
34 my $response = Response->new(
35 status => sub {
36 my ( $self, $input ) = @_;
37 ::isa_ok( $self, 'Response' );
38 ::is( $input, 'calling status', 'status called' );
39 return 'foo';
40 },
41
42 header => sub {
43 my ( $self, $input ) = @_;
44 ::isa_ok( $self, 'Response' );
45 ::is( $input, 'calling header', 'header called' );
46 return qw<bar baz>;
47 },
48 );
49
50 isa_ok( $response, 'Response' );
51
52 is_deeply(
53 [ $response->status('calling status') ],
54 [ 'foo' ],
55 'status() works',
56 );
57
58 is_deeply(
59 [ $response->header('calling header') ],
60 [ qw<bar baz> ],
61 'header() works',
62 );
63 }
64
65 my $handler = Handler->new;
66 isa_ok( $handler, 'Handler' );
67 can_ok( $handler, qw<response standard_response> );
68
69 note '->response'; {
70 # set up status and header
71 my $app = App->new(
72 response => Response->new(
73 status => sub {
74 my ( $self, $code ) = @_;
75 ::isa_ok( $self, 'Response' );
76 ::is( $code, '400', 'Correct status code' );
77 },
78
79 header => sub {
80 my ( $self, $hdr_name, $hdr_content ) = @_;
81 ::isa_ok( $self, 'Response' );
82 ::is( $hdr_name, 'Content-Type', 'Correct header name' );
83 ::is( $hdr_content, 'text/plain', 'Correct header value' );
84 },
85 )
86 );
87
88 is(
89 $handler->response( $app, 400, 'Some Message' ),
90 'Some Message',
91 'Correct response created',
92 );
93 }
94
95 note '->standard_response'; {
96 # set up status and header
97 my $app = App->new(
98 response => Response->new(
99 status => sub {
100 my ( $self, $code ) = @_;
101 ::isa_ok( $self, 'Response' );
102 ::is( $code, '400', 'Correct status code' );
103 },
104
105 header => sub {
106 my ( $self, $hdr_name, $hdr_content ) = @_;
107 ::isa_ok( $self, 'Response' );
108 ::is( $hdr_name, 'Content-Type', 'Correct header name' );
109 ::is( $hdr_content, 'text/plain', 'Correct header value' );
110 },
111 )
112 );
113
114 is(
115 $handler->standard_response( $app, 400 ),
116 'Bad Request',
117 'Correct response 400 created',
118 );
119 }
120