1
# Before `make install' is performed this script should be runnable with
2
# `make test'. After `make install' it should work as `perl test.pl'
4
#########################
6
use Test::More tests => 15;
8
use_ok('CGI::Application::Plugin::Stream');
10
unshift @INC, 't/lib';
18
# Useless here, since the point is to test streaming directly.
19
#$ENV{CGI_APP_RETURN_ONLY} = 1;
24
my $stdout = tie *STDOUT, 'TieOut' or die;
25
my ($content_sent, $test_name);
29
# Testing with a file handle
31
my $app = StreamTest->new();
34
$content_sent = $stdout->read;
36
$test_name = "with fh: Content-Disposition and filename headers are correct";
37
like($content_sent, qr/Content-Disposition: attachment; filename="FILE"/i,$test_name);
39
$test_name = 'with fh: Content-type detected correctly by File::MMagic';
40
like($content_sent, qr!Content-Type: text/plain!i, $test_name);
42
$test_name = 'with fh: correct Content-Length header found';
43
like($content_sent, qr/Content-Length: 29/i,$test_name);
46
$app = StreamTest->new();
49
$content_sent = $stdout->read;
50
$test_name = "Content-Disposition and filename headers are correct";
51
like($content_sent, qr/Content-Disposition: attachment; filename="test_file_to_stream.txt"/i,$test_name);
53
$test_name = 'Content-type detected correctly by File::MMagic';
54
like($content_sent, qr!Content-Type: text/plain!i, $test_name);
56
$test_name = 'correct Content-Length header found';
57
like($content_sent, qr/Content-Length: 29/i,$test_name);
61
$test_name = 'Setting a custom Content-Length';
62
$app = StreamTest->new();
63
$app->header_props(-Content_Length => 1 );
65
$content_sent = $stdout->read;
66
like($content_sent, qr/Content-Length: 1/i,$test_name);
70
$test_name = 'Setting a custom -Content-Length';
71
$app = StreamTest->new();
72
$app->header_props(-Content_Length => 4 );
74
$content_sent = $stdout->read;
75
like($content_sent, qr/Content-Length: 4/i,$test_name);
79
$test_name = 'Setting a custom type';
80
$app = StreamTest->new();
81
$app->header_props(type => 'jelly/bean' );
83
$content_sent = $stdout->read;
84
like($content_sent, qr/jelly/i,$test_name);
88
$test_name = 'Setting a custom -type';
89
$app = StreamTest->new();
90
$app->header_props(-type => 'recumbent/bicycle' );
92
$content_sent = $stdout->read;
93
like($content_sent, qr/recumbent/i,$test_name);
97
$test_name = 'Setting a custom attachment';
98
$app = StreamTest->new();
99
$app->header_props(attachment => 'save_the_planet_from_the_humans.txt' );
101
$content_sent = $stdout->read;
102
like($content_sent, qr/save_the_planet/i,$test_name);
106
$test_name = 'Setting a custom -type';
107
$app = StreamTest->new();
108
$app->header_props(-attachment => 'do_some_yoga.mp3' );
110
$content_sent = $stdout->read;
111
like($content_sent, qr/yoga/i,$test_name);
115
$test_name = 'Setting a non-attachment header is preserved';
116
$app = StreamTest->new();
117
$app->header_props(-dryer => 'clothes_line' );
119
$content_sent = $stdout->read;
120
like($content_sent, qr/dryer/i,$test_name);
124
$test_name = 'Setting a explicit byte Content-Length at least doesn\'t die';
125
$app = StreamTest->new();
127
$content_sent = $stdout->read;
128
like($content_sent, qr/Content-type/i,$test_name);
134
use base 'CGI::Application';
135
use CGI::Application::Plugin::Stream (qw/stream_file/);
139
$self->run_modes([qw/start with_fh with_bytes/])
145
return $self->stream_file('t/test_file_to_stream.txt');
152
open($fh,'<t/test_file_to_stream.txt') || die;
153
return $self->stream_file($fh);
158
return $self->stream_file('t/test_file_to_stream.txt',2048);