From: Matt S Trout Date: Thu, 23 Feb 2006 03:55:00 +0000 (+0000) Subject: Initial support for :Args attribute X-Git-Tag: 5.7099_04~699 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=4082e67814e85bc9820e56eb38e5e21511c0a5f8;hp=855ed93153e2b0c0e88d9bb8f23822c914393048 Initial support for :Args attribute --- diff --git a/lib/Catalyst/Action.pm b/lib/Catalyst/Action.pm index 0002ef7..8e5ea75 100644 --- a/lib/Catalyst/Action.pm +++ b/lib/Catalyst/Action.pm @@ -46,6 +46,16 @@ sub execute { # Execute ourselves against a context return $c->execute( $self->class, $self ); } +=head2 match + +=cut + +sub match { + my ( $self, $c ) = @_; + return 1 unless exists $self->attributes->{Args}; + return scalar(@{$c->req->args}) == $self->attributes->{Args}[0]; +} + =head2 namespace =head2 reverse diff --git a/lib/Catalyst/DispatchType/Default.pm b/lib/Catalyst/DispatchType/Default.pm index f1864b2..abe887e 100644 --- a/lib/Catalyst/DispatchType/Default.pm +++ b/lib/Catalyst/DispatchType/Default.pm @@ -25,7 +25,7 @@ sub match { my $result = ( $c->get_actions( 'default', $c->req->path ) )[-1]; # Find default on namespace or super - if ($result) { + if ($result && $result->match($c)) { $c->action($result); $c->namespace( $result->namespace ); $c->req->action('default'); diff --git a/lib/Catalyst/DispatchType/Index.pm b/lib/Catalyst/DispatchType/Index.pm index dd8b010..f876850 100644 --- a/lib/Catalyst/DispatchType/Index.pm +++ b/lib/Catalyst/DispatchType/Index.pm @@ -24,7 +24,7 @@ sub match { return if @{ $c->req->args }; my $result = $c->get_action( 'index', $path ); - if ($result) { + if ($result && $result->match($c)) { $c->action($result); $c->namespace( $result->namespace ); $c->req->action('index'); diff --git a/lib/Catalyst/DispatchType/Path.pm b/lib/Catalyst/DispatchType/Path.pm index 64bc477..ceea17b 100644 --- a/lib/Catalyst/DispatchType/Path.pm +++ b/lib/Catalyst/DispatchType/Path.pm @@ -42,6 +42,7 @@ sub match { $path ||= '/'; if ( my $action = $self->{paths}->{$path} ) { + return 0 unless $action->match($c); $c->req->action($path); $c->req->match($path); $c->action($action); diff --git a/lib/Catalyst/DispatchType/Regex.pm b/lib/Catalyst/DispatchType/Regex.pm index b8c0d15..49f7636 100644 --- a/lib/Catalyst/DispatchType/Regex.pm +++ b/lib/Catalyst/DispatchType/Regex.pm @@ -44,6 +44,7 @@ sub match { foreach my $compiled ( @{ $self->{compiled} || [] } ) { if ( my @snippets = ( $path =~ $compiled->{re} ) ) { + next unless $compiled->{action}->match($c); $c->req->action( $compiled->{path} ); $c->req->match($path); $c->req->snippets( \@snippets ); diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 05101b9..858c1ec 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -194,9 +194,10 @@ sub prepare_action { } # If not, move the last part path to args - unshift @args, pop @path; + my $arg = pop(@path); + $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; + unshift @args, $arg; } - s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg for @args; $c->log->debug( 'Path is "' . $c->req->match . '"' ) if ( $c->debug && $c->req->match ); diff --git a/t/lib/TestApp/Controller/Action.pm b/t/lib/TestApp/Controller/Action.pm index 02bd50a..543d6e1 100644 --- a/t/lib/TestApp/Controller/Action.pm +++ b/t/lib/TestApp/Controller/Action.pm @@ -12,6 +12,7 @@ sub begin : Private { sub default : Private { my ( $self, $c ) = @_; $c->res->output("Error - TestApp::Controller::Action\n"); + $c->res->status(404); } 1; diff --git a/t/lib/TestApp/Controller/Action/Local.pm b/t/lib/TestApp/Controller/Action/Local.pm index 65f3293..9d30cf2 100644 --- a/t/lib/TestApp/Controller/Action/Local.pm +++ b/t/lib/TestApp/Controller/Action/Local.pm @@ -8,7 +8,7 @@ sub one : Action Relative { $c->forward('TestApp::View::Dump::Request'); } -sub two : Action Local { +sub two : Action Local Args(2) { my ( $self, $c ) = @_; $c->forward('TestApp::View::Dump::Request'); } diff --git a/t/live_component_controller_action_local.t b/t/live_component_controller_action_local.t index 62c2674..5177163 100644 --- a/t/live_component_controller_action_local.t +++ b/t/live_component_controller_action_local.t @@ -10,7 +10,7 @@ our $iters; BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 2; } -use Test::More tests => 30*$iters; +use Test::More tests => 32*$iters; use Catalyst::Test 'TestApp'; if ( $ENV{CAT_BENCHMARK} ) { @@ -44,7 +44,7 @@ sub run_tests { } { - ok( my $response = request('http://localhost/action/local/two'), + ok( my $response = request('http://localhost/action/local/two/1/2'), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); is( $response->content_type, 'text/plain', 'Response Content-Type' ); @@ -63,6 +63,12 @@ sub run_tests { } { + ok( my $response = request('http://localhost/action/local/two'), + 'Request' ); + ok( !$response->is_success, 'Request with wrong number of args failed' ); + } + + { ok( my $response = request('http://localhost/action/local/three'), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); @@ -106,13 +112,13 @@ sub run_tests { { ok( my $response = - request('http://localhost/action/local/two/foo%2Fbar'), + request('http://localhost/action/local/one/foo%2Fbar'), 'Request' ); ok( $response->is_success, 'Response Successful 2xx' ); is( $response->content_type, 'text/plain', 'Response Content-Type' ); is( $response->header('X-Catalyst-Action'), - 'action/local/two', 'Test Action' ); + 'action/local/one', 'Test Action' ); is( $response->header('X-Test-Class'), 'TestApp::Controller::Action::Local',