package TestModperl::env;

use strict;
use warnings FATAL => 'all';

use Apache::RequestRec ();
use APR::Table ();

use Apache::Test;
use Apache::TestUtil;

use Apache::Const -compile => 'OK';

sub handler {
    my $r = shift;

    plan $r, tests => 20 + keys(%ENV);

    my $env = $r->subprocess_env;

    ok $ENV{MODPERL_EXTRA_PL}; #set in t/conf/modperl_extra.pl
    ok $ENV{MOD_PERL};

    $ENV{FOO} = 2;
    ok $ENV{FOO} == 2;
    ok $env->get('FOO') == 2;

    $ENV{FOO}++;
    ok $ENV{FOO} == 3;
    ok $env->get('FOO') == 3;

    $ENV{FOO} .= 6;
    ok $ENV{FOO} == 36;
    ok $env->get('FOO') == 36;

    delete $ENV{FOO};
    ok ! $ENV{FOO};
    ok ! $env->get('FOO');

    ok $ENV{SERVER_SOFTWARE};
    ok $env->get('SERVER_SOFTWARE');

    {
        local %ENV = (FOO => 1, BAR => 2);

        ok $ENV{FOO} == 1;
        ok $env->get('FOO') == 1;

        ok ! $ENV{SERVER_SOFTWARE};
        ok ! $env->get('SERVER_SOFTWARE');
    }

    ok ! $ENV{FOO};
    #ok ! $env->get('FOO');
    #XXX: keys in the original subprocess_env are restored
    #     but new ones added to the local %ENV are not removed
    #     after the local %ENV goes out of scope
    #skip "r->subprocess_env + local() doesnt fully work yet", 1;
    ok 1; #the skip() message is just annoying

    ok $ENV{SERVER_SOFTWARE};
    ok $env->get('SERVER_SOFTWARE');

    #Make sure each key can be deleted

    for my $key (sort keys %ENV) {
        eval { delete $ENV{$key}; };
        ok t_cmp('', $@, $key);
    }

    Apache::OK;
}

1;
__END__
SetHandler perl-script
