From: Caleb Cushing Date: Tue, 29 Nov 2011 01:11:29 +0000 (-0600) Subject: create status 302 "found" X-Git-Tag: 1.08~61 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-Serialize-Data-Serializer.git;a=commitdiff_plain;h=e52456a4e301e6443284318748eb328641d8737a create status 302 "found" Signed-off-by: Caleb Cushing --- diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index be66bef..92c7b0d 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -455,6 +455,36 @@ sub status_multiple_choices { return 1; } +=item status_found + +Returns a "302 FOUND" response. Takes an "entity" to serialize. +Also takes optional "location" for preferred choice. + +=cut + +sub status_found { + my $self = shift; + my $c = shift; + my %p = Params::Validate::validate( + @_, + { + entity => 1, + location => { type => SCALAR | OBJECT, optional => 1 }, + }, + ); + + 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'}; + $self->_set_entity( $c, $p{'entity'} ); + return 1; +} + =item status_bad_request Returns a "400 BAD REQUEST" response. Takes a "message" argument diff --git a/t/catalyst-controller-rest.t b/t/catalyst-controller-rest.t index 14556fc..5962bd6 100644 --- a/t/catalyst-controller-rest.t +++ b/t/catalyst-controller-rest.t @@ -31,6 +31,12 @@ ok $res = request( $t->get( url => '/rest/test_status_no_content' ) ); is $res->code, 204, "... status no content"; is $res->content, '', '... no content'; +ok $res = request( $t->get( url => '/rest/test_status_found' ) ); +is $res->code, 302, '... status found'; +is_deeply Load( $res->content ), + { status => 'found' }, + "... status found message"; + ok $res = request( $t->get( url => '/rest/test_status_bad_request' ) ); is $res->code, 400, '... status bad request'; is_deeply Load( $res->content ), diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm index 2783b87..c4a6be4 100644 --- a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm +++ b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm @@ -29,6 +29,15 @@ sub test_status_multiple_choices : Local { ); } +sub test_status_found : Local { + my ( $self, $c ) = @_; + $self->status_found( + $c, + location => '/rest', + entity => { status => 'found' }, + ); +} + sub test_status_accepted : Local { my ( $self, $c ) = @_; $self->status_accepted( $c, entity => { status => "queued", } );