stas        2003/09/22 16:34:46

  Added:       t/modperl cookie.t
               t/response/TestModperl cookie.pm
  Removed:     t/apache cookie2.t
               t/response/TestApache cookie2.pm
  Log:
  - move the modperl cookie tests to be before perl-script tests, also change
    the location to t/modperl/ as these aren't apache tests.
  - extend testing to check that the cookie value can get into %ENV via
    $r->subprocess_env and that it doesn't persist on the next request
    $r->which doesn't provide the Cookie header
  
  Revision  Changes    Path
  1.1                  modperl-2.0/t/modperl/cookie.t
  
  Index: cookie.t
  ===================================================================
  use strict;
  use warnings FATAL => 'all';
  
  # The Cookie HTTP header can be accessed via $r->headers_in and in certain
  # situations via $ENV{HTTP_COOKIE}.
  #
  # in this test we should be able get the cookie via %ENV,
  # since 'SetHandler perl-script' sets up mod_cgi env var. Moreover
  # adding 'PerlOptions +SetupEnv' adds them at the very first stage used
  # by mod_perl handlers, 'access' in this test. the last sub-test makes
  # sure, that mod_cgi env vars don't persist and are properly re-set at
  # the end of each request
  #
  # since the test is run against the same interpreter we also test that
  # the cookie value doesn't persist if it makes it to %ENV.
  
  
  use Apache::Test;
  use Apache::TestUtil;
  use Apache::TestRequest;
  
  plan tests => 3;
  
  my $module   = 'TestModperl::cookie';
  my $location = '/' . Apache::TestRequest::module2path($module);
  
  my $cookie = 'foo=bar';
  my %cookies = (
       header   => $cookie,
       env      => $cookie,
       nocookie => '',
  );
  
  # 'nocookie' must be run last, server-side shouldn't find a cookie
  # (testing that %ENV is reset to its original values for vars set by
  # $r->subprocess_env, which is run internally for 'perl-script')
  # this requires that all the tests are run against the same interpter
  
  my @tests_ordered = qw(header env nocookie);
  
  t_debug "getting the same interp ID for $location";
  my $same_interp = Apache::TestRequest::same_interp_tie($location);
  
  my $skip = $same_interp ? 0 : 1;
  for my $test (@tests_ordered) {
      my $expected = $test eq 'nocookie' ? '' : "bar";
      my @headers = ();
      push @headers, (Cookie => $cookies{$test}) unless $test eq 'nocookie';
  
      my $received = get_body($same_interp, \&GET, "$location?$test", @headers);
      $skip++ unless defined $received;
      skip_not_same_interp(
          $skip,
          $expected,
          $received,
          "perl-script+SetupEnv/cookie: $test"
      );
  }
  
  # if we fail to find the same interpreter, return undef (this is not
  # an error)
  sub get_body {
      my $res = eval {
          Apache::TestRequest::same_interp_do(@_);
      };
      return undef if $@ =~ /unable to find interp/;
      return $res->content if $res;
      die $@ if $@;
  }
  
  # make the tests resistant to a failure of finding the same perl
  # interpreter, which happens randomly and not an error.
  # the first argument is used to decide whether to skip the sub-test,
  # the rest of the arguments are passed to 'ok t_cmp';
  sub skip_not_same_interp {
      my $skip_cond = shift;
      if ($skip_cond) {
          skip "Skip couldn't find the same interpreter";
      }
      else {
          my($package, $filename, $line) = caller;
          # trick ok() into reporting the caller filename/line when a
          # sub-test fails in sok()
          return eval <<EOE;
  #line $line $filename
      ok &t_cmp;
  EOE
      }
  }
  
  
  
  1.1                  modperl-2.0/t/response/TestModperl/cookie.pm
  
  Index: cookie.pm
  ===================================================================
  package TestModperl::cookie;
  
  use strict;
  use warnings FATAL => 'all';
  
  use Apache::TestTrace;
  
  use Apache::RequestRec ();
  use Apache::RequestIO ();
  
  use Apache::Const -compile => 'OK';
  
  sub access {
      my $r = shift;
  
      my($key, $val) = cookie($r);
      my $cookie_is_expected =
          ($r->args eq 'header' or $r->args eq 'env') ? 1 : 0;
      die "Can't get the cookie" if $cookie_is_expected && !defined $val;
  
      return Apache::OK;
  }
  
  sub handler {
      my $r = shift;
  
      my($key, $val) = cookie($r);
      $r->print($val) if defined $val;
  
      return Apache::OK;
  }
  
  sub cookie {
      my $r = shift;
  
      my $header = $r->headers_in->{Cookie} || '';
      my $env    = $ENV{HTTP_COOKIE} || $ENV{COOKIE} || ''; # from CGI::Cookie
      debug "cookie (" .$r->args . "): header: [$header], env: [$env]";
  
      return split '=', $r->args eq 'header' ? $header : $env;
  }
  
  1;
  
  __DATA__
  SetHandler perl-script
  PerlModule          TestModperl::cookie
  PerlInitHandler     Apache::TestHandler::same_interp_fixup
  PerlAccessHandler   TestModperl::cookie::access
  PerlResponseHandler TestModperl::cookie
  # PerlOptions +SetupEnv is needed here, because we want the mod_cgi
  # env to be set at the access phase. without it, perl-script sets it
  # only for the response phase
  PerlOptions +SetupEnv
  
  
  

Reply via email to