add 403 Forbidden
Caleb Cushing [Fri, 25 Nov 2011 01:44:45 +0000 (19:44 -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 45d5b7b..be66bef 100644 (file)
@@ -481,6 +481,32 @@ sub status_bad_request {
     return 1;
 }
 
+=item status_forbidden
+
+Returns a "403 FORBIDDEN" response.  Takes a "message" argument
+as a scalar, which will become the value of "error" in the serialized
+response.
+
+Example:
+
+  $self->status_forbidden(
+    $c,
+    message => "access denied",
+  );
+
+=cut
+
+sub status_forbidden {
+    my $self = shift;
+    my $c    = shift;
+    my %p    = Params::Validate::validate( @_, { message => { type => SCALAR }, }, );
+
+    $c->response->status(403);
+    $c->log->debug( "Status Forbidden: " . $p{'message'} ) if $c->debug;
+    $self->_set_entity( $c, { error => $p{'message'} } );
+    return 1;
+}
+
 =item status_not_found
 
 Returns a "404 NOT FOUND" response.  Takes a "message" argument
index e45f0b9..14556fc 100644 (file)
@@ -37,6 +37,12 @@ is_deeply Load( $res->content ),
     { error => "Cannot do what you have asked!" },
     "...  status bad request message";
 
+ok $res = request( $t->get( url => '/rest/test_status_forbidden' ) );
+is $res->code, 403, '... status forbidden';
+is_deeply Load( $res->content ),
+    { error => "access denied" },
+    "...  status forbidden";
+
 ok $res = request( $t->get( url => '/rest/test_status_not_found' ) );
 is $res->code, 404, '... status not found';
 is_deeply Load( $res->content ),
index f9f7147..2783b87 100644 (file)
@@ -45,6 +45,12 @@ sub test_status_bad_request : Local {
         message => "Cannot do what you have asked!", );
 }
 
+sub test_status_forbidden : Local {
+    my ( $self, $c ) = @_;
+    $self->status_forbidden ( $c,
+        message => "access denied", );
+}
+
 sub test_status_not_found : Local {
     my ( $self, $c ) = @_;
     $self->status_not_found( $c,