Add optional location parameter to status_acceped handler. RT#7369
Tomas Doran [Wed, 4 Jan 2012 15:51:26 +0000 (15:51 +0000)]
Changes
README
lib/Catalyst/Action/REST.pm
lib/Catalyst/Controller/REST.pm
t/catalyst-controller-rest.t
t/lib/Test/Catalyst/Action/REST/Controller/REST.pm

diff --git a/Changes b/Changes
index 45cb2a7..fb7d010 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,3 +1,5 @@
+  Add optional location parameter to status_acceped handler. RT#73691 (ghenry)
+
 Fri  09 Dec 2011 08:35:00 GMT - Release 0.94
   Add 403 Forbidden and 302 Not Found status methods to
   Catalyst::Controller::REST (Caleb Cushing)
diff --git a/README b/README
index b7d5b0a..f1259c4 100644 (file)
--- a/README
+++ b/README
@@ -102,8 +102,10 @@ CONTRIBUTORS
 
     J. Shirley <jshirley@gmail.com>
 
+    Gavin Henry <ghenry@surevoip.co.uk>
+
 COPYRIGHT
-    Copyright (c) 2006-2011 the above named AUTHOR and CONTRIBUTORS
+    Copyright (c) 2006-2012 the above named AUTHOR and CONTRIBUTORS
 
 LICENSE
     You may distribute this code under the same terms as Perl itself.
index fd1278f..adaf1aa 100644 (file)
@@ -224,9 +224,11 @@ Arthur Axel "fREW" Schmidt E<lt>frioux@gmail.comE<gt>
 
 J. Shirley E<lt>jshirley@gmail.comE<gt>
 
+Gavin Henry E<lt>ghenry@surevoip.co.ukE<gt>
+
 =head1 COPYRIGHT
 
-Copyright (c) 2006-2011 the above named AUTHOR and CONTRIBUTORS
+Copyright (c) 2006-2012 the above named AUTHOR and CONTRIBUTORS
 
 =head1 LICENSE
 
index e900987..ffd6992 100644 (file)
@@ -14,7 +14,7 @@ Catalyst::Controller::REST - A RESTful controller
     package Foo::Controller::Bar;
     use Moose;
     use namespace::autoclean;
-    
+
     BEGIN { extends 'Catalyst::Controller::REST' }
 
     sub thing : Local : ActionClass('REST') { }
@@ -39,15 +39,15 @@ Catalyst::Controller::REST - A RESTful controller
         my ( $self, $c ) = @_;
 
         $radiohead = $c->req->data->{radiohead};
-        
+
         $self->status_created(
             $c,
-            location => $c->req->uri->as_string,
+            location => $c->req->uri,
             entity => {
                 radiohead => $radiohead,
             }
         );
-    }     
+    }
 
 =head1 DESCRIPTION
 
@@ -81,7 +81,7 @@ which are described below.
 
 "The HTTP POST, PUT, and OPTIONS methods will all automatically
 L<deserialize|Catalyst::Action::Deserialize> the contents of
-C<< $c->request->body >> into the C<< $c->request->data >> hashref", based on 
+C<< $c->request->body >> into the C<< $c->request->data >> hashref", based on
 the request's C<Content-type> header. A list of understood serialization
 formats is L<below|/AVAILABLE SERIALIZERS>.
 
@@ -225,7 +225,7 @@ Your views should have a C<process> method like this:
       $c->response->body( $output );
       return 1;  # important
   }
-  
+
   sub serialize {
       my ( $self, $data ) = @_;
 
@@ -255,7 +255,7 @@ to act on, both callbacks are passed the controller object and the context
 
 =back
 
-By default, L<Catalyst::Controller::REST> will return a 
+By default, L<Catalyst::Controller::REST> will return a
 C<415 Unsupported Media Type> response if an attempt to use an unsupported
 content-type is made.  You can ensure that something is always returned by
 setting the C<default> config option:
@@ -268,12 +268,12 @@ C<text/x-yaml>.
 =head1 CUSTOM SERIALIZERS
 
 Implementing new Serialization formats is easy!  Contributions
-are most welcome!  If you would like to implement a custom serializer, 
+are most welcome!  If you would like to implement a custom serializer,
 you should create two new modules in the L<Catalyst::Action::Serialize>
 and L<Catalyst::Action::Deserialize> namespace.  Then assign your new
 class to the content-type's you want, and you're done.
 
-See L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize> 
+See L<Catalyst::Action::Serialize> and L<Catalyst::Action::Deserialize>
 for more information.
 
 =head1 STATUS HELPERS
@@ -352,7 +352,7 @@ Example:
 
   $self->status_created(
     $c,
-    location => $c->req->uri->as_string,
+    location => $c->req->uri,
     entity => {
         radiohead => "Is a good band!",
     }
@@ -374,14 +374,8 @@ sub status_created {
         },
     );
 
-    my $location;
-    if ( ref( $p{'location'} ) ) {
-        $location = $p{'location'}->as_string;
-    } else {
-        $location = $p{'location'};
-    }
     $c->response->status(201);
-    $c->response->header( 'Location' => $location );
+    $c->response->header( 'Location' => $p{location} );
     $self->_set_entity( $c, $p{'entity'} );
     return 1;
 }
@@ -389,11 +383,13 @@ sub status_created {
 =item status_accepted
 
 Returns a "202 ACCEPTED" response.  Takes an "entity" to serialize.
+Also takes optional "location" for queue type scenarios.
 
 Example:
 
   $self->status_accepted(
     $c,
+    location => $c->req->uri,
     entity => {
         status => "queued",
     }
@@ -404,9 +400,16 @@ Example:
 sub status_accepted {
     my $self = shift;
     my $c    = shift;
-    my %p    = Params::Validate::validate( @_, { entity => 1, }, );
+    my %p    = Params::Validate::validate(
+        @_,
+        {
+            location => { type => SCALAR | OBJECT, optional => 1 },
+            entity   => 1,
+        },
+    );
 
     $c->response->status(202);
+    $c->response->header( 'Location' => $p{location} ) if exists $p{location};
     $self->_set_entity( $c, $p{'entity'} );
     return 1;
 }
@@ -443,14 +446,8 @@ sub status_multiple_choices {
         },
     );
 
-    my $location;
-    if ( ref( $p{'location'} ) ) {
-        $location = $p{'location'}->as_string;
-    } else {
-        $location = $p{'location'};
-    }
     $c->response->status(300);
-    $c->response->header( 'Location' => $location ) if exists $p{'location'};
+    $c->response->header( 'Location' => $p{location} ) if exists $p{'location'};
     $self->_set_entity( $c, $p{'entity'} );
     return 1;
 }
@@ -458,7 +455,7 @@ sub status_multiple_choices {
 =item status_found
 
 Returns a "302 FOUND" response. Takes an "entity" to serialize.
-Also takes optional "location" for preferred choice.
+Also takes optional "location".
 
 =cut
 
@@ -473,14 +470,8 @@ sub status_found {
         },
     );
 
-    my $location;
-    if ( ref( $p{'location'} ) ) {
-        $location = $p{'location'}->as_string;
-    } else {
-        $location = $p{'location'};
-    }
     $c->response->status(302);
-    $c->response->header( 'Location' => $location ) if exists $p{'location'};
+    $c->response->header( 'Location' => $p{location} ) if exists $p{'location'};
     $self->_set_entity( $c, $p{'entity'} );
     return 1;
 }
index 5962bd6..e37dab8 100644 (file)
@@ -23,9 +23,11 @@ is_deeply(
 
 ok my $res = request( $t->get( url => '/rest/test_status_created' ) );
 is $res->code, 201, "... status created";
+is $res->header('Location'), '/rest', "...location of what was created";
 
 ok $res = request( $t->get( url => '/rest/test_status_accepted' ) );
 is $res->code, 202, "... status accepted";
+is $res->header('Location'), '/rest', "...location of what was accepted";
 
 ok $res = request( $t->get( url => '/rest/test_status_no_content' ) );
 is $res->code, 204, "... status no content";
@@ -36,6 +38,7 @@ is $res->code, 302, '... status found';
 is_deeply Load( $res->content ),
     { status => 'found' },
     "...  status found message";
+is $res->header('Location'), '/rest', "...location of what was found";
 
 ok $res = request( $t->get( url => '/rest/test_status_bad_request' ) );
 is $res->code, 400, '... status bad request';
@@ -66,6 +69,7 @@ is $res->code, 300, "... multiple choices";
 is_deeply Load($res->content),
     { choices => [qw(/rest/choice1 /rest/choice2)] },
     "... 300 multiple choices has response body";
+is $res->header('Location'), '/rest/choice1', "...main location of what was found";
 
 done_testing;
 
index c4a6be4..63fdea1 100644 (file)
@@ -40,7 +40,11 @@ sub test_status_found : Local {
 
 sub test_status_accepted : Local {
     my ( $self, $c ) = @_;
-    $self->status_accepted( $c, entity => { status => "queued", } );
+    $self->status_accepted(
+        $c,
+        location => '/rest',
+        entity => { status => "queued", }
+    );
 }
 
 sub test_status_no_content : Local {