[LON-CAPA-cvs] cvs: modules /jerf/tests ApacheRequest.pm ApacheRequestTest.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Fri, 30 May 2003 20:07:53 -0000


bowersj2		Fri May 30 16:07:53 2003 EDT

  Modified files:              
    /modules/jerf/tests	ApacheRequest.pm ApacheRequestTest.pm 
  Log:
  Support passing in an %ENV to a handler, which can be used to simulate
  form entries and such. Test for validity of the %ENV support included.
  
  
  
Index: modules/jerf/tests/ApacheRequest.pm
diff -u modules/jerf/tests/ApacheRequest.pm:1.3 modules/jerf/tests/ApacheRequest.pm:1.4
--- modules/jerf/tests/ApacheRequest.pm:1.3	Thu May 29 16:21:39 2003
+++ modules/jerf/tests/ApacheRequest.pm	Fri May 30 16:07:52 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Apache Request Simulator
 #
-# $Id: ApacheRequest.pm,v 1.3 2003/05/29 20:21:39 bowersj2 Exp $
+# $Id: ApacheRequest.pm,v 1.4 2003/05/30 20:07:52 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -144,7 +144,7 @@
     my $self = shift; # the args array
     if (!defined($self)) { $self = {}; }
 
-    # Initial ENV overriding; trash the old env
+    # Initial ENV overriding; totally trash the old env
     foreach (keys %ENV) {
         delete $ENV{$_};
     }
@@ -366,7 +366,25 @@
 
     eval "use $handlerPackageName;";
 
+    # Replace/augment the %ENV if necessary
+    # We remember old values and put them back at the end of this call,
+    # UNLESS the handler did something to change the %ENV, in which case
+    # we preserve the changes
+    my $oldenvvalues = {};
+    if ($self->{env}) {
+	for (keys %{$self->{env}}) {
+	    $oldenvvalues->{$_} = $ENV{$_};
+	    $ENV{$_} = $self->{env}->{$_};
+	}
+    }
     $self->{return_value} = eval "${handlerPackageName}::handler(" . '$self);';
+
+    # Replace the ENV
+    for (keys (%{$self->{env}})) {
+	if ($self->{env}->{$_} eq $ENV{$_}) {
+	    $ENV{$_} = $oldenvvalues->{$_};
+	}
+    }
     die $@ if $@;
     return;
 }
Index: modules/jerf/tests/ApacheRequestTest.pm
diff -u modules/jerf/tests/ApacheRequestTest.pm:1.3 modules/jerf/tests/ApacheRequestTest.pm:1.4
--- modules/jerf/tests/ApacheRequestTest.pm:1.3	Thu May 29 16:21:56 2003
+++ modules/jerf/tests/ApacheRequestTest.pm	Fri May 30 16:07:52 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Apache Request Simulator Tester
 #
-# $Id: ApacheRequestTest.pm,v 1.3 2003/05/29 20:21:56 bowersj2 Exp $
+# $Id: ApacheRequestTest.pm,v 1.4 2003/05/30 20:07:52 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -233,6 +233,17 @@
     $self->assert($ENV{'DOCUMENT_ROOT'} eq $Apache::lonnet::perlvar{'lonDocRoot'});
 }
 
+sub test_env_handled_right {
+    # Test that passing in an environment doesn't corrupt our environment,
+    # and that changes the handler makes are kept.
+    my $self = shift;
+    $ENV{'todd'} = 'g4tg';
+    my $r = ApacheRequest->new({ env => { 'todd' => 'no',
+					  'shouldntbeseen' => 'fe'}});
+    $r->doHandler("SimpleHandlerEnvChanger");
+    $self->assert(!defined($ENV{'shouldnebeseen'}));
+    $self->assert($ENV{'todd'} eq 'steve');
+}
 1;
 
 package SimpleTestHandler;
@@ -249,6 +260,21 @@
     $r->send_http_header();
 
     $r->print("This is a simple test print.");
+
+    return OK;
+}
+
+1;
+
+package SimpleHandlerEnvChanger;
+
+use lib '.';
+use Apache::Constants qw(:common);
+
+sub handler {
+    my $r = shift;
+
+    $ENV{'todd'} = 'steve';
 
     return OK;
 }