r12983@zaphod: kd | 2008-04-28 18:10:27 +1000
[catagits/Catalyst-Runtime.git] / t / live_component_controller_action_chained.t
index b9367a2..a8b7e09 100644 (file)
@@ -8,9 +8,9 @@ use lib "$FindBin::Bin/lib";
 
 our $iters;
 
-BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 2; }
+BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 1; }
 
-use Test::More tests => 99*$iters;
+use Test::More tests => 141*$iters;
 use Catalyst::Test 'TestApp';
 
 if ( $ENV{CAT_BENCHMARK} ) {
@@ -19,11 +19,12 @@ if ( $ENV{CAT_BENCHMARK} ) {
 }
 else {
     for ( 1 .. $iters ) {
-        run_tests();
+        run_tests($_);
     }
 }
 
 sub run_tests {
+    my ($run_number) = @_;
 
     #
     #   This is a simple test where the parent and child actions are
@@ -56,7 +57,7 @@ sub run_tests {
             'chained + local endpoint; missing last argument' );
         is( $response->header('X-Catalyst-Executed'),
             $expected, 'Executed actions' );
-        is( $response->header('Status'), 500, 'Status OK' );
+        is( $response->code, 500, 'Status OK' );
     }
 
     #
@@ -234,7 +235,7 @@ sub run_tests {
             'multi-action (three args, should lead to error)' );
         is( $response->header('X-Catalyst-Executed'),
             $expected, 'Executed actions' );
-        is( $response->header('Status'), 500, 'Status OK' );
+        is( $response->code, 500, 'Status OK' );
     }
 
     #
@@ -527,6 +528,86 @@ sub run_tests {
     }
 
     #
+    #   Test if :Chained('../act') is working
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained->one
+          TestApp::Controller::Action::Chained::ParentChain->chained_rel
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/one/1/chained_rel/3/2'),
+            ":Chained('../action') chains to correct action" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 3, 2', 'Content OK' );
+    }
+
+    #
+    #   Test if ../ works to go up more than one level
+    #
+    {
+        my @expected = qw[
+            TestApp::Controller::Action::Chained->begin
+            TestApp::Controller::Action::Chained->one
+            TestApp::Controller::Action::Chained::ParentChain::Relative->chained_rel_two
+            TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/one/1/chained_rel_two/42/23'),
+            "../ works to go up more than one level" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '1; 42, 23', 'Content OK' );
+    }
+
+    #
+    #   Test if :ChainedParent is working
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained->loose
+          TestApp::Controller::Action::Chained::ParentChain->loose
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/loose/4/loose/a/b'),
+            ":Chained('../action') chains to correct action" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '4; a, b', 'Content OK' );
+    }
+
+    #
+    #   Test if :Chained('../name/act') is working
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::Bar->cross1
+          TestApp::Controller::Action::Chained::ParentChain->up_down
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/cross/4/up_down/5'),
+            ":Chained('../action') chains to correct action" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '4; 5', 'Content OK' );
+    }
+
+    #
     #   Test behaviour of auto actions returning '1' for the chain.
     #
     {
@@ -645,7 +726,7 @@ sub run_tests {
             "Loose end is not callable" );
         is( $response->header('X-Catalyst-Executed'),
             $expected, 'Executed actions' );
-        is( $response->header('Status'), 500, 'Status OK' );
+        is( $response->code, 500, 'Status OK' );
     }
 
     #
@@ -688,4 +769,216 @@ sub run_tests {
             $expected, 'Executed actions' );
         is( $response->content, '1; 2', 'Content OK' );
     }
+
+    #
+    #   Tests that an uri_for to a chained root index action
+    #   returns the right value.
+    #
+    {
+        ok( my $response = request(
+            'http://localhost/action/chained/to_root' ),
+            'uri_for with chained root action as arg' );
+        like( $response->content,
+            qr(URI:https?://[^/]+/),
+            'Correct URI generated' );
+    }
+
+    #
+    #   Test interception of recursive chains. This test was added because at
+    #   one point during the :Chained development, Catalyst used to hang on
+    #   recursive chains.
+    #
+    {
+        eval { require 'TestAppChainedRecursive.pm' };
+        if ($run_number == 1) {
+            ok( ! $@, "Interception of recursive chains" );
+        }
+        else { pass( "Interception of recursive chains already tested" ) }
+    }
+
+    #
+    #   Test failure of absolute path part arguments.
+    #
+    {
+        eval { require 'TestAppChainedAbsolutePathPart.pm' };
+        if ($run_number == 1) {
+            like( $@, qr(foo/foo),
+                "Usage of absolute path part argument emits error" );
+        }
+        else { pass( "Error on absolute path part arguments already tested" ) }
+    }
+
+    #
+    #   Test chained actions in the root controller
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained::Root->rootsub
+          TestApp::Controller::Action::Chained::Root->endpointsub
+          TestApp->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/rootsub/1/endpointsub/2'), 'chained in root namespace' );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '', 'Content OK' );
+    }
+
+    #
+    #   Complex path with multiple empty pathparts
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained->mult_nopp_base
+          TestApp::Controller::Action::Chained->mult_nopp_all
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/chained/mult_nopp'),
+            "Complex path with multiple empty pathparts" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '; ', 'Content OK' );
+    }
+
+    #
+    #   Higher Args() hiding more specific CaptureArgs chains sections
+    #
+    {
+        my @expected = qw[
+            TestApp::Controller::Action::Chained->begin
+            TestApp::Controller::Action::Chained->cc_base
+            TestApp::Controller::Action::Chained->cc_link
+            TestApp::Controller::Action::Chained->cc_anchor
+            TestApp::Controller::Action::Chained->end
+            ];
+
+        my $expected = join ', ', @expected;
+
+        ok( my $response = request('http://localhost/chained/choose_capture/anchor.html'),
+            'Choose between an early Args() and a later more ideal chain' );
+        is( $response->header('X-Catalyst-Executed') => $expected, 'Executed actions');
+        is( $response->content => '; ', 'Content OK' );
+    }
+
+    #
+    #   Less specific chain not being seen correctly due to earlier looser capture
+    #
+    {
+        my @expected = qw[
+            TestApp::Controller::Action::Chained->begin
+            TestApp::Controller::Action::Chained->cc_base
+            TestApp::Controller::Action::Chained->cc_b
+            TestApp::Controller::Action::Chained->cc_b_link
+            TestApp::Controller::Action::Chained->cc_b_anchor
+            TestApp::Controller::Action::Chained->end
+            ];
+
+        my $expected = join ', ', @expected;
+
+        ok( my $response = request('http://localhost/chained/choose_capture/b/a/anchor.html'),
+            'Choose between a more specific chain and an earlier looser one' );
+        is( $response->header('X-Catalyst-Executed') => $expected, 'Executed actions');
+        is( $response->content => 'a; ', 'Content OK' );
+    }
+
+    #
+    #   Check we get the looser one when it's the correct match
+    #
+    {
+        my @expected = qw[
+            TestApp::Controller::Action::Chained->begin
+            TestApp::Controller::Action::Chained->cc_base
+            TestApp::Controller::Action::Chained->cc_a
+            TestApp::Controller::Action::Chained->cc_a_link
+            TestApp::Controller::Action::Chained->cc_a_anchor
+            TestApp::Controller::Action::Chained->end
+            ];
+
+        my $expected = join ', ', @expected;
+
+        ok( my $response = request('http://localhost/chained/choose_capture/a/a/anchor.html'),
+            'Choose between a more specific chain and an earlier looser one' );
+        is( $response->header('X-Catalyst-Executed') => $expected, 'Executed actions');
+        is( $response->content => 'a; anchor.html', 'Content OK' );
+    }
+
+    #
+    #   Args(0) should win over Args() if we actually have no arguments.
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::ArgsOrder->base
+          TestApp::Controller::Action::Chained::ArgsOrder->index
+          TestApp::Controller::Action::Chained::ArgsOrder->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+    # With no args, we should run "index"
+        ok( my $response = request('http://localhost/argsorder/'),
+            'Correct arg order ran' );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, 'base; ; index; ', 'Content OK' );
+
+    # With args given, run "all"
+        ok( $response = request('http://localhost/argsorder/X'),
+            'Correct arg order ran' );
+        is( $response->header('X-Catalyst-Executed'), 
+        join(", ", 
+         qw[
+             TestApp::Controller::Action::Chained->begin
+             TestApp::Controller::Action::Chained::ArgsOrder->base
+             TestApp::Controller::Action::Chained::ArgsOrder->all
+             TestApp::Controller::Action::Chained::ArgsOrder->end
+          ])
+      );
+        is( $response->content, 'base; ; all; X', 'Content OK' );
+    
+    }
+
+    #
+    #   PathPrefix
+    #
+    {
+        my @expected = qw[
+          TestApp::Controller::Action::Chained->begin
+          TestApp::Controller::Action::Chained::PathPrefix->instance
+          TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/action/chained/pathprefix/1'),
+            "PathPrefix (as an endpoint)" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, '; 1', 'Content OK' );
+    }
+
+    #
+    #   static paths vs. captures
+    #
+    {
+        my @expected = qw[
+            TestApp::Controller::Action::Chained->begin
+            TestApp::Controller::Action::Chained->apan
+            TestApp::Controller::Action::Chained->korv
+            TestApp::Controller::Action::Chained->static_end
+            TestApp::Controller::Action::Chained->end
+        ];
+
+        my $expected = join( ", ", @expected );
+
+        ok( my $response = request('http://localhost/action/chained/static_end'),
+            "static paths are prefered over captures" );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+    }
 }