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