Commit patch from Fabien Wernli
Tomas Doran [Thu, 6 May 2010 08:21:49 +0000 (09:21 +0100)]
lib/Catalyst/Action/Serialize.pm
lib/Catalyst/Controller/REST.pm
t/catalyst-controller-rest.t
t/lib/Test/Catalyst/Action/REST/Controller/REST.pm

index 1ee6e1c..2d283ec 100644 (file)
@@ -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",
index fe3d49f..87ee40c 100644 (file)
@@ -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
index 5d8f731..18a0640 100644 (file)
@@ -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";
+
index e434159..f9f7147 100644 (file)
@@ -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", } );