X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=t%2Flive_component_controller_action_chained.t;h=411542ec58bc80fb0f521db1fe42bd020e68f446;hp=46d95a98d6e7522f115367a7f31ede13875d1e3a;hb=6365b5279cf3bbecd61735b3b744809983a7bfdc;hpb=dbc702f6ef12c059c8645776e0762e4495c95715 diff --git a/t/live_component_controller_action_chained.t b/t/live_component_controller_action_chained.t index 46d95a9..411542e 100644 --- a/t/live_component_controller_action_chained.t +++ b/t/live_component_controller_action_chained.t @@ -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 => 66*$iters; +use Test::More tests => 118*$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' ); } # @@ -120,7 +121,7 @@ sub run_tests { # # The first three-chain test tries to call the action with :Args(1) - # specification. There's also a one action with a :Captures(1) + # specification. There's also a one action with a :CaptureArgs(1) # attribute, that should not be dispatched to. # { @@ -142,7 +143,7 @@ sub run_tests { # # This is the second three-chain test, it goes for the action that # handles "/one/$cap/two/$arg1/$arg2" paths. Should be the two action - # having :Args(2), not the one having :Captures(2). + # having :Args(2), not the one having :CaptureArgs(2). # { my @expected = qw[ @@ -162,9 +163,9 @@ sub run_tests { } # - # Last of the three-chain tests. Has no concurrent action with :Captures + # Last of the three-chain tests. Has no concurrent action with :CaptureArgs # and is more thought to simply test the chain as a whole and the 'two' - # action specifying :Captures. + # action specifying :CaptureArgs. # { my @expected = qw[ @@ -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' ); } # @@ -364,6 +365,27 @@ sub run_tests { } # + # This belongs to the former test but tests if two chained actions have + # priority over an action with one child action not having the Args() attr set. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained->priority_c1 + TestApp::Controller::Action::Chained->priority_c2_xyz + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/priority_c/1/xyz/'), + 'priority - no Args() order mismatch' ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; ', 'Content OK' ); + } + + # # Test dispatching between two controllers that are on the same level and # therefor have no parent/child relationship. # @@ -465,4 +487,345 @@ sub run_tests { $expected, 'Executed actions' ); is( $response->content, '1; 2, 3', 'Content OK' ); } + + # + # Test if :Chained is the same as :Chained('/') + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained->rootdef + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/rootdef/23'), + ":Chained is the same as :Chained('/')" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '; 23', 'Content OK' ); + } + + # + # Test if :Chained('.') is working + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained->parentchain + TestApp::Controller::Action::Chained::ParentChain->child + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/parentchain/1/child/2'), + ":Chained('.') chains to parent controller action" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Test behaviour of auto actions returning '1' for the chain. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained::Auto->auto + TestApp::Controller::Action::Chained::Auto::Foo->auto + TestApp::Controller::Action::Chained::Auto->foo + TestApp::Controller::Action::Chained::Auto::Foo->fooend + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/autochain1/1/fooend/2'), + "Behaviour when auto returns 1 correct" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Test behaviour of auto actions returning '0' for the chain. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained::Auto->auto + TestApp::Controller::Action::Chained::Auto::Bar->auto + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/autochain2/1/barend/2'), + "Behaviour when auto returns 0 correct" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Test what auto actions are run when namespaces are changed + # horizontally. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained::Auto->auto + TestApp::Controller::Action::Chained::Auto::Foo->auto + TestApp::Controller::Action::Chained::Auto::Bar->crossloose + TestApp::Controller::Action::Chained::Auto::Foo->crossend + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/auto_cross/1/crossend/2'), + "Correct auto actions are run on cross controller dispatch" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Test forwarding from auto action in chain dispatch. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained::Auto->auto + TestApp::Controller::Action::Chained::Auto::Forward->auto + TestApp::Controller::Action::Chained::Auto->fw3 + TestApp::Controller::Action::Chained::Auto->fw1 + TestApp::Controller::Action::Chained::Auto::Forward->forwardend + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/auto_forward/1/forwardend/2'), + "Forwarding out of auto in chain" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Detaching out of the auto action of a chain. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained::Auto->auto + TestApp::Controller::Action::Chained::Auto::Detach->auto + TestApp::Controller::Action::Chained::Auto->fw3 + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/auto_detach/1/detachend/2'), + "Detaching out of auto in chain" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Test forwarding from auto action in chain dispatch. + # + { + my $expected = undef; + + ok( my $response = request('http://localhost/chained/loose/23'), + "Loose end is not callable" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->code, 500, 'Status OK' ); + } + + # + # Test forwarding out of a chain. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained->chain_fw_a + TestApp::Controller::Action::Chained->fw_dt_target + TestApp::Controller::Action::Chained->chain_fw_b + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/chain_fw/1/end/2'), + "Forwarding out a chain" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, '1; 2', 'Content OK' ); + } + + # + # Test detaching out of a chain. + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained->chain_dt_a + TestApp::Controller::Action::Chained->fw_dt_target + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/chain_dt/1/end/2'), + "Forwarding out a chain" ); + is( $response->header('X-Catalyst-Executed'), + $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:http://[^/]+/), + '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' ); + } + }