Use REST::ForBrowsers action class
Dave Rolsky [Sat, 22 Jan 2011 16:22:01 +0000 (10:22 -0600)]
If given a non-absolute path, make it a local path based on the current controller namespace.

lib/CatalystX/Routes.pm
t/lib/MyApp1/Controller/C1.pm
t/routes.t

index e284835..1801983 100644 (file)
@@ -42,7 +42,7 @@ sub del {
 sub _add_route {
     my $rest = shift;
     my $meta = shift;
-    my ( $name, $attrs, $sub ) = _process_args(@_);
+    my ( $name, $attrs, $sub ) = _process_args( $meta, @_ );
 
     my $meth_base = '__route__' . $name;
 
@@ -55,10 +55,6 @@ sub _add_route {
     return;
 }
 
-sub path_part ($) {
-    return ( PathPart => [ $_[0] ] );
-}
-
 sub chained ($) {
     return ( Chained => [ $_[0] ] );
 }
@@ -71,11 +67,16 @@ sub capture_args ($) {
     return ( CaptureArgs => [ $_[0] ] );
 }
 
+sub path_part ($) {
+    return ( PathPart => [ $_[0] ] );
+}
+
 sub action ($) {
     return ( ActionClass => [ $_[0] ] );
 }
 
 sub _process_args {
+    my $meta = shift;
     my $path = shift;
     my $sub  = pop;
 
@@ -90,18 +91,22 @@ sub _process_args {
 
     my %p = @_;
 
-    $p{ActionClass} ||= 'REST';
+    $p{ActionClass} ||= 'REST::ForBrowsers';
 
     unless ( exists $p{Chained} ) {
         $p{Chained} = q{/};
 
-        unless ( exists $p{PathPart} ) {
-            ( my $part = $path ) =~ s{^/}{};
+        unless ( $p{PathPart} ) {
+            my $part = $path;
+            unless ( $part =~ s{^/}{} ) {
+                $part = $meta->name()->action_namespace('FakeConfig') . q{/} . $part;
+            }
+
             $p{PathPart} = [$part];
         }
     }
 
-    unless ( exists $p{Args} ) {
+    unless ( $p{Args} || $p{Local} ) {
         $p{Args} = [0];
     }
 
@@ -144,4 +149,14 @@ sub _STRINGLIKE0 ($) {
         && length "$_[0]" );
 }
 
+{
+    # This is a nasty hack around some weird back compat code in
+    # Catalyst::Controller->action_namespace
+    package FakeConfig;
+
+    sub config {
+        return { case_sensitive => 0 };
+    }
+}
+
 1;
index d8cc767..0646229 100644 (file)
@@ -23,6 +23,16 @@ put '/foo' => \&_put;
 
 del '/foo' => \&_del;
 
+get 'bar'=> \&_get;
+
+get_html 'bar'=> \&_get_html;
+
+post 'bar'=> \&_post;
+
+put 'bar'=> \&_put;
+
+del 'bar'=> \&_del;
+
 sub normal : Chained('/') : Args(0) {
     $REQ{normal}++;
 }
index 9f63dd2..15d0368 100644 (file)
@@ -7,58 +7,109 @@ use lib 't/lib';
 use Catalyst::Test 'MyApp1';
 use HTTP::Request::Common qw( GET PUT POST DELETE );
 
-get('/foo');
-
-is(
-    $MyApp1::Controller::C1::REQ{get}, 1,
-    'GET request for /foo went to the right sub'
-);
-
-request(
-    GET '/foo',
-    [
-        Accept => '*/*',
-    ]
-);
-
-is(
-    $MyApp1::Controller::C1::REQ{get_html}, 1,
-    'GET request for /foo that looks like a browser went to the right sub'
-);
-
-request( POST '/foo' );
-
-is(
-    $MyApp1::Controller::C1::REQ{post}, 1,
-    'POST request for /foo went to the right sub'
-);
-
-request( PUT '/foo' );
-
-is(
-    $MyApp1::Controller::C1::REQ{put}, 1,
-    'PUT request for /foo went to the right sub'
-);
-
-request( DELETE '/foo' );
-
-is(
-    $MyApp1::Controller::C1::REQ{delete}, 1,
-    'DELETE request for /foo went to the right sub'
-);
-
-get('/normal');
-
-is(
-    $MyApp1::Controller::C1::REQ{normal}, 1,
-    'GET request for /norma went to the right sub'
-);
-
-request( POST '/normal' );
-
-is(
-    $MyApp1::Controller::C1::REQ{normal}, 2,
-    'POST request for /norma went to the right sub'
-);
+{
+    request(
+        GET '/foo',
+        [
+            Accept => 'application/json',
+        ]
+    );
+
+    is(
+        $MyApp1::Controller::C1::REQ{get}, 1,
+        'GET request for /foo went to the right sub'
+    );
+
+    request(
+        GET '/foo',
+        [
+            Accept => '*/*',
+        ]
+    );
+
+    is(
+        $MyApp1::Controller::C1::REQ{get_html}, 1,
+        'GET request for /foo that looks like a browser went to the right sub'
+    );
+
+    request( POST '/foo' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{post}, 1,
+        'POST request for /foo went to the right sub'
+    );
+
+    request( PUT '/foo' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{put}, 1,
+        'PUT request for /foo went to the right sub'
+    );
+
+    request( DELETE '/foo' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{delete}, 1,
+        'DELETE request for /foo went to the right sub'
+    );
+}
+
+{
+    get('c1/bar');
+
+    is(
+        $MyApp1::Controller::C1::REQ{get}, 2,
+        'GET request for c1/bar went to the right sub'
+    );
+
+    request(
+        GET 'c1/bar',
+        [
+            Accept => '*/*',
+        ]
+    );
+
+    is(
+        $MyApp1::Controller::C1::REQ{get_html}, 2,
+        'GET request for c1/bar that looks like a browser went to the right sub'
+    );
+
+    request( POST 'c1/bar' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{post}, 2,
+        'POST request for c1/bar went to the right sub'
+    );
+
+    request( PUT 'c1/bar' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{put}, 2,
+        'PUT request for c1/bar went to the right sub'
+    );
+
+    request( DELETE 'c1/bar' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{delete}, 2,
+        'DELETE request for c1/bar went to the right sub'
+    );
+}
+
+{
+    get('/normal');
+
+    is(
+        $MyApp1::Controller::C1::REQ{normal}, 1,
+        'GET request for /norma went to the right sub'
+    );
+
+    request( POST '/normal' );
+
+    is(
+        $MyApp1::Controller::C1::REQ{normal}, 2,
+        'POST request for /norma went to the right sub'
+    );
+}
 
 done_testing();