From: Tomas Doran Date: Wed, 4 Jan 2012 15:51:26 +0000 (+0000) Subject: Add optional location parameter to status_acceped handler. RT#7369 X-Git-Tag: 0.95~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=commitdiff_plain;h=259c53c7c52a591187ae5d70e10d2ffc241a48f6 Add optional location parameter to status_acceped handler. RT#7369 --- diff --git a/Changes b/Changes index 45cb2a7..fb7d010 100644 --- 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 --- a/README +++ b/README @@ -102,8 +102,10 @@ CONTRIBUTORS J. Shirley + Gavin Henry + 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. diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm index fd1278f..adaf1aa 100644 --- a/lib/Catalyst/Action/REST.pm +++ b/lib/Catalyst/Action/REST.pm @@ -224,9 +224,11 @@ Arthur Axel "fREW" Schmidt Efrioux@gmail.comE J. Shirley Ejshirley@gmail.comE +Gavin Henry Eghenry@surevoip.co.ukE + =head1 COPYRIGHT -Copyright (c) 2006-2011 the above named AUTHOR and CONTRIBUTORS +Copyright (c) 2006-2012 the above named AUTHOR and CONTRIBUTORS =head1 LICENSE diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index e900987..ffd6992 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -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 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 header. A list of understood serialization formats is L. @@ -225,7 +225,7 @@ Your views should have a C 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 will return a +By default, L 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 config option: @@ -268,12 +268,12 @@ C. =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 and L namespace. Then assign your new class to the content-type's you want, and you're done. -See L and L +See L and L 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; } diff --git a/t/catalyst-controller-rest.t b/t/catalyst-controller-rest.t index 5962bd6..e37dab8 100644 --- a/t/catalyst-controller-rest.t +++ b/t/catalyst-controller-rest.t @@ -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; diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm index c4a6be4..63fdea1 100644 --- a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm +++ b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm @@ -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 {