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

bowersj2 lon-capa-cvs@mail.lon-capa.org
Fri, 23 May 2003 18:10:45 -0000


This is a MIME encoded message

--bowersj21053713445
Content-Type: text/plain

bowersj2		Fri May 23 14:10:45 2003 EDT

  Modified files:              
    /modules/jerf/tests	ApacheRequest.pm ApacheRequestTest.pm 
  Log:
  Enough infrastructure in place to run very simple test handlers. 
  Starting to work on getting lonhelp.pm to run outside of the server, as 
  it's one of the simplest handlers we've got.
  
  
  
--bowersj21053713445
Content-Type: text/plain
Content-Disposition: attachment; filename="bowersj2-20030523141045.txt"

Index: modules/jerf/tests/ApacheRequest.pm
diff -u modules/jerf/tests/ApacheRequest.pm:1.1 modules/jerf/tests/ApacheRequest.pm:1.2
--- modules/jerf/tests/ApacheRequest.pm:1.1	Thu May 22 16:47:15 2003
+++ modules/jerf/tests/ApacheRequest.pm	Fri May 23 14:10:45 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: ApacheRequest.pm,v 1.1 2003/05/22 20:47:15 bowersj2 Exp $
+# $Id: ApacheRequest.pm,v 1.2 2003/05/23 18:10:45 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -61,6 +61,9 @@
     or it can be a hash ref containing name => value pairs, in which case
     it will be converted to the appropriate string for you.
 
+=item * I<postcontent>: The posted content for the request, which may also
+    be passed as a string or a hash ref.
+
 =item * I<stuff>: Stuff.
 
 =back
@@ -90,6 +93,7 @@
 # my constants
 sub RFLUSH { return '*-*RFLUSH*-*RFLUSH*-*RFLUSH*-*'; }
 sub HEADER { return '*-*HEADER*-*HEADER*-*HEADER*-*'; }
+sub NOT_YET_RUN { return 'NOT_YET_RUN'; }
 
 sub new {
     my $proto = shift;
@@ -99,16 +103,18 @@
 
     $self->{OUTPUT} = [];
 
-    # Handle args, if any
-    if (defined($self->{args})) {
-        if (ref($self->{args})) {
-            my $args = [];
-            foreach (keys(%{$self->{args}})) {
-                push @$args, escape($_) . '=' . escape($self->{args}->{$_});
+    # Handle args & post, if any
+    for my $input ('querystring', 'postcontent') {
+        if (defined($self->{$input})) {
+            if (ref($self->{$input})) {
+                my $args = [];
+                foreach (keys(%{$self->{$input}})) {
+                    push @$args, escape($_) . '=' . escape($self->{$input}->{$_});
+                }
+                $self->{$input} = join('&', @$args);
             }
-            $self->{args} = join('&', @$args);
-        }
-    } # else, just leave the string along
+        } # else, just leave the string along
+    }
 
     bless $self, $class;
     return $self;
@@ -122,7 +128,9 @@
 =head2 Apache Request Simulation methods
 
 These are the methods this object emulates. Notes follow if the emulation significantly
-differs from the real implementation. Does not check autoflush $|.
+differs from the real implementation.
+
+=over 4 
 
 =item * I<print>($text): Adds the given text to the output array.
 
@@ -159,6 +167,37 @@
 
 =pod
 
+=item * I<args>(): Currently functions only in scalar context, returning the string for
+    the args.
+
+=cut
+
+sub args {
+    my $self = shift;
+    return $self->{querystring};
+}
+
+
+=pod
+
+=item * I<content>(): Returns the string for the args the first time it is called,
+    or undef otherwise.
+
+=cut 
+
+sub content {
+    my $self = shift;
+    if ($self->{postcontent}) {
+        my $tmp = $self->{postcontent};
+        $self->{postcontent} = undef;
+        return $tmp;
+    }
+
+    return undef;
+}
+
+=pod
+
 =back
 
 =cut
@@ -192,6 +231,25 @@
 
 =pod
 
+=item * I<doHandler>(handlerPackageName): Executes the handler indicated by the
+    handlerPackageName. Does a "use $handlerPackageName" and calls the handler
+    on this instance of the ApacheRequest object.
+
+=cut
+
+sub doHandler {
+    my $self = shift;
+    (my $handlerPackageName) = @_;
+
+    eval "use $handlerPackageName;";
+
+    $self->{return_value} = eval "${handlerPackageName}::handler(" . '$self);';
+
+    return;
+}
+
+=pod
+
 =item * I<getOutputString>(): Returns a string representing all of the displayed output 
     of the handler. May take a bit to run. Does not return rflushes or headers.
 
@@ -212,12 +270,29 @@
 
 =pod
 
-=item * I<getReturnValue>(): Returns the return value of the handler.
+=item * I<getReturnValue>(): Returns the return value of the handler, if has been
+    run, or ApacheRequest::NOT_YET_RUN() otherwise.
+
+=cut
+
+sub getReturnValue {
+    my $self = shift;
+    if (defined($self->{return_value})) {
+        return $self->{return_value};
+    } else {
+        return NOT_YET_RUN;
+    }
+}
+
+=pod
 
 =back
 
 =cut
 
+
+########################
+# Misc other necessary stuff.
 
 # Routines imported from lonnet.pm so as to avoid conflict while developing
 sub escape {
Index: modules/jerf/tests/ApacheRequestTest.pm
diff -u modules/jerf/tests/ApacheRequestTest.pm:1.1 modules/jerf/tests/ApacheRequestTest.pm:1.2
--- modules/jerf/tests/ApacheRequestTest.pm:1.1	Thu May 22 16:47:15 2003
+++ modules/jerf/tests/ApacheRequestTest.pm	Fri May 23 14:10:45 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Navigate Maps Handler
 #
-# $Id: ApacheRequestTest.pm,v 1.1 2003/05/22 20:47:15 bowersj2 Exp $
+# $Id: ApacheRequestTest.pm,v 1.2 2003/05/23 18:10:45 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -27,17 +27,31 @@
 #
 # (Testing Infrastructure: Apache Request Simulator Tester
 
+# The tests in this file were written in the order shown here. By
+# starting with testing from the get-go, we are confident as we
+# build up the ApacheRequest object that it at least works as we
+# expect.
+
 package ApacheRequestTest;
 
+# Since we use some of the overridden Apache packages (at least Constants),
+# we need this.
+use lib '.';
+
 # The base class that provides the integration with the test suite rountines, 
 # and all of the assert* methods.
 use base qw(Test::Unit::TestCase);
 use strict;
-use Data::Dumper;
+use Data::Dumper; # for debugging, not always literally used
+use Apache::Constants qw(:common); # for checking handler return values
 
 # Since we're testing this, we need this in here
 use ApacheRequest;
 
+# Right now, I don't need any set up or tear down operations.
+# Later tests on actual handlers will need things, like making sure
+# certain sequences exist or something.
+
 sub new {
     my $self = shift()->SUPER::new(@_);
     return $self;
@@ -111,7 +125,91 @@
     my $querystring = "a=b&c=d";
     my $params = { querystring => $querystring }; 
     my $r = ApacheRequest->new($params);
+    $self->assert($r->args() eq 'a=b&c=d');
+}
+
+sub test_argument_hash_handling {
+    my $self = shift;
+    my $querystring = { a => 'b', c => 'd' };
+    my $params = { querystring => $querystring };
+    my $r = ApacheRequest->new($params);
+    $self->assert($r->args() eq 'a=b&c=d' || $r->args() eq 'c=d&a=b');
+
+    # Check that arguments are escaped correctly
+    $r = ApacheRequest->new({querystring => { 'a&.' => ":,,"}});
+    # escape apparently also encodes ., which is not strictly required
+    # but which is acceptable.
+    $self->assert(lc($r->args()) eq 'a%26%2e=%3a%2c%2c');
+}
+
+# Same test as for querystring, except we check to make sure the content
+# correctly goes away after the first call
+sub test_post_string_handling {
+    my $self = shift;
+    my $postcontent = "a=b&c=d";
+    my $params = { postcontent => $postcontent }; 
+    my $r = ApacheRequest->new($params);
+    $self->assert($r->content() eq 'a=b&c=d');
+    my $content = $r->content();
+    $self->assert(!defined($content));
+}
+
+sub test_post_hash_handling {
+    my $self = shift;
+    my $postcontent = { a => 'b', c => 'd' };
+    my $params = { postcontent => $postcontent };
+    my $r = ApacheRequest->new($params);
+    my $content = $r->content();
+    $self->assert($content eq 'a=b&c=d' || $content eq 'c=d&a=b');
+    $self->assert(!defined($r->content()));
+
+    # Check that arguments are escaped correctly
+    $r = ApacheRequest->new({postcontent => { 'a&.' => ":,,"}});
+    # escape apparently also encodes ., which is not strictly required
+    # but which is acceptable.
+    $self->assert(lc($r->content()) eq 'a%26%2e=%3a%2c%2c');
+    $self->assert(!defined($r->content()));
+}
+
+# Since we never call these functions by name, might as well
+# make them descriptive
+sub test_return_value_starts_not_yet_run {
+    my $self = shift;
+    my $r = ApacheRequest->new();
+    $self->assert($r->getReturnValue() eq ApacheRequest::NOT_YET_RUN);
+}
+
+# Having laid a foundation, we can test our first simple handler. 
+sub test_simple_handler_execution {
+    my $self = shift;
+    my $r = ApacheRequest->new();
     
+    $r->doHandler('SimpleTestHandler');
+
+    $self->assert($r->getReturnValue == OK);
+    my $printed = $r->getOutput();
+    $self->assert($printed->[0] eq ApacheRequest::HEADER);
+    $self->assert($printed->[1] eq 'This is a simple test print.');
+    $self->assert(scalar(@$printed) == 2);
+}
+1;
+
+package SimpleTestHandler;
+
+use lib '.';
+use Apache::Constants qw(:common);
+
+# This is a fake handler that allows testing for the bare minimum
+# of handler-running functionality.
+
+sub handler {
+    my $r = shift;
+
+    $r->send_http_header();
+
+    $r->print("This is a simple test print.");
+
+    return OK;
 }
 
 1;

--bowersj21053713445--