Subject: | status_accepted should also allow and return a Location header |
Hi,
Patch attached with tests and new tests to test for Location headers for all status helpers that
support the Location header.
Thanks,
Gavin.
Subject: | status_accepted.patch |
diff --git a/README b/README
index b7d5b0a..f1259c4 100644
--- a/README
+++ b/README
@@ -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.
diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm
index fd1278f..adaf1aa 100644
--- a/lib/Catalyst/Action/REST.pm
+++ b/lib/Catalyst/Action/REST.pm
@@ -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
diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm
index e900987..32434ae 100644
--- a/lib/Catalyst/Controller/REST.pm
+++ b/lib/Catalyst/Controller/REST.pm
@@ -389,11 +389,13 @@ sub status_created {
=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->as_string,
entity => {
status => "queued",
}
@@ -404,9 +406,22 @@ 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 },
+ entity => 1,
+ },
+ );
+ my $location;
+ if ( ref( $p{'location'} ) ) {
+ $location = $p{'location'}->as_string;
+ } else {
+ $location = $p{'location'};
+ }
$c->response->status(202);
+ $c->response->header( 'Location' => $location );
$self->_set_entity( $c, $p{'entity'} );
return 1;
}
@@ -458,7 +473,7 @@ sub status_multiple_choices {
=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
diff --git a/t/catalyst-controller-rest.t b/t/catalyst-controller-rest.t
index 5962bd6..e37dab8 100644
--- a/t/catalyst-controller-rest.t
+++ b/t/catalyst-controller-rest.t
@@ -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";
@@ -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';
@@ -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;
diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm
index c4a6be4..5f649ad 100644
--- a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm
+++ b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm
@@ -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 {