Initial support for :Args attribute
Matt S Trout [Thu, 23 Feb 2006 03:55:00 +0000 (03:55 +0000)]
lib/Catalyst/Action.pm
lib/Catalyst/DispatchType/Default.pm
lib/Catalyst/DispatchType/Index.pm
lib/Catalyst/DispatchType/Path.pm
lib/Catalyst/DispatchType/Regex.pm
lib/Catalyst/Dispatcher.pm
t/lib/TestApp/Controller/Action.pm
t/lib/TestApp/Controller/Action/Local.pm
t/live_component_controller_action_local.t

index 0002ef7..8e5ea75 100644 (file)
@@ -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
index f1864b2..abe887e 100644 (file)
@@ -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');
index dd8b010..f876850 100644 (file)
@@ -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');
index 64bc477..ceea17b 100644 (file)
@@ -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);
index b8c0d15..49f7636 100644 (file)
@@ -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 );
index 05101b9..858c1ec 100644 (file)
@@ -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 );
index 02bd50a..543d6e1 100644 (file)
@@ -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;
index 65f3293..9d30cf2 100644 (file)
@@ -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');
 }
index 62c2674..5177163 100644 (file)
@@ -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',