Fix Bill Moseley's double-auto bug, and add tests
Yuval Kogman [Tue, 2 May 2006 15:06:06 +0000 (15:06 +0000)]
lib/Catalyst/Dispatcher.pm
t/lib/TestApp/Controller/Action/Auto/Default.pm [new file with mode: 0644]
t/lib/TestAppDoubleAutoBug.pm [new file with mode: 0644]
t/live_component_controller_action_auto_doublebug.t [new file with mode: 0644]

index d038d9e..c4e4dd0 100644 (file)
@@ -332,9 +332,11 @@ sub get_containers {
 
     my @containers;
 
-    do {
-        push @containers, $self->container_hash->{$namespace};
-    } while ( $namespace =~ s#/[^/]+$## );
+    if ( length $namespace ) {
+        do {
+            push @containers, $self->container_hash->{$namespace};
+        } while ( $namespace =~ s#/[^/]+$## );
+    }
 
     return reverse grep { defined } @containers, $self->container_hash->{''};
 
diff --git a/t/lib/TestApp/Controller/Action/Auto/Default.pm b/t/lib/TestApp/Controller/Action/Auto/Default.pm
new file mode 100644 (file)
index 0000000..7fa7eb4
--- /dev/null
@@ -0,0 +1,22 @@
+package TestApp::Controller::Action::Auto::Default;
+
+use strict;
+use base 'TestApp::Controller::Action';
+
+sub begin : Private { }
+
+sub auto : Private {
+    my ( $self, $c ) = @_;
+    $c->stash->{auto_ran}++;
+    return 1;
+}
+
+sub default : Private {
+    my ( $self, $c ) = @_;
+    $c->res->body( sprintf 'default (auto: %d)', $c->stash->{auto_ran} );
+}
+
+sub end : Private { }
+
+1;
+
diff --git a/t/lib/TestAppDoubleAutoBug.pm b/t/lib/TestAppDoubleAutoBug.pm
new file mode 100644 (file)
index 0000000..5c72461
--- /dev/null
@@ -0,0 +1,59 @@
+use strict;
+use warnings;
+
+package TestAppDoubleAutoBug;
+
+use Catalyst qw/
+    Test::Errors
+    Test::Headers
+    Test::Plugin
+/;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->config( name => 'TestAppDoubleAutoBug', root => '/some/dir' );
+
+__PACKAGE__->setup;
+
+sub execute {
+    my $c      = shift;
+    my $class  = ref( $c->component( $_[0] ) ) || $_[0];
+    my $action = "$_[1]";
+
+    my $method;
+
+    if ( $action =~ /->(\w+)$/ ) {
+        $method = $1;
+    }
+    elsif ( $action =~ /\/(\w+)$/ ) {
+        $method = $1;
+    }
+    elsif ( $action =~ /^(\w+)$/ ) {
+        $method = $action;
+    }
+
+    if ( $class && $method && $method !~ /^_/ ) {
+        my $executed = sprintf( "%s->%s", $class, $method );
+        my @executed = $c->response->headers->header('X-Catalyst-Executed');
+        push @executed, $executed;
+        $c->response->headers->header(
+            'X-Catalyst-Executed' => join ', ',
+            @executed
+        );
+    }
+
+    return $c->SUPER::execute(@_);
+}
+
+
+
+sub auto : Private {
+    my ( $self, $c ) = @_;
+    ++$c->stash->{auto_count};
+    return 1;
+}
+
+sub default : Private {
+    my ( $self, $c ) = @_;
+    $c->res->body( sprintf 'default, auto=%d', $c->stash->{auto_count} );
+}
diff --git a/t/live_component_controller_action_auto_doublebug.t b/t/live_component_controller_action_auto_doublebug.t
new file mode 100644 (file)
index 0000000..6d8eb99
--- /dev/null
@@ -0,0 +1,41 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+our $iters;
+
+BEGIN { $iters = $ENV{CAT_BENCH_ITERS} || 2; }
+
+use Test::More tests => 3*$iters;
+use Catalyst::Test 'TestAppDoubleAutoBug';
+
+if ( $ENV{CAT_BENCHMARK} ) {
+    require Benchmark;
+    Benchmark::timethis( $iters, \&run_tests );
+}
+else {
+    for ( 1 .. $iters ) {
+        run_tests();
+    }
+}
+    
+sub run_tests {
+    {
+        my @expected = qw[
+            TestAppDoubleAutoBug->auto
+            TestAppDoubleAutoBug->default
+            TestAppDoubleAutoBug->end
+        ];
+    
+        my $expected = join( ", ", @expected );
+    
+        ok( my $response = request('http://localhost/action/auto/one'), 'auto + local' );
+        is( $response->header('X-Catalyst-Executed'),
+            $expected, 'Executed actions' );
+        is( $response->content, 'default, auto=1', 'Content OK' );
+    }
+}