9 require Test::LongString;
10 Test::LongString->import(max => 128);
12 no warnings 'redefine'; # override module to not escape newlines
13 my $formatter = *Test::LongString::_display;
14 my $parent = \&{$formatter};
17 $s =~ s/\Q\x{0a}/\n /g; # revert newline quoting
20 } or *is_string = \&is; # fallback to ugly unformatted is()
22 eval { require PerlIO::scalar };
23 plan skip_all => "PerlIO required (perl 5.8) to test PLP" if $@;
27 require_ok('PLP::Backend::CGI') or BAIL_OUT();
29 $PLP::use_cache = 0 if $PLP::use_cache;
30 #TODO: caching on (change file names)
32 my $base = Cwd::abs_path(File::Spec->tmpdir || File::Spec->curdir);
33 -w $base or BAIL_OUT("$base not writable");
34 my $testfile = 'testfile.plp';
35 not -f "$base/$testfile" or BAIL_OUT("$testfile exists");
37 open ORGOUT, '>&', *STDOUT;
40 my ($test, $plp, $expect, $env) = @_;
42 local $Test::Builder::Level = $Test::Builder::Level + 1;
45 REQUEST_METHOD => 'GET',
46 REQUEST_URI => "/$testfile/test/123",
47 QUERY_STRING => 'test=1&test=2',
48 GATEWAY_INTERFACE => 'CGI/1.1',
50 SCRIPT_NAME => '/plp.cgi',
51 SCRIPT_FILENAME => "$base/plp.cgi",
52 PATH_INFO => "/$testfile/test/123",
53 PATH_TRANSLATED => "$base/$testfile/test/123",
54 DOCUMENT_ROOT => $base,
57 ); # Apache/2.2.4 CGI environment
61 open my $testfh, '>', "$base/$testfile" or die $!;
62 print {$testfh} $plp or die $!;
63 close $testfh or die $!;
65 not $@ or fail("write $testfile"), diag(" Error: $@"), return;
69 open STDOUT, '>', \my $output; # STDOUT buffered to scalar
70 select STDOUT; # output before start() (which selects PLPOUT)
72 local $SIG{__WARN__} = sub {
73 # include warnings in stdout (but modified to distinguish)
75 my $eol = $msg =~ s/(\s*\z)// && $1;
76 print "<warning>$msg</warning>$eol"
81 select ORGOUT; # return to original STDOUT
85 diag(" Error: $failure");
88 $output =~ s{((?:.+\n)*)}{ join "", sort split /(?<=\n)/, $1 }e; # order headers
89 is_string($output, $expect, $test);
93 # captures the first warning produced by the given code string
94 my ($code, $line, $file) = @_;
96 local $SIG{__WARN__} = sub { die @_ };
97 # warnings module runs at BEGIN, so we need to use icky expression evals
98 eval qq(# line $line "$file"\n$code; return);
104 my $HEAD = <<EOT; # common header output
105 Content-Type: text/html
106 X-PLP-Version: $PLP::VERSION
109 plp_is('print', '0<: print print 2 :>3', "$HEAD\n0213");
111 plp_is('exit', '1<:exit:>not <(reached)>', "$HEAD\n1");
113 plp_is('<:=', '1<:=$foo=2:>3<:= $foo', "$HEAD\n1232");
115 plp_is('%get', '<: print $get{test} if defined $get{test} and not exists $get{test2}', "$HEAD\n2\n");
117 plp_is('%get array', '<:= @{$get{q/@test/}}', "$HEAD\n12\n");
120 '<: $headers{_test}=2; print $header{x_PLP_version}; BEGIN { $header{"-tesT"}=1 }',
121 "-tesT: 2\n$HEAD\n$PLP::VERSION"
124 plp_is('%header repetition', '.<: BEGIN{$header{A}="1\n2"} $header{A}=3', <<TEST);
128 .<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>Can't set headers after sending them at testfile.plp line 1.
129 (Output started at testfile.plp line 1.)
137 plp_is('PLP_END', '<: PLP_END{print 1}; PLP_END{print 2}; print 3', "$HEAD\n321");
139 plp_is('no warnings by default', '<: ignoreme :>ok', "$HEAD\nok");
141 rename "$base/$testfile", "$base/$testfile.inc";
142 plp_is('include', "<($testfile.inc)> <: include '$testfile.inc'", "$HEAD\nok ok");
143 unlink "$base/$testfile.inc";
145 my $SYNTAXERR = getwarning("q\cq\n\cq; syntax(error", 1, $testfile);
146 plp_is('fatal error', "runtime\n<: syntax(error :>\nruntime", <<TEST);
148 <table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>$SYNTAXERR
154 my $INCFILE = File::Spec->rel2abs("$base/missinginclude");
155 if (open my $dummy, "<", $INCFILE) { # like PLP::source will
156 fail("file missinginclude shouldn't exist");
157 skip("missinginclude tests", 2);
159 my $INCWARN = qq{Can't open "$INCFILE" ($!)};
161 my $VOIDWARN = getwarning('42', 2, $testfile);
163 plp_is('warnings', split /\n\n/, <<TEST, 2);
166 3<: warn "warning" :>
167 4<: include "missinginclude" :>
172 <warning>${VOIDWARN}</warning>
175 3<warning>warning at $testfile line 3.</warning>
177 4<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>$INCWARN at $testfile line 4.
179 5<table border=1 class="PLPerror"><tr><td><b>Debug information:</b><br>$INCWARN at $testfile line 5.
183 plp_is('$PLP::ERROR',
184 '<: $PLP::ERROR = sub {print "Oh no: $_[0]"} :> <(missinginclude)>.',
185 qq{$HEAD\n Oh no: $INCWARN at $testfile line 1.\n\n}
188 plp_is('$PLP::DEBUG',
189 '<: $PLP::DEBUG = 2; delete $header{x_plp_version} :>1<(missinginclude)>2',
190 "Content-Type: text/plain\n\nContent-Type: text/html\n\n1"
195 plp_is('utf8', '<: use open qw/:std :utf8/; print chr 191', <<TEST);
196 Content-Type: text/html; charset=utf-8
197 X-PLP-Version: $PLP::VERSION
203 'ok <:=$ENV{SCRIPT_NAME}:> <:=$ENV{SCRIPT_FILENAME}',
204 "$HEAD\nok /$testfile $base/$testfile"
207 plp_is('%ENV (on apache)', @envtest);
210 chmod 0244, $testfile or skip("changed permissions", 1);
211 plp_is('permission denied', undef, <<TEST);
212 <warning>PLP: Can't read: $base/$testfile (/$testfile/test/123)</warning>
213 Content-Type: text/html
216 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
218 <title>403 Forbidden</title>
221 You don't have permission to access /$testfile/test/123 on this server.<p>
225 chmod 0644, $testfile;
228 my %nf = (PATH_TRANSLATED => "$base/missinginclude/test/123");
229 plp_is('not found', undef, <<TEST, \%nf);
230 <warning>PLP: Not found: $base/missinginclude/test/123 (/$testfile/test/123)</warning>
231 Content-Type: text/html
234 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
236 <title>404 Not Found</title>
239 The requested URL /$testfile/test/123 was not found on this server.<p>
244 plp_is('%ENV on lighttpd', @envtest, {
245 # lighttpd/1.4.7 CGI environment
246 REQUEST_METHOD => 'GET',
247 REQUEST_URI => "/$testfile/test/123",
248 QUERY_STRING => 'test=1&test=2',
249 GATEWAY_INTERFACE => 'CGI/1.1',
251 SCRIPT_NAME => "/$testfile", #XXX: .plp?
252 SCRIPT_FILENAME => "$base/$testfile",
253 PATH_INFO => '/test/123',
254 PATH_TRANSLATED => undef,
255 DOCUMENT_ROOT => undef,
258 unlink "$base/$testfile";