File: 002-basic-content-type-handlers.t

package info (click to toggle)
libweb-machine-perl 0.17-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: perl: 5,481; makefile: 2
file content (96 lines) | stat: -rw-r--r-- 1,912 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
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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Plack::Test;

use HTTP::Request::Common;

use Web::Machine;

my $HTML = '<html><body>Hello World</body></html>';

{
    package My::Resource::String;
    use strict;
    use warnings;

    use parent 'Web::Machine::Resource';

    sub content_types_provided { [{ 'text/html' => 'to_html' }] }

    sub to_html { $HTML }
}

{
    package My::Resource::IO;
    use strict;
    use warnings;

    use parent 'Web::Machine::Resource';

    sub content_types_provided { [{ 'text/html' => 'to_html' }] }

    sub to_html {
        my $str = $HTML;
        open my $fh, '<', \$str;
        return $fh;
    }
}

{
    package My::Resource::Code;
    use strict;
    use warnings;

    use IO::Handle::Util 'io_from_getline';

    use parent 'Web::Machine::Resource';

    sub content_types_provided { [{ 'text/html' => 'to_html' }] }

    sub to_html {
        my $str = $HTML;
        return io_from_getline sub {
            length $str ? substr $str, 0, 1, '' : undef
        };
    }
}

test_psgi
    app    => Web::Machine->new(resource => 'My::Resource::String'),
    client => sub {
        my $cb = shift;

        {
            my $res = $cb->(GET '/');
            ok($res->is_success) || diag($res->content);
            is($res->content, $HTML);
        }
    };

test_psgi
    app    => Web::Machine->new(resource => 'My::Resource::IO'),
    client => sub {
        my $cb = shift;

        {
            my $res = $cb->(GET '/');
            ok($res->is_success) || diag($res->content);
            is($res->content, $HTML);
        }
    };

test_psgi
    app    => Web::Machine->new(resource => 'My::Resource::Code'),
    client => sub {
        my $cb = shift;

        {
            my $res = $cb->(GET '/');
            ok($res->is_success) || diag($res->content);
            is($res->content, $HTML);
        }
    };

done_testing;