From: Tomas Doran Date: Thu, 6 May 2010 08:21:49 +0000 (+0100) Subject: Commit patch from Fabien Wernli X-Git-Tag: 0.84~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-REST.git;a=commitdiff_plain;h=bdff70a9a41c75023febf0783e4b014d02743673 Commit patch from Fabien Wernli --- diff --git a/lib/Catalyst/Action/Serialize.pm b/lib/Catalyst/Action/Serialize.pm index 1ee6e1c..2d283ec 100644 --- a/lib/Catalyst/Action/Serialize.pm +++ b/lib/Catalyst/Action/Serialize.pm @@ -25,7 +25,7 @@ sub execute { return 1 if $c->req->method eq 'HEAD'; return 1 if length( $c->response->body ); return 1 if scalar @{ $c->error }; - return 1 if $c->response->status =~ /^(?:204|3\d\d)$/; + return 1 if $c->response->status =~ /^(?:204)$/; my ( $sclass, $sarg, $content_type ) = $self->_load_content_plugins( "Catalyst::Action::Serialize", diff --git a/lib/Catalyst/Controller/REST.pm b/lib/Catalyst/Controller/REST.pm index fe3d49f..87ee40c 100644 --- a/lib/Catalyst/Controller/REST.pm +++ b/lib/Catalyst/Controller/REST.pm @@ -398,6 +398,36 @@ sub status_no_content { return 1.; } +=item status_multiple_choices + +Returns a "300 MULTIPLE CHOICES" response. Takes an "entity" to serialize, which should +provide list of possible locations. Also takes optional "location" for preferred choice. + +=cut + +sub status_multiple_choices { + 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(300); + $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 5d8f731..18a0640 100644 --- a/t/catalyst-controller-rest.t +++ b/t/catalyst-controller-rest.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 18; +use Test::More tests => 20; use YAML::Syck; use FindBin; @@ -48,3 +48,7 @@ is $res->code, 410, '... status gone'; is_deeply Load( $res->content ), { error => "Document have been deleted by foo" }, "... status gone message"; + +ok $res = request( $t->get( url => '/rest/test_status_multiple_choices' ) ); +is $res->code, 300, "... multiple choices"; + diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm index e434159..f9f7147 100644 --- a/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm +++ b/t/lib/Test/Catalyst/Action/REST/Controller/REST.pm @@ -20,6 +20,15 @@ sub test_status_created : Local { ); } +sub test_status_multiple_choices : Local { + my ( $self, $c ) = @_; + $self->status_multiple_choices( + $c, + location => '/rest/choice1', + entity => { choices => [qw(/rest/choice1 /rest/choice2)] } + ); +} + sub test_status_accepted : Local { my ( $self, $c ) = @_; $self->status_accepted( $c, entity => { status => "queued", } );