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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 36;
{
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;
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 response_400 response_403 response_404> );
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 '->response_400'; {
# 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_400($app),
'Bad Request',
'Correct response 400 created',
);
}
note '->response_403'; {
# set up status and header
my $app = App->new(
response => Response->new(
status => sub {
my ( $self, $code ) = @_;
::isa_ok( $self, 'Response' );
::is( $code, '403', '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_403($app),
'Unauthorized',
'Correct response 403 created',
);
}
note '->response_404'; {
# set up status and header
my $app = App->new(
response => Response->new(
status => sub {
my ( $self, $code ) = @_;
::isa_ok( $self, 'Response' );
::is( $code, '404', '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_404($app),
'Not Found',
'Correct response 404 created',
);
}
|