Debian Codegraph @

t/classes/Dancer2-Core-Response/new_from.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 3;
4 use Plack::Response;
5 use Dancer2::Core::Response;
6
7 sub normalize_headers {
8 my $headers = shift;
9
10 my %headers = ();
11 while ( my ( $key, $val ) = splice @{$headers}, 0, 2 ) {
12 $headers{$key} = $val;
13 }
14
15 return %headers;
16 }
17
18 can_ok( Dancer2::Core::Response::, qw<new_from_array new_from_plack> );
19
20 my %default_headers = (
21 'Content-Type' => 'text/plain',
22 'X-Test' => 'Val',
23 );
24
25 subtest 'new_from_array' => sub {
26 plan tests => 4;
27
28 my $array = [ 200, [%default_headers], ['Foo'] ];
29 my $response = Dancer2::Core::Response->new_from_array($array);
30
31 isa_ok( $response, 'Dancer2::Core::Response' );
32 is( $response->status, 200, 'Correct status' );
33 is( $response->content, 'Foo', 'Correct content' );
34
35 # hash randomization
36 my %headers = normalize_headers( $response->headers_to_array );
37
38 is_deeply(
39 \%headers,
40 \%default_headers,
41 'All headers correct',
42 );
43 };
44
45 subtest 'new_from_plack' => sub {
46 plan tests => 5;
47
48 my $plack = Plack::Response->new();
49 isa_ok( $plack, 'Plack::Response' );
50
51 $plack->status(200);
52 $plack->body('Bar');
53 foreach my $header_name ( keys %default_headers ) {
54 $plack->header( $header_name => $default_headers{$header_name} );
55 }
56
57 my $response = Dancer2::Core::Response->new_from_plack($plack);
58 isa_ok( $response, 'Dancer2::Core::Response' );
59 is( $response->status, 200, 'Correct status' );
60 is( $response->content, 'Bar', 'Correct content' );
61
62 # hash randomization
63 my %headers = normalize_headers( $response->headers_to_array );
64
65 is_deeply(
66 \%headers,
67 \%default_headers,
68 'All headers correct',
69 );
70 };
71