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