mark some tests as requiring no preloads when run with yath
[catagits/Catalyst-Runtime.git] / t / body_fh.t
1 use warnings;
2 use strict;
3 use Test::More;
4 use HTTP::Request::Common;
5 use HTTP::Message::PSGI;
6 use Plack::Util;
7
8 # Test case to check that we now send scalar and filehandle like
9 # bodys directly to the PSGI engine, rather than call $writer->write
10 # or unroll the filehandle ourselves.
11
12 {
13   package MyApp::Controller::Root;
14
15   use base 'Catalyst::Controller';
16
17   sub flat_response :Local {
18     my $response = 'Hello flat_response';
19     pop->res->body($response);
20   }
21
22   sub memory_stream :Local {
23     my $response = 'Hello memory_stream';
24     open my $fh, '<', \$response || die "$!";
25
26     pop->res->body($fh);
27   }
28
29   sub manual_write_fh :Local {
30     my ($self, $c) = @_;
31     my $response = 'Hello manual_write_fh';
32     my $writer = $c->res->write_fh;
33     $writer->write($response);
34     $writer->close;
35   }
36
37   sub manual_write :Local {
38     my ($self, $c) = @_;
39     $c->res->write('Hello');
40     $c->res->body('manual_write');
41   }
42
43   $INC{'MyApp/Controller/Root.pm'} = __FILE__; # sorry...
44
45   package MyApp;
46   use Catalyst;
47
48 }
49
50
51
52 ok(MyApp->setup);
53 ok(my $psgi = MyApp->psgi_app);
54
55 {
56   ok(my $env = req_to_psgi(GET '/root/flat_response'));
57   ok(my $psgi_response = $psgi->($env));
58
59   $psgi_response->(sub {
60     my $response_tuple = shift;
61     my ($status, $headers, $body) = @$response_tuple;
62
63     ok $status;
64     ok $headers;
65     is $body->[0], 'Hello flat_response';
66
67    });
68 }
69
70 {
71   ok(my $env = req_to_psgi(GET '/root/memory_stream'));
72   ok(my $psgi_response = $psgi->($env));
73
74   $psgi_response->(sub {
75     my $response_tuple = shift;
76     my ($status, $headers, $body) = @$response_tuple;
77
78     ok $status;
79     ok $headers;
80     is ref($body), 'GLOB';
81
82   });
83 }
84
85 {
86   ok(my $env = req_to_psgi(GET '/root/manual_write_fh'));
87   ok(my $psgi_response = $psgi->($env));
88
89   $psgi_response->(sub {
90     my $response_tuple = shift;
91     my ($status, $headers, $body) = @$response_tuple;
92
93     ok $status;
94     ok $headers;
95     ok !$body;
96
97     return Plack::Util::inline_object(
98         write => sub { is shift, 'Hello manual_write_fh' },
99         close => sub { ok 1, 'closed' },
100       );
101   });
102 }
103
104 {
105   ok(my $env = req_to_psgi(GET '/root/manual_write'));
106   ok(my $psgi_response = $psgi->($env));
107
108   $psgi_response->(sub {
109     my $response_tuple = shift;
110     my ($status, $headers, $body) = @$response_tuple;
111
112     ok $status;
113     ok $headers;
114     ok !$body;
115
116     my @expected = (qw/Hello manual_write/);
117     return Plack::Util::inline_object(
118         close => sub { ok 1, 'closed'; is scalar(@expected), 0; },
119         write => sub { is shift, shift(@expected) },
120       );
121   });
122 }
123
124 ## We need to specify the number of expected tests because tests that live
125 ## in the callbacks might never get run (thus all ran tests pass but not all
126 ## required tests run).
127
128 done_testing(28);