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