File: 02_response.t

package info (click to toggle)
libfurl-perl 3.14-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 660 kB
  • sloc: perl: 2,188; makefile: 5; sh: 1
file content (116 lines) | stat: -rw-r--r-- 2,911 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Test::More;
use Test::Requires qw(Plack::Request HTTP::Body), 'HTTP::Response';
use Furl::Response;

my $res = Furl::Response->new(
    1, 200, 'OK',
    +{
        'x-foo'            => ['yay'],
        'x-bar'            => ['hoge'],
        'content-length'   => [9],
        'content-type'     => ['text/html'],
        'content-encoding' => ['chunked'],
    },
    'hit man'
);
is $res->protocol, 'HTTP/1.1';
is $res->code, 200;
is $res->message, 'OK';
isa_ok $res->headers, 'Furl::Headers';
is $res->content, 'hit man';
is($res->headers->header('X-Foo'), 'yay');
ok $res->is_success;
is $res->status_line, '200 OK';
is $res->content_length, 9;
is $res->content_type, 'text/html';
is $res->content_encoding, 'chunked';
my $hres = $res->as_http_response;
isa_ok $hres, 'HTTP::Response';
is $hres->code, 200;
is $hres->message, 'OK';
isa_ok $hres->headers, 'HTTP::Headers';
is $hres->content_type, 'text/html';
is $hres->content, 'hit man';
is $hres->protocol, 'HTTP/1.1';

subtest 'as_hashref' => sub {
    my $dat = $res->as_hashref;
    my $headers = delete $dat->{headers};
    is_deeply(
        $dat, {
            message => 'OK',
            code => 200,
            content => 'hit man',
            protocol => 'HTTP/1.1',
        }
    );
    is_deeply(
        [sort @{$headers}],
        [sort qw(
            content-type text/html
            x-foo yay
            x-bar hoge
            content-length 9
            content-encoding chunked
        )]
    );
};

subtest 'to_psgi' => sub {
    my $dat = $res->to_psgi;
    is(0+@$dat, 3);
    is($dat->[0], 200);
    is_deeply(
        [sort @{$dat->[1]}],
        [sort qw(
            content-type text/html
            x-foo yay
            x-bar hoge
            content-length 9
            content-encoding chunked
        )]
    );
    is_deeply($dat->[2], ['hit man']);
};

subtest decoded_content => sub {
    my $res = Furl::Response->new(
        1, 200, 'OK',
        +{
            'content-type' => ['text/plain; charset=UTF-8'],
        },
        "\343\201\202\343\201\204\343\201\206\343\201\210\343\201\212",
    );
    is $res->decoded_content, "\x{3042}\x{3044}\x{3046}\x{3048}\x{304a}";
};

subtest 'as_string' => sub {
    my $res = Furl::Response->new(
        1, 200, 'OK',
        +{
            'x-foo'            => ['yay'],
            'x-bar'            => ['hoge'],
            'content-length'   => [9],
            'content-type'     => ['text/html'],
            'content-encoding' => ['chunked'],
        },
        'hit man'
    );
    my $expected = join("\015\012",
        '200 OK',
        'content-encoding: chunked',
        'content-length: 9',
        'content-type: text/html',
        'x-bar: hoge',
        'x-foo: yay',
        '',
        'hit man',
    );
    is($res->as_string, $expected);
    is(length($res->as_string), length($expected));
};

done_testing;