From: Matt S Trout Date: Tue, 15 Aug 2006 14:42:06 +0000 (+0000) Subject: uri_for fixup (with thanks to phaylon) X-Git-Tag: 5.7099_04~371 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=81e75875ca7cea9004783a0c142040081da630d8 uri_for fixup (with thanks to phaylon) --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index b619635..cb3e86d 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -889,6 +889,7 @@ sub uri_for { : [] ); $path = $c->dispatcher->uri_for_action($path, $captures); return undef unless defined($path); + $path = '/' if $path eq ''; } # massage namespace, empty if absolute path diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 7e03356..ff25f96 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -372,7 +372,8 @@ sub uri_for_action { $captures ||= []; foreach my $dispatch_type ( @{ $self->dispatch_types } ) { my $uri = $dispatch_type->uri_for_action( $action, $captures ); - return $uri if defined($uri); + return( $uri eq '' ? '/' : $uri ) + if defined($uri); } return undef; } diff --git a/t/lib/TestApp/Controller/Action/Chained.pm b/t/lib/TestApp/Controller/Action/Chained.pm index 9e6a93f..dba0d5d 100644 --- a/t/lib/TestApp/Controller/Action/Chained.pm +++ b/t/lib/TestApp/Controller/Action/Chained.pm @@ -153,6 +153,7 @@ sub empty_chain_f : Chained('empty_chain_e') PathPart('') Args(1) sub end :Private { my ($self, $c) = @_; + return if $c->stash->{no_end}; my $out = join('; ', map { join(', ', @$_) } ($c->req->captures, $c->req->args)); $c->res->body($out); diff --git a/t/lib/TestApp/Controller/Action/Chained/Foo.pm b/t/lib/TestApp/Controller/Action/Chained/Foo.pm index 7e3ff4b..840a619 100644 --- a/t/lib/TestApp/Controller/Action/Chained/Foo.pm +++ b/t/lib/TestApp/Controller/Action/Chained/Foo.pm @@ -25,4 +25,15 @@ sub pcp2 :Chained('/action/chained/pcp1') :CaptureArgs(1) { } # sub cross2 :PathPart('end') :Chained('/action/chained/bar/cross1') :Args(1) { } +# +# Create a uri to the root index +# +sub to_root : Chained('/') PathPart('action/chained/to_root') { + my ( $self, $c ) = @_; + my $uri = $c->uri_for( + $c->controller('Root')->action_for('chain_root_index') ); + $c->res->body( "URI:$uri" ); + $c->stash->{no_end}++; +} + 1; diff --git a/t/lib/TestApp/Controller/Root.pm b/t/lib/TestApp/Controller/Root.pm index 281782a..a9cbbda 100644 --- a/t/lib/TestApp/Controller/Root.pm +++ b/t/lib/TestApp/Controller/Root.pm @@ -4,4 +4,6 @@ use base 'Catalyst::Controller'; __PACKAGE__->config->{namespace} = ''; +sub chain_root_index : Chained('/') PathPart('') Args(0) { } + 1; diff --git a/t/live_component_controller_action_chained.t b/t/live_component_controller_action_chained.t index a92f32c..0ed19c7 100644 --- a/t/live_component_controller_action_chained.t +++ b/t/live_component_controller_action_chained.t @@ -10,7 +10,7 @@ our $iters; BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 2; } -use Test::More tests => 101*$iters; +use Test::More tests => 103*$iters; use Catalyst::Test 'TestApp'; if ( $ENV{CAT_BENCHMARK} ) { @@ -691,6 +691,19 @@ sub run_tests { } # + # 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' ); + is( $response->content, + 'URI:http://localhost/', + '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.