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