From: Caleb Cushing Date: Fri, 25 Nov 2011 01:44:45 +0000 (-0600) Subject: add 403 Forbidden X-Git-Tag: 0.94~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=550807bc7de841d2a615b065c2e4afedd44b0d84;p=catagits%2FCatalyst-Action-REST.git add 403 Forbidden Signed-off-by: Caleb Cushing --- diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index 45d5b7b..be66bef 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -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 diff --git a/t/catalyst-controller-rest.t b/t/catalyst-controller-rest.t index e45f0b9..14556fc 100644 --- a/t/catalyst-controller-rest.t +++ b/t/catalyst-controller-rest.t @@ -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 ), diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm index f9f7147..2783b87 100644 --- a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm +++ b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm @@ -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,