[Catalyst-commits] r9531 - in Catalyst-Runtime/5.80/trunk: . lib/Catalyst t

t0m at dev.catalyst.perl.org t0m at dev.catalyst.perl.org
Fri Mar 20 14:17:09 GMT 2009


Author: t0m
Date: 2009-03-20 14:17:09 +0000 (Fri, 20 Mar 2009)
New Revision: 9531

Modified:
   Catalyst-Runtime/5.80/trunk/Changes
   Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm
   Catalyst-Runtime/5.80/trunk/t/unit_load_catalyst_test.t
Log:
Port kanes r9520 up to 5.80 trunk


Modified: Catalyst-Runtime/5.80/trunk/Changes
===================================================================
--- Catalyst-Runtime/5.80/trunk/Changes	2009-03-18 21:39:14 UTC (rev 9530)
+++ Catalyst-Runtime/5.80/trunk/Changes	2009-03-20 14:17:09 UTC (rev 9531)
@@ -1,5 +1,7 @@
 # This file documents the revision history for Perl extension Catalyst.
 
+        - Add Catalyst::Test::crequest to return both HTTP::Response object
+          & $c for local requests (kane)
         - debug() POD rewrite (jhannah)
         - Change the warning when you have conflicting components to
           present a list (t0m)

Modified: Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm
===================================================================
--- Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm	2009-03-18 21:39:14 UTC (rev 9530)
+++ Catalyst-Runtime/5.80/trunk/lib/Catalyst/Test.pm	2009-03-20 14:17:09 UTC (rev 9531)
@@ -30,9 +30,39 @@
 
     my $get = sub { $request->(@_)->content };
 
+    my $crequest = sub {
+        my $me      = ref $self || $self;
+
+        ### throw an exception if crequest is being used against a remote
+        ### server
+        Catalyst::Exception->throw("$me only works with local requests, not remote")
+            if $ENV{CATALYST_SERVER};
+
+        ### place holder for $c after the request finishes; reset every time
+        ### requests are done.
+        my $c;
+
+        ### hook into 'dispatch' -- the function gets called after all plugins
+        ### have done their work, and it's an easy place to capture $c.
+        no warnings 'redefine';
+        my $dispatch = Catalyst->can('dispatch');
+        local *Catalyst::dispatch = sub {
+            $c = shift;
+            $dispatch->( $c, @_ );
+        };
+
+        ### do the request; C::T::request will know about the class name, and
+        ### we've already stopped it from doing remote requests above.
+        my $res = $request->( @_ );
+
+        ### return both values
+        return ( $res, $c );
+    };
+
     return {
-        request => $request,
-        get     => $get,
+        request  => $request,
+        get      => $get,
+        crequest => $crequest,
         content_like => sub {
             my $action = shift;
             return Test::More->builder->like($get->($action), at _);
@@ -71,6 +101,7 @@
         $import->($self, '-all' => { class => $class });
         $opts = {} unless ref $opts eq 'HASH';
         $default_host = $opts->{default_host} if exists $opts->{default_host};
+        return 1;
     }
 }
 
@@ -85,8 +116,9 @@
 
     # Tests
     use Catalyst::Test 'TestApp';
-    request('index.html');
-    get('index.html');
+    my $content  = get('index.html');           # Content as string
+    my $response = request('index.html');       # HTTP::Response object
+    my($res, $c) = crequest('index.html');      # HTTP::Response & context object
 
     use HTTP::Request::Common;
     my $response = request POST '/foo', [
@@ -138,7 +170,7 @@
 
 =head2 METHODS
 
-=head2 get
+=head2 $content = get( ... )
 
 Returns the content.
 
@@ -155,7 +187,7 @@
     is ( $uri->path , '/y');
     my $content = get($uri->path);
 
-=head2 request
+=head2 $res = request( ... );
 
 Returns a C<HTTP::Response> object. Accepts an optional hashref for request
 header configuration; currently only supports setting 'host' value.
@@ -163,8 +195,15 @@
     my $res = request('foo/bar?test=1');
     my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'});
 
-=head2 local_request
+=head1 FUNCTIONS
 
+=head2 ($res, $c) = crequest( ... );
+
+Works exactly like C<Catalyst::Test::request>, except it also returns the
+catalyst context object, C<$c>. Note that this only works for local requests.
+
+=head2 $res = Catalyst::Test::local_request( $AppClass, $url );
+
 Simulate a request using L<HTTP::Request::AsCGI>.
 
 =cut
@@ -185,7 +224,7 @@
 
 my $agent;
 
-=head2 remote_request
+=head2 $res = Catalyst::Test::remote_request( $url );
 
 Do an actual remote request using LWP.
 

Modified: Catalyst-Runtime/5.80/trunk/t/unit_load_catalyst_test.t
===================================================================
--- Catalyst-Runtime/5.80/trunk/t/unit_load_catalyst_test.t	2009-03-18 21:39:14 UTC (rev 9530)
+++ Catalyst-Runtime/5.80/trunk/t/unit_load_catalyst_test.t	2009-03-20 14:17:09 UTC (rev 9531)
@@ -3,23 +3,95 @@
 use strict;
 use warnings;
 
-use Test::More;
+use FindBin;
+use lib         "$FindBin::Bin/lib";
+use Test::More  tests => 56;
 use FindBin qw/$Bin/;
 use lib "$Bin/lib";
 use Catalyst::Utils;
 use HTTP::Request::Common;
 use Test::Exception;
 
-plan tests => 11;
+my $Class   = 'Catalyst::Test';
+my $App     = 'TestApp';
+my $Pkg     = __PACKAGE__;
+my $Url     = 'http://localhost/';
+my $Content = "root index";
 
-use_ok('Catalyst::Test');
+my %Meth    = (
+    $Pkg    => [qw|get request crequest|],          # exported
+    $Class  => [qw|local_request remote_request|],  # not exported
+);
 
-eval "get('http://localhost')";
-isnt( $@, "", "get returns an error message with no app specified");
+### make sure we're not trying to connect to a remote host -- these are local tests
+local $ENV{CATALYST_SERVER};                
 
-eval "request('http://localhost')";
-isnt( $@, "", "request returns an error message with no app specified");
+use_ok( $Class );
 
+### check available methods
+{   ### turn of redefine warnings, we'll get new subs exported
+    ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in
+    ### test.pm, so trap them for now --kane
+    {   local $SIG{__WARN__} = sub {};
+        ok( $Class->import,     "Argumentless import for methods only" );
+    }
+
+    while( my($class, $meths) = each %Meth ) {
+        for my $meth ( @$meths ) { SKIP: {
+            
+            ### method available?
+            can_ok( $class,     $meth );
+
+            ### only for exported methods
+            skip "Error tests only for exported methods", 2 unless $class eq $Pkg;
+
+            ### check error conditions
+            eval { $class->can($meth)->( $Url ) };
+            ok( $@,             "   $meth without app gives error" );
+            like( $@, qr/$Class/, 
+                                "       Error filled with expected content for '$meth'" );
+        } }       
+    }
+}    
+ 
+### simple tests for exported methods 
+{   ### turn of redefine warnings, we'll get new subs exported
+    ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in
+    ### test.pm, so trap them for now --kane
+    {   local $SIG{__WARN__} = sub {};
+        ok( $Class->import( $App ), 
+                                "Loading $Class for App $App" );
+    }
+    
+    ### test exported methods again
+    for my $meth ( @{ $Meth{$Pkg} } ) { SKIP: {
+
+        ### do a call, we should get a result and perhaps a $c if it's 'crequest';
+        my ($res, $c) = eval { $Pkg->can($meth)->( $Url ) };
+        
+        ok( 1,                  "   Called $Pkg->$meth( $Url )" );
+        ok( !$@,                "       No critical error $@" );
+        ok( $res,               "       Result obtained" );
+        
+        ### get the content as a string, to make sure we got what we expected
+        my $res_as_string = $meth eq 'get' ? $res : $res->content;
+        is( $res_as_string, $Content,
+                                "           Content as expected: $res_as_string" );    
+        
+        ### some tests for 'crequest'
+        skip "Context tests skipped for '$meth'", 6 unless $meth eq 'crequest';
+        
+        ok( $c,                 "           Context object returned" );
+        isa_ok( $c, $App,       "               Object" );
+        is( $c->request->uri, $Url,
+                                "               Url recorded in request" );
+        is( $c->response->body, $Content,
+                                "               Content recorded in response" );
+        ok( $c->stash,          "               Stash accessible" );
+        ok( $c->action,         "               Action object accessible" );
+    } }
+}
+
 # FIXME - These vhosts in tests tests should be somewhere else...
 
 sub customize { Catalyst::Test::_customize_request(@_) }
@@ -67,3 +139,4 @@
 lives_ok {
     request(GET('/dummy'), []);
 } 'array additional param to request method ignored';
+




More information about the Catalyst-commits mailing list