Fix ->finalize_headers getting called twice. RT#78090
[catagits/Catalyst-Runtime.git] / t / aggregate / live_component_controller_action_streaming.t
CommitLineData
50cc3183 1#!perl
2
3use strict;
4use warnings;
5
6use FindBin;
42da66a9 7use lib "$FindBin::Bin/../lib";
50cc3183 8
9our $iters;
10
6b25e555 11BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
50cc3183 12
6e1254dc 13use Test::More;
50cc3183 14use Catalyst::Test 'TestApp';
15
16if ( $ENV{CAT_BENCHMARK} ) {
17 require Benchmark;
18 Benchmark::timethis( $iters, \&run_tests );
19}
20else {
21 for ( 1 .. $iters ) {
22 run_tests();
23 }
24}
25
26sub run_tests {
27 # test direct streaming
28 {
29 ok( my $response = request('http://localhost/streaming'), 'Request' );
30 ok( $response->is_success, 'Response Successful 2xx' );
31 is( $response->content_type, 'text/plain', 'Response Content-Type' );
258733f1 32 is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
89ba65d5 33 is( $response->header('X-Test-Header-Call-Count'), 1);
09ea4a24 34
048f45ee 35 SKIP:
36 {
37 if ( $ENV{CATALYST_SERVER} ) {
38 skip "Using remote server", 1;
39 }
09ea4a24 40
6e1254dc 41 ok(!defined $response->content_length, 'No Content-Length for streaming responses');
42 is(length $response->content, 12, 'Response content' );
048f45ee 43 }
09ea4a24 44
50cc3183 45 is( $response->content,, <<'EOF', 'Content is a stream' );
46foo
47bar
48baz
49EOF
50 }
51
52 # test streaming by passing a handle to $c->res->body
53 SKIP:
54 {
55 if ( $ENV{CATALYST_SERVER} ) {
9c74923d 56 skip "Using remote server", 10;
50cc3183 57 }
58
0e155123 59 my $file = "$FindBin::Bin/../lib/TestApp/Controller/Action/Streaming.pm";
50cc3183 60 my $fh = IO::File->new( $file, 'r' );
61 my $buffer;
62 if ( defined $fh ) {
9c74923d 63 $fh->read( $buffer, 2048 );
50cc3183 64 $fh->close;
65 }
66
67 ok( my $response = request('http://localhost/action/streaming/body'),
68 'Request' );
69 ok( $response->is_success, 'Response Successful 2xx' );
70 is( $response->content_type, 'text/plain', 'Response Content-Type' );
3b6a1db1 71 is( $response->content_length, -s $file, 'Response Content-Length' );
258733f1 72 is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
89ba65d5 73 is( $response->header('X-Test-Header-Call-Count'), 1);
50cc3183 74 is( $response->content, $buffer, 'Content is read from filehandle' );
9c74923d 75
76 ok( $response = request('http://localhost/action/streaming/body_glob'),
77 'Request' );
78 ok( $response->is_success, 'Response Successful 2xx' );
79 is( $response->content_type, 'text/plain', 'Response Content-Type' );
80 is( $response->content_length, -s $file, 'Response Content-Length' );
258733f1 81 is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
89ba65d5 82 is( $response->header('X-Test-Header-Call-Count'), 1);
9c74923d 83 is( $response->content, $buffer, 'Content is read from filehandle' );
50cc3183 84 }
a9b467d3 85
86 {
87 my $size = 128 * 1024; # more than one read with the default chunksize
88
89 ok( my $response = request('http://localhost/action/streaming/body_large'), 'Request' );
90 ok( $response->is_success, 'Response Successful 2xx' );
91 is( $response->content_type, 'text/plain', 'Response Content-Type' );
258733f1 92 is( $response->header('X-Test-Header'), 'valid', 'Headers sent properly' );
89ba65d5 93 is( $response->header('X-Test-Header-Call-Count'), 1);
a9b467d3 94 is( $response->content_length, $size, 'Response Content-Length' );
95 is( $response->content, "\0" x $size, 'Content is read from filehandle' );
96 }
50cc3183 97}
6e1254dc 98
99done_testing;