actually use the new keywords
[catagits/Catalyst-Runtime.git] / t / body_fh.t
CommitLineData
46fff667 1use warnings;
2use strict;
3use Test::More;
4use HTTP::Request::Common;
5use HTTP::Message::PSGI;
6use Plack::Util;
7af15a33 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.
46fff667 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 package MyApp;
44 use Catalyst;
45
46}
47
48$INC{'MyApp/Controller/Root.pm'} = '1'; # sorry...
49
50ok(MyApp->setup);
51ok(my $psgi = MyApp->psgi_app);
52
53{
54 ok(my $env = req_to_psgi(GET '/root/flat_response'));
55 ok(my $psgi_response = $psgi->($env));
56
57 $psgi_response->(sub {
58 my $response_tuple = shift;
59 my ($status, $headers, $body) = @$response_tuple;
60
61 ok $status;
62 ok $headers;
63 is $body->[0], 'Hello flat_response';
64
65 });
66}
67
68{
69 ok(my $env = req_to_psgi(GET '/root/memory_stream'));
70 ok(my $psgi_response = $psgi->($env));
71
72 $psgi_response->(sub {
73 my $response_tuple = shift;
74 my ($status, $headers, $body) = @$response_tuple;
75
76 ok $status;
77 ok $headers;
78 is ref($body), 'GLOB';
79
80 });
81}
82
83{
84 ok(my $env = req_to_psgi(GET '/root/manual_write_fh'));
85 ok(my $psgi_response = $psgi->($env));
86
87 $psgi_response->(sub {
88 my $response_tuple = shift;
89 my ($status, $headers, $body) = @$response_tuple;
90
91 ok $status;
92 ok $headers;
93 ok !$body;
94
95 return Plack::Util::inline_object(
96 write => sub { is shift, 'Hello manual_write_fh' },
97 close => sub { ok 1, 'closed' },
98 );
99 });
100}
101
102{
103 ok(my $env = req_to_psgi(GET '/root/manual_write'));
104 ok(my $psgi_response = $psgi->($env));
105
106 $psgi_response->(sub {
107 my $response_tuple = shift;
108 my ($status, $headers, $body) = @$response_tuple;
109
110 ok $status;
111 ok $headers;
112 ok !$body;
113
114 my @expected = (qw/Hello manual_write/);
115 return Plack::Util::inline_object(
116 close => sub { ok 1, 'closed'; is scalar(@expected), 0; },
117 write => sub { is shift, shift(@expected) },
118 );
119 });
120}
121
122## We need to specify the number of expected tests because tests that live
123## in the callbacks might never get run (thus all ran tests pass but not all
124## required tests run).
125
126done_testing(28);