~ubuntu-branches/ubuntu/utopic/libcgi-application-plugin-stream-perl/utopic

« back to all changes in this revision

Viewing changes to t/basic.t

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2013-12-23 20:13:46 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131223201346-x6gy06702g1judoc
Tags: 2.11-1
* Team upload.
* New upstream release.
* Drop 0001-Fix-a-test-failure-due-triggered-by-hash-randomizati.patch.
  Merged  upstream.
* Update build dependencies.
* Declare compliance with Debian Policy 3.9.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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'
3
 
 
4
 
#########################
5
 
 
6
 
use Test::More tests => 15;
7
 
BEGIN {
8
 
  use_ok('CGI::Application::Plugin::Stream');
9
 
 
10
 
  unshift @INC, 't/lib';
11
 
}
12
 
 
13
 
use strict;
14
 
use TieOut;
15
 
 
16
 
 
17
 
 
18
 
# Useless here, since the point is to test streaming directly.
19
 
#$ENV{CGI_APP_RETURN_ONLY} = 1;
20
 
 
21
 
#####
22
 
 
23
 
 
24
 
my $stdout = tie *STDOUT, 'TieOut' or die;
25
 
my ($content_sent, $test_name);
26
 
 
27
 
##############
28
 
 
29
 
# Testing with a file handle
30
 
 
31
 
my $app = StreamTest->new();
32
 
$app->with_fh();
33
 
 
34
 
$content_sent = $stdout->read;
35
 
 
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);
38
 
 
39
 
$test_name    = 'with fh: Content-type detected correctly by File::MMagic';
40
 
like($content_sent, qr!Content-Type: text/plain!i, $test_name);
41
 
 
42
 
$test_name    = 'with fh: correct Content-Length  header found';
43
 
like($content_sent, qr/Content-Length: 29/i,$test_name);
44
 
 
45
 
# Testing with a file
46
 
$app = StreamTest->new();
47
 
$app->run();
48
 
 
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);
52
 
 
53
 
$test_name = 'Content-type detected correctly by File::MMagic';
54
 
like($content_sent, qr!Content-Type: text/plain!i, $test_name);
55
 
 
56
 
$test_name = 'correct Content-Length header found';
57
 
like($content_sent, qr/Content-Length: 29/i,$test_name);
58
 
 
59
 
###
60
 
 
61
 
$test_name = 'Setting a custom Content-Length';
62
 
$app = StreamTest->new();
63
 
$app->header_props(-Content_Length => 1 );
64
 
$app->with_fh();
65
 
$content_sent = $stdout->read;
66
 
like($content_sent, qr/Content-Length: 1/i,$test_name);
67
 
 
68
 
###
69
 
 
70
 
$test_name = 'Setting a custom -Content-Length';
71
 
$app = StreamTest->new();
72
 
$app->header_props(-Content_Length => 4 );
73
 
$app->with_fh();
74
 
$content_sent = $stdout->read;
75
 
like($content_sent, qr/Content-Length: 4/i,$test_name);
76
 
 
77
 
###
78
 
 
79
 
$test_name = 'Setting a custom type';
80
 
$app = StreamTest->new();
81
 
$app->header_props(type => 'jelly/bean' );
82
 
$app->with_fh();
83
 
$content_sent = $stdout->read;
84
 
like($content_sent, qr/jelly/i,$test_name);
85
 
 
86
 
###
87
 
 
88
 
$test_name = 'Setting a custom -type';
89
 
$app = StreamTest->new();
90
 
$app->header_props(-type => 'recumbent/bicycle' );
91
 
$app->with_fh();
92
 
$content_sent = $stdout->read;
93
 
like($content_sent, qr/recumbent/i,$test_name);
94
 
 
95
 
###
96
 
 
97
 
$test_name = 'Setting a custom attachment';
98
 
$app = StreamTest->new();
99
 
$app->header_props(attachment => 'save_the_planet_from_the_humans.txt' );
100
 
$app->with_fh();
101
 
$content_sent = $stdout->read;
102
 
like($content_sent, qr/save_the_planet/i,$test_name);
103
 
 
104
 
###
105
 
 
106
 
$test_name = 'Setting a custom -type';
107
 
$app = StreamTest->new();
108
 
$app->header_props(-attachment => 'do_some_yoga.mp3' );
109
 
$app->with_fh();
110
 
$content_sent = $stdout->read;
111
 
like($content_sent, qr/yoga/i,$test_name);
112
 
 
113
 
###
114
 
 
115
 
$test_name = 'Setting a non-attachment header is preserved';
116
 
$app = StreamTest->new();
117
 
$app->header_props(-dryer => 'clothes_line' );
118
 
$app->with_fh();
119
 
$content_sent = $stdout->read;
120
 
like($content_sent, qr/dryer/i,$test_name);
121
 
 
122
 
###
123
 
 
124
 
$test_name = 'Setting a explicit byte Content-Length at least doesn\'t die';
125
 
$app = StreamTest->new();
126
 
$app->with_bytes();
127
 
$content_sent = $stdout->read;
128
 
like($content_sent, qr/Content-type/i,$test_name);
129
 
 
130
 
 
131
 
#################
132
 
 
133
 
package StreamTest;
134
 
use base 'CGI::Application';
135
 
use CGI::Application::Plugin::Stream (qw/stream_file/);
136
 
 
137
 
sub setup {
138
 
    my $self = shift;
139
 
    $self->run_modes([qw/start with_fh with_bytes/])
140
 
}
141
 
 
142
 
 
143
 
sub start {
144
 
    my $self = shift;
145
 
    return $self->stream_file('t/test_file_to_stream.txt');
146
 
}
147
 
 
148
 
sub with_fh  {
149
 
    my $self = shift;
150
 
 
151
 
    my $fh;
152
 
    open($fh,'<t/test_file_to_stream.txt') || die;
153
 
    return $self->stream_file($fh);
154
 
}
155
 
 
156
 
sub with_bytes  {
157
 
    my $self = shift;
158
 
    return $self->stream_file('t/test_file_to_stream.txt',2048);
159
 
}
160
 
 
161
 
1;