5 use File::Basename qw( dirname );
10 require Test::LongString;
11 Test::LongString->import(max => 128);
13 no warnings 'redefine'; # override module to not escape newlines
14 my $formatter = *Test::LongString::_display;
15 my $parent = \&{$formatter};
18 $s =~ s/\Q\x{0a}/\n /g; # revert newline quoting
21 } or *is_string = \&is; # fallback to ugly unformatted is()
23 eval { require PerlIO::scalar };
24 plan skip_all => "PerlIO required (perl 5.8) to test PLP" if $@;
28 require_ok('PLP::Backend::CGI') or BAIL_OUT();
30 $PLP::use_cache = 0 if $PLP::use_cache;
31 #TODO: caching on (change file names)
33 chdir File::Spec->catdir(dirname($0), '50-cgi')
34 or BAIL_OUT('cannot change to test directory ./50-cgi/');
35 my $ORGDIR = Cwd::getcwd();
36 open ORGOUT, '>&', *STDOUT;
39 my ($test, $src, $expect, $env, $in) = @_;
40 local $Test::Builder::Level = $Test::Builder::Level + 1;
43 REQUEST_METHOD => 'GET',
44 REQUEST_URI => "/$src/test/123",
45 QUERY_STRING => 'test=1&test=2',
46 GATEWAY_INTERFACE => 'CGI/1.1',
48 SCRIPT_NAME => '/plp.cgi',
49 SCRIPT_FILENAME => "$ORGDIR/plp.cgi",
50 PATH_INFO => "/$src/test/123",
51 PATH_TRANSLATED => "$ORGDIR/$src/test/123",
52 DOCUMENT_ROOT => $ORGDIR,
55 ); # Apache/2.2.4 CGI environment
58 $ENV{CONTENT_LENGTH} = length $in;
59 $ENV{CONTENT_TYPE} = 'application/x-www-form-urlencoded';
65 open STDOUT, '>', \my $output; # STDOUT buffered to scalar
66 select STDOUT; # output before start() (which selects PLPOUT)
68 local $SIG{__WARN__} = sub {
69 # include warnings in stdout (but modified to distinguish)
71 my $eol = $msg =~ s/(\s*\z)// && $1;
72 print "<warning>$msg</warning>$eol"
77 select ORGOUT; # return to original STDOUT
81 diag(" Error: $failure");
84 $output =~ s{((?:.+\n)*)}{ join "", sort split /(?<=\n)/, $1 }e; # order headers
85 is_string($output, $expect, $test);
89 # captures the first warning produced by the given code string
90 my ($code, $line, $file) = @_;
92 local $SIG{__WARN__} = sub { die @_ };
93 # warnings module runs at BEGIN, so we need to use icky expression evals
94 eval qq(# line $line "$file"\n$code; return);
101 my ($file, %replace) = @_;
103 (my $name = $file) =~ s/[.][^.]+$//;
104 $file = "$name.html";
105 my $infile = delete $replace{-input} // "$name.plp";
106 my $addin = -e "$name.txt" && "$name.txt";
107 $name =~ s/^(\d*)-// and $name .= " ($1)";
110 local $/ = undef; # slurp
111 open my $fh, '<', $file or die "$!\n";
114 if (not defined $out) {
116 diag("error reading output from $file: $@");
120 my $env = delete $replace{-env};
121 $replace{HEAD} //= "Content-Type: text/html\nX-PLP-Version: $PLP::VERSION\n";
122 $replace{VERSION } //= $PLP::VERSION;
123 $replace{SCRIPT_NAME } //= $infile;
124 $replace{SCRIPT_FILENAME} //= "$ORGDIR/$infile";
127 $out =~ s/\$$_/$replace{$_}/g for keys %replace;
129 <eval \s+ line="([^"]*)"> (.*?) </eval>
130 }{ getwarning($2, $1, $infile) }msxge;
132 plp_is($name, $infile, $out, $env, $addin);
135 # 0*: permission checks using generated dummy files
137 for my $file (glob '0*.html') {
138 $file =~ s/[.]html$/.plp/;
139 my ($mode) = $file =~ /^..-(\d*)\b/;
142 return 1; # do not create
146 open my $out, '>', $file or die "cannot generate source file ($!)\n";
150 chmod 0244, $file or die "cannot change permissions ($!)\n";
154 } or chomp $@, skip("$file: $@", 1); # ignore generation failure
157 eval { unlink $file }; # clean up
160 # 1*-2*: generic tests with standard environment
161 plp_ok($_) for glob '[12]*.html';
163 # 3*: error tests depending on warning message
165 my @inctests = glob '3*.html';
167 my $INCFILE = File::Spec->rel2abs("$ORGDIR/missinginclude");
168 if (open my $dummy, "<", $INCFILE) { # like PLP::source will
169 fail("file missinginclude shouldn't exist");
170 skip("missinginclude tests (3*)", @inctests - 1);
172 my $INCWARN = qq{Can't open "$INCFILE" ($!)};
174 plp_ok($_, INCWARN => $INCWARN) for @inctests;
177 # 4*-7*: apache environment (default)
178 plp_ok($_) for glob '[4-7]*.html';
183 # 8*: lighttpd environment
185 # lighttpd/1.4.7 CGI environment
186 REQUEST_METHOD => 'GET',
187 REQUEST_URI => "/$_/test/123",
188 QUERY_STRING => 'test=1&test=2',
189 GATEWAY_INTERFACE => 'CGI/1.1',
191 SCRIPT_NAME => "/$_", #XXX: .plp?
192 SCRIPT_FILENAME => "$ORGDIR/$_",
193 PATH_INFO => '/test/123',
194 PATH_TRANSLATED => undef,
195 DOCUMENT_ROOT => undef,
196 }) for glob '8*.plp';