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