warn pre-start() errors
[perl/plp/.git] / t / 50-cgi.t
1 use strict;
2 use warnings;
3
4 use File::Spec;
5 use Test::More;
6
7 eval { require PerlIO::scalar };
8 plan skip_all => "PerlIO required (perl 5.8) to test PLP" if $@;
9
10 plan tests => 20;
11
12 require_ok('PLP::Backend::CGI') or BAIL_OUT();
13
14 $PLP::use_cache = 0 if $PLP::use_cache;
15 #TODO: caching on (change file names)
16
17 my $base = File::Spec->tmpdir || File::Spec->curdir;
18 -w $base or BAIL_OUT("$base not writable");
19 my $testfile = 'testfile.plp';
20 not -f "$base/$testfile" or BAIL_OUT("$testfile exists");
21
22 open ORGOUT, '>&', *STDOUT;
23
24 sub plp_is {
25         my ($test, $plp, $expect) = @_;
26         chomp $expect;
27         local $Test::Builder::Level = $Test::Builder::Level + 1;
28
29         if (defined $plp) {
30                 eval {
31                         open my $testfh, '>', "$base/$testfile" or die $!;
32                         print {$testfh} $plp or die $!;
33                         close $testfh or die $!;
34                 };
35                 not $@ or fail("write $testfile"), diag("    Error: $@"), return;
36         }
37
38         close STDOUT;
39         open STDOUT, '>', \my $output;  # STDOUT buffered to scalar
40         select STDOUT;  # output before start() (which selects PLPOUT)
41         eval {
42                 local $SIG{__WARN__} = sub { print $_[0] }; # enables warnings
43                 PLP::everything();
44         };
45         select ORGOUT;  # return to original STDOUT
46
47         not $@ or fail($test), diag("    Error: $@"), return;
48         $output =~ s{((?:.+\n)*)}{ join "", sort split /(?<=\n)/, $1 }e; # order headers
49         is($output, $expect, $test);
50 }
51
52 %ENV = (
53         REQUEST_METHOD => 'GET',
54         REQUEST_URI => "/$testfile/test/123",
55         QUERY_STRING => 'test=1&test=2',
56         GATEWAY_INTERFACE => 'CGI/1.1',
57         
58         SCRIPT_NAME => '/plp.cgi',
59         SCRIPT_FILENAME => "$base/plp.cgi",
60         PATH_INFO => "/$testfile/test/123",
61         PATH_TRANSLATED => "$base/$testfile/test/123",
62         DOCUMENT_ROOT => $base,
63 ); # Apache/2.2.4 CGI environment
64
65 my $HEAD = <<EOT;  # common header output
66 Content-Type: text/html
67 X-PLP-Version: $PLP::VERSION
68 EOT
69
70 plp_is('print', '0<: print print 2 :>3', "$HEAD\n0213");
71
72 plp_is('exit', '1<:exit:>not <(reached)>', "$HEAD\n1");
73
74 plp_is('<:=', '1<:=$foo=2:>3<:= $foo', "$HEAD\n1232");
75
76 plp_is('%get', '<: print $get{test} if defined $get{test} and not exists $get{test2}', "$HEAD\n2\n");
77
78 plp_is('%get array', '<:= @{$get{q/@test/}}', "$HEAD\n12\n");
79
80 plp_is('%header',
81         '<: $headers{_test}=2; print $header{x_PLP_version}; BEGIN { $header{"-tesT"}=1 }',
82         "-tesT: 2\n$HEAD\n$PLP::VERSION"
83 );
84
85 plp_is('%header repetition', '.<: BEGIN{$header{A}="1\n2"} $header{A}=3', <<TEST);
86 A: 1
87 A: 2
88 $HEAD
89 .<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>Can't set headers after sending them at testfile.plp line 1.
90 (Output started at testfile.plp line 1.)
91 </td></tr></table>
92 TEST
93
94 #TODO: %post
95 #TODO: %fields
96 #TODO: %cookie
97
98 plp_is('PLP_END', '<: PLP_END{print 1}; PLP_END{print 2}; print 3', "$HEAD\n321");
99
100 plp_is('no warnings by default', '<: ignoreme :>ok', "$HEAD\nok");
101
102 rename "$base/$testfile", "$base/$testfile.inc";
103 plp_is('include', "<($testfile.inc)> <: include '$testfile.inc'", "$HEAD\nok ok");
104 unlink "$base/$testfile.inc";
105
106 plp_is('fatal error', "runtime\n<: syntax(error :>\nruntime", <<TEST);
107 $HEAD
108 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>syntax error at $testfile line 2, at EOF
109   (Might be a runaway multi-line \cq\cq string starting on line 1)
110 </td></tr></table>
111 TEST
112
113 SKIP: {
114
115 my $INCFILE = File::Spec->rel2abs("$base/missinginclude");
116 if (open my $dummy, "<", $INCFILE) {  # like PLP::source will
117         fail("file missinginclude shouldn't exist");
118         skip("missinginclude tests", 2);
119 }
120 my $INCWARN = qq{Can't open "$INCFILE" ($!)};
121
122 plp_is('warnings', split /\n\n/, <<TEST, 2);
123 1
124 <: use warnings :>
125 2
126 <: 42 :>
127 3
128 <: warn "warning" :>
129 4
130 <: include "missinginclude" :>
131 5
132 <(missinginclude)>
133 6
134
135 $HEAD
136 Useless use of a constant in void context at $testfile line 4.
137 1
138
139 2
140
141 3
142 warning at $testfile line 6.
143
144 4
145 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>$INCWARN at $testfile line 8.
146 </td></tr></table>
147 5
148 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>$INCWARN at $testfile line 10.
149 </td></tr></table>
150 TEST
151
152 plp_is('$PLP::ERROR',
153         '<: $PLP::ERROR = sub {print "Oh no: $_[0]"} :> <(missinginclude)>.',
154         qq{$HEAD\n Oh no: $INCWARN at $testfile line 1.\n\n}
155 );
156
157 plp_is('$PLP::DEBUG',
158         '<: $PLP::DEBUG = 2; delete $header{x_plp_version} :>1<(missinginclude)>2',
159         "Content-Type: text/plain\n\nContent-Type: text/html\n\n1"
160 );
161
162 }
163
164 plp_is('utf8', '<: use open qw/:std :utf8/; print chr 191', <<TEST);
165 Content-Type: text/html; charset=utf-8
166 X-PLP-Version: $PLP::VERSION
167
168 \302\277
169 TEST
170
171 my @envtest = (
172         'ok <:=$ENV{SCRIPT_NAME}:> <:=$ENV{SCRIPT_FILENAME}',
173         "$HEAD\nok /$testfile $base/$testfile"
174 );
175
176 plp_is('%ENV (on apache)', @envtest);
177
178 SKIP: {
179 chmod 0244, $testfile or skip("changed permissions", 1);
180 plp_is('permission denied', undef, <<TEST);
181 Content-Type: text/html
182 PLP: Can't read: $base/$testfile (/$testfile/test/123)
183 Status: 403
184
185 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
186 <html><head>
187 <title>403 Forbidden</title>
188 </head></body>
189 <h1>Forbidden</h1>
190 You don't have permission to access /$testfile/test/123 on this server.<p>
191 <hr>
192 </body></html>
193 TEST
194 chmod 0644, $testfile;
195 }
196
197 $ENV{PATH_TRANSLATED} = "$base/missinginclude/test/123";
198 plp_is('not found', undef, <<TEST);
199 Content-Type: text/html
200 PLP: Not found: $base/missinginclude/test/123 (/$testfile/test/123)
201 Status: 404
202
203 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
204 <html><head>
205 <title>404 Not Found</title>
206 </head></body>
207 <h1>Not Found</h1>
208 The requested URL /$testfile/test/123 was not found on this server.<p>
209 <hr>
210 </body></html>
211 TEST
212
213 %ENV = (
214         REQUEST_METHOD => 'GET',
215         REQUEST_URI => "/$testfile/test/123",
216         QUERY_STRING => 'test=1&test=2',
217         GATEWAY_INTERFACE => 'CGI/1.1',
218         
219         SCRIPT_NAME => "/$testfile", #XXX: .plp?
220         SCRIPT_FILENAME => "$base/$testfile",
221         PATH_INFO => '/test/123',
222 ); # lighttpd/1.4.7 CGI environment
223
224 plp_is('%ENV on lighttpd', @envtest);
225
226 unlink "$base/$testfile";
227