Skip to content

Commit

Permalink
Add optional location parameter to status_acceped handler. RT#7369
Browse files Browse the repository at this point in the history
  • Loading branch information
bobtfish committed Jan 4, 2012
1 parent c288a30 commit 259c53c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 36 deletions.
2 changes: 2 additions & 0 deletions 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)
Expand Down
4 changes: 3 additions & 1 deletion README
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion lib/Catalyst/Action/REST.pm
Expand Up @@ -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
Expand Down
57 changes: 24 additions & 33 deletions lib/Catalyst/Controller/REST.pm
Expand Up @@ -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') { }
Expand All @@ -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
Expand Down Expand Up @@ -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>.
Expand Down Expand Up @@ -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 ) = @_;
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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!",
}
Expand All @@ -374,26 +374,22 @@ 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;
}

=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",
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -443,22 +446,16 @@ 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;
}

=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

Expand All @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions t/catalyst-controller-rest.t
Expand Up @@ -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";
Expand All @@ -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';
Expand Down Expand Up @@ -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;

6 changes: 5 additions & 1 deletion t/lib/Test/Catalyst/Action/REST/Controller/REST.pm
Expand Up @@ -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 {
Expand Down

0 comments on commit 259c53c

Please sign in to comment.