File: new_from.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 (71 lines) | stat: -rw-r--r-- 1,762 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
use strict;
use warnings;
use Test::More tests => 3;
use Plack::Response;
use Dancer2::Core::Response;

sub normalize_headers {
    my $headers = shift;

    my %headers = ();
    while ( my ( $key, $val ) = splice @{$headers}, 0, 2 ) {
        $headers{$key} = $val;
    }

    return %headers;
}

can_ok( Dancer2::Core::Response::, qw<new_from_array new_from_plack> );

my %default_headers = (
    'Content-Type' => 'text/plain',
    'X-Test'       => 'Val',
);

subtest 'new_from_array' => sub {
    plan tests => 4;

    my $array    = [ 200, [%default_headers], ['Foo'] ];
    my $response = Dancer2::Core::Response->new_from_array($array);

    isa_ok( $response, 'Dancer2::Core::Response' );
    is( $response->status,  200,   'Correct status' );
    is( $response->content, 'Foo', 'Correct content' );

    # hash randomization
    my %headers = normalize_headers( $response->headers_to_array );

    is_deeply(
        \%headers,
        \%default_headers,
        'All headers correct',
    );
};

subtest 'new_from_plack' => sub {
    plan tests => 5;

    my $plack = Plack::Response->new();
    isa_ok( $plack, 'Plack::Response' );

    $plack->status(200);
    $plack->body('Bar');
    foreach my $header_name ( keys %default_headers ) {
        $plack->header( $header_name => $default_headers{$header_name} );
    }

    my $response = Dancer2::Core::Response->new_from_plack($plack);
    isa_ok( $response, 'Dancer2::Core::Response' );
    is( $response->status,  200,   'Correct status' );
    is( $response->content, 'Bar', 'Correct content' );

    # hash randomization
    my %headers = normalize_headers( $response->headers_to_array );

    is_deeply(
        \%headers,
        \%default_headers,
        'All headers correct',
    );
};