create status 302 "found"
Caleb Cushing [Tue, 29 Nov 2011 01:11:29 +0000 (19:11 -0600)]
Signed-off-by: Caleb Cushing <xenoterracide@gmail.com>

lib/Catalyst/Controller/REST.pm
t/catalyst-controller-rest.t
t/lib/Test/Catalyst/Action/REST/Controller/REST.pm

index be66bef..92c7b0d 100644 (file)
@@ -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
index 14556fc..5962bd6 100644 (file)
@@ -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 ),
index 2783b87..c4a6be4 100644 (file)
@@ -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", } );