There's no Content-Length for streaming responses.
[catagits/Catalyst-Runtime.git] / t / aggregate / live_component_controller_action_streaming.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/../lib";
8
9 our $iters;
10
11 BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
12
13 use Test::More;
14 use Catalyst::Test 'TestApp';
15
16 if ( $ENV{CAT_BENCHMARK} ) {
17     require Benchmark;
18     Benchmark::timethis( $iters, \&run_tests );
19 }
20 else {
21     for ( 1 .. $iters ) {
22         run_tests();
23     }
24 }
25
26 sub 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' );
32         
33         SKIP:
34         {
35             if ( $ENV{CATALYST_SERVER} ) {
36                 skip "Using remote server", 1;
37             }
38             
39             # XXX: Length should be undef here, but HTTP::Request::AsCGI sets it
40             ok(!defined $response->content_length, 'No Content-Length for streaming responses');
41             is(length $response->content, 12, 'Response content' );
42         }
43         
44         is( $response->content,, <<'EOF', 'Content is a stream' );
45 foo
46 bar
47 baz
48 EOF
49     }
50
51     # test streaming by passing a handle to $c->res->body
52   SKIP:
53     {
54         if ( $ENV{CATALYST_SERVER} ) {
55             skip "Using remote server", 5;
56         }
57
58         my $file = "$FindBin::Bin/../lib/TestApp/Controller/Action/Streaming.pm";
59         my $fh = IO::File->new( $file, 'r' );
60         my $buffer;
61         if ( defined $fh ) {
62             $fh->read( $buffer, 1024 );
63             $fh->close;
64         }
65
66         ok( my $response = request('http://localhost/action/streaming/body'),
67             'Request' );
68         ok( $response->is_success, 'Response Successful 2xx' );
69         is( $response->content_type, 'text/plain', 'Response Content-Type' );
70         is( $response->content_length, -s $file, 'Response Content-Length' );
71         is( $response->content, $buffer, 'Content is read from filehandle' );
72     }
73
74     {
75         my $size = 128 * 1024; # more than one read with the default chunksize
76
77         ok( my $response = request('http://localhost/action/streaming/body_large'), 'Request' );
78         ok( $response->is_success, 'Response Successful 2xx' );
79         is( $response->content_type, 'text/plain', 'Response Content-Type' );
80         is( $response->content_length, $size, 'Response Content-Length' );
81         is( $response->content, "\0" x $size, 'Content is read from filehandle' );
82     }
83 }
84
85 done_testing;