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