t/50-cgi: reset environment for each test
[perl/plp/.git] / t / 50-cgi.t
index 111e112beeede499f056d763c356f8649ee33a85..d6c2856de5a6162f35cf4d95c331f3dd5d243c8e 100644 (file)
@@ -5,6 +5,20 @@ use Cwd;
 use File::Spec;
 use Test::More;
 
+eval {
+       require Test::LongString;
+       Test::LongString->import(max => 128);
+
+       no warnings 'redefine';  # override module to not escape newlines
+       my $formatter = *Test::LongString::_display;
+       my $parent = \&{$formatter};
+       *{$formatter} = sub {
+               my $s = &{$parent};
+               $s =~ s/\Q\x{0a}/\n              /g;  # revert newline quoting
+               return $s;
+       };
+} or *is_string = \&is;  # fallback to ugly unformatted is()
+
 eval { require PerlIO::scalar };
 plan skip_all => "PerlIO required (perl 5.8) to test PLP" if $@;
 
@@ -23,10 +37,25 @@ not -f "$base/$testfile" or BAIL_OUT("$testfile exists");
 open ORGOUT, '>&', *STDOUT;
 
 sub plp_is {
-       my ($test, $plp, $expect) = @_;
+       my ($test, $plp, $expect, $env) = @_;
        chomp $expect;
        local $Test::Builder::Level = $Test::Builder::Level + 1;
 
+       %ENV = (
+               REQUEST_METHOD => 'GET',
+               REQUEST_URI => "/$testfile/test/123",
+               QUERY_STRING => 'test=1&test=2',
+               GATEWAY_INTERFACE => 'CGI/1.1',
+               
+               SCRIPT_NAME => '/plp.cgi',
+               SCRIPT_FILENAME => "$base/plp.cgi",
+               PATH_INFO => "/$testfile/test/123",
+               PATH_TRANSLATED => "$base/$testfile/test/123",
+               DOCUMENT_ROOT => $base,
+               
+               $env ? %{$env} : (),
+       ); # Apache/2.2.4 CGI environment
+
        if (defined $plp) {
                eval {
                        open my $testfh, '>', "$base/$testfile" or die $!;
@@ -57,7 +86,7 @@ sub plp_is {
                return;
        }
        $output =~ s{((?:.+\n)*)}{ join "", sort split /(?<=\n)/, $1 }e; # order headers
-       is($output, $expect, $test);
+       is_string($output, $expect, $test);
 }
 
 sub getwarning {
@@ -72,19 +101,6 @@ sub getwarning {
        return $res;
 };
 
-%ENV = (
-       REQUEST_METHOD => 'GET',
-       REQUEST_URI => "/$testfile/test/123",
-       QUERY_STRING => 'test=1&test=2',
-       GATEWAY_INTERFACE => 'CGI/1.1',
-       
-       SCRIPT_NAME => '/plp.cgi',
-       SCRIPT_FILENAME => "$base/plp.cgi",
-       PATH_INFO => "/$testfile/test/123",
-       PATH_TRANSLATED => "$base/$testfile/test/123",
-       DOCUMENT_ROOT => $base,
-); # Apache/2.2.4 CGI environment
-
 my $HEAD = <<EOT;  # common header output
 Content-Type: text/html
 X-PLP-Version: $PLP::VERSION
@@ -209,8 +225,8 @@ TEST
 chmod 0644, $testfile;
 }
 
-$ENV{PATH_TRANSLATED} = "$base/missinginclude/test/123";
-plp_is('not found', undef, <<TEST);
+my %nf = (PATH_TRANSLATED => "$base/missinginclude/test/123");
+plp_is('not found', undef, <<TEST, \%nf);
 <warning>PLP: Not found: $base/missinginclude/test/123 (/$testfile/test/123)</warning>
 Content-Type: text/html
 Status: 404
@@ -225,7 +241,8 @@ The requested URL /$testfile/test/123 was not found on this server.<p>
 </body></html>
 TEST
 
-%ENV = (
+plp_is('%ENV on lighttpd', @envtest, {
+       # lighttpd/1.4.7 CGI environment
        REQUEST_METHOD => 'GET',
        REQUEST_URI => "/$testfile/test/123",
        QUERY_STRING => 'test=1&test=2',
@@ -234,9 +251,9 @@ TEST
        SCRIPT_NAME => "/$testfile", #XXX: .plp?
        SCRIPT_FILENAME => "$base/$testfile",
        PATH_INFO => '/test/123',
-); # lighttpd/1.4.7 CGI environment
-
-plp_is('%ENV on lighttpd', @envtest);
+       PATH_TRANSLATED => undef,
+       DOCUMENT_ROOT => undef,
+});
 
 unlink "$base/$testfile";