Fix display of foo_GET mehods in non-root controllers
Tomas Doran [Tue, 5 Jun 2012 21:33:00 +0000 (22:33 +0100)]
Changes
lib/Catalyst/Action/REST.pm
t/catalyst-action-rest-action-dispatch.t
t/lib/Test/Catalyst/Action/REST/Controller/Actions.pm

diff --git a/Changes b/Changes
index d45db77..e93c04d 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,6 @@
+ Fix forwarded REST methods, e.g. foo_GET to be more
+ correctly displayed as a forward in the stats info.
+
 Tue  29 May 2012 20:19:00 BST - Release 1.01
  Add Catalyst::Action::Deserialize::JSON::XS
 
index a91beac..0445422 100644 (file)
@@ -99,14 +99,14 @@ sub _dispatch_rest_method {
 
     my ($code, $name);
 
+    # Execute normal 'foo' action.
+    $c->execute( $self->class, $self, @{ $c->req->args } );
+
     # Common case, for foo_GET etc
     if ( $code = $controller->action_for($rest_method) ) {
-        $c->execute( $self->class, $self, @{ $c->req->args } ); # Execute normal 'foo' action.
         return $c->forward( $code,  $c->req->args ); # Forward to foo_GET if it's an action
     }
     elsif ($code = $controller->can($rest_method)) {
-        # Execute normal action
-        $c->execute( $self->class, $self, @{ $c->req->args } );
         $name = $rest_method; # Stash name and code to run 'foo_GET' like an action below.
     }
 
@@ -127,8 +127,11 @@ sub _dispatch_rest_method {
 
     # localise stuff so we can dispatch the action 'as normal, but get
     # different stats shown, and different code run.
+    # Also get the full path for the action, and make it look like a forward
     local $self->{code} = $code;
-    local $self->{reverse} = $name;
+    my @name = split m{/}, $self->reverse;
+    $name[-1] = $name;
+    local $self->{reverse} = "-> " . join('/', @name);
 
     $c->execute( $self->class, $self, @{ $c->req->args } );
 }
index 348966b..0f1e60e 100644 (file)
@@ -2,7 +2,7 @@ use strict;
 use warnings;
 use Test::More 0.88;
 use FindBin;
-
+use Data::Dumper;
 use lib ( "$FindBin::Bin/lib", "$FindBin::Bin/../lib" );
 use Test::Rest;
 
@@ -11,35 +11,38 @@ my $t = Test::Rest->new( 'content_type' => 'text/plain' );
 
 use_ok 'Catalyst::Test', 'Test::Catalyst::Action::REST';
 
-foreach my $method (qw(GET DELETE POST PUT OPTIONS)) {
-    my $run_method = lc($method);
-    my $res;
-    if ( grep /$method/, qw(GET DELETE OPTIONS) ) {
-        $res = request( $t->$run_method( url => '/actions/test' ) );
-    } else {
-        $res = request(
-            $t->$run_method(
-                url  => '/actions/test',
-                data => '',
-            )
+foreach my $endpoint (qw/ test other_test /) {
+    foreach my $method (qw(GET DELETE POST PUT OPTIONS)) {
+        my $run_method = lc($method);
+        my $res;
+        if ( grep /$method/, qw(GET DELETE OPTIONS) ) {
+            $res = request( $t->$run_method( url => '/actions/' . $endpoint ) );
+        }
+        else {
+            $res = request(
+                $t->$run_method(
+                    url  => '/actions/' . $endpoint,
+                    data => '',
+                )
+            );
+        }
+        ok( $res->is_success, "$method request succeeded" ) or warn Dumper($res);
+        is(
+            $res->content,
+            "$method",
+            "$method request had proper response"
+        );
+        is(
+            $res->header('X-Was-In-TopLevel'),
+            '1',
+            "went through top level action for dispatching to $method"
+        );
+        is(
+            $res->header('Using-Action'),
+            'STATION',
+            "went through action for dispatching to $method"
         );
     }
-    ok( $res->is_success, "$method request succeeded" );
-    is(
-        $res->content,
-        "$method",
-        "$method request had proper response"
-    );
-    is(
-        $res->header('X-Was-In-TopLevel'),
-        '1',
-        "went through top level action for dispatching to $method"
-    );
-    is(
-        $res->header('Using-Action'),
-        'STATION',
-        "went through action for dispatching to $method"
-    );
 }
 
 my $res = request(
index 2e91a35..86210fb 100644 (file)
@@ -39,6 +39,37 @@ sub test_OPTIONS : Path('foobar') {
     $c->res->body('OPTIONS');
 }
 
+sub other_test :Local :ActionClass('+Catalyst::Action::REST') {
+    my ( $self, $c ) = @_;
+    $c->res->header('X-Was-In-TopLevel', 1);
+}
+
+sub other_test_GET {
+    my ( $self, $c ) = @_;
+    $c->res->body('GET');
+}
+
+sub other_test_POST {
+    my ( $self, $c ) = @_;
+    $c->res->body('POST');
+}
+
+sub other_test_PUT :ActionClass('+Test::Action::Class::Sub') {
+    my ( $self, $c ) = @_;
+    $c->res->body('PUT');
+}
+
+sub other_test_DELETE {
+    my ( $self, $c ) = @_;
+    $c->res->body('DELETE');
+}
+
+sub other_test_OPTIONS {
+    my ( $self, $c ) = @_;
+
+    $c->res->body('OPTIONS');
+}
+
 sub end : Private {} # Don't need serialization..
 
 1;