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