Fix warning and test blank body behavior
Tomas Doran [Tue, 11 Jan 2011 23:07:31 +0000 (23:07 +0000)]
Changes
Makefile.PL
lib/Catalyst/Action/REST.pm
lib/Catalyst/Action/Serialize.pm
t/catalyst-action-serialize.t
t/lib/Test/Catalyst/Action/REST.pm
t/lib/Test/Catalyst/Action/REST/Controller/Serialize.pm

diff --git a/Changes b/Changes
index ca20d2b..cfbea6e 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,13 @@
   Fix documentation for overriding Serialize and Deserialize actions
   in Catalyst::Controller::REST.
 
+  Avoid warning with empty response bodies and new Catalyst version
+  (>= 5.80030)
+
+  Returning a body of '' is now possible - Catalyst::Action::Serialize
+  acts like Catalyst::Action::RenderView (>= 0.16) by using the has_body
+  predicate in Catalyst::Response (>= 5.80030)
+
 Wed  3 Nov 2010 19:46:00 GMT - Release 0.87
 
   Fix Request class role when used with new Moose and other request
index da7f27d..fb4305d 100644 (file)
@@ -11,7 +11,7 @@ all_from 'lib/Catalyst/Action/REST.pm';
 
 requires 'Moose' => '1.03';
 requires 'namespace::autoclean';
-requires('Catalyst::Runtime'         => '5.80');
+requires('Catalyst::Runtime'         => '5.80030');
 requires('Params::Validate'          => '0.76');
 requires('YAML::Syck'                => '0.67');
 requires('Module::Pluggable::Object' => undef);
index 10d7c32..f5f7d56 100644 (file)
@@ -161,7 +161,7 @@ You likely want to look at L<Catalyst::Controller::REST>, which implements a
 sensible set of defaults for a controller doing REST.
 
 This class automatically adds the L<Catalyst::TraitFor::Request::REST> role to
-your request class.  If you're writing a webapp which provides RESTful
+your request class.  If you're writing a web application which provides RESTful
 responses and still needs to accommodate web browsers, you may prefer to use
 L<Catalyst::TraitFor::Request::REST::ForBrowsers> instead.
 
index 29ee605..57f4bbb 100644 (file)
@@ -23,7 +23,7 @@ sub execute {
     $self->maybe::next::method(@_);
 
     return 1 if $c->req->method eq 'HEAD';
-    return 1 if length( $c->response->body );
+    return 1 if $c->response->has_body;
     return 1 if scalar @{ $c->error };
     return 1 if $c->response->status =~ /^(?:204)$/;
 
index 78ce12b..dfdbcec 100644 (file)
@@ -1,6 +1,6 @@
 use strict;
 use warnings;
-use Test::More tests => 9;
+use Test::More 0.88;
 use Data::Serializer;
 use FindBin;
 
@@ -35,4 +35,17 @@ $res2 = request($t->get(url => '/serialize/test_second'));
 ok( $res2->is_success, 'request succeeded (deprecated config)' );
 is( $res2->content, "{'lou' => 'is my cat'}", "request returned proper data");
 
-1;
+$res = request($t->get(url => '/serialize/empty_serialized'));
+is $res->content, q[{'foo' => 'bar'}], 'normal case ok';
+ok $res->header('Content-Length'), 'set content-length when we serialize';
+
+$res = request($t->get(url => '/serialize/empty_not_serialized_undef'));
+is $res->content, '', "body explicitly set to undef results in '' content";
+ok !$res->header('Content-Length'), "body explicitly set to undef - no automatic content-length";
+
+$res = request($t->get(url => '/serialize/empty_not_serialized_blank'));
+is $res->content, '', "body explicitly set to '' results in '' content";
+ok !$res->header('Content-Length'), "body explicitly set to '' - no automatic content-length";
+
+done_testing;
+
index c6422bd..0b992b7 100644 (file)
@@ -17,6 +17,7 @@ __PACKAGE__->config(
     },
 );
 __PACKAGE__->setup;
-__PACKAGE__->log( Test::Catalyst::Log->new );
+__PACKAGE__->log( Test::Catalyst::Log->new )
+    unless __PACKAGE__->debug;
 
 1;
index 9d358c4..8e0c051 100644 (file)
@@ -32,4 +32,26 @@ sub test_second :Local :ActionClass('Serialize') {
     };
 }
 
+# For testing saying 'here is an explicitly empty body, do not serialize'
+sub empty : Chained('/') PathPart('serialize') CaptureArgs(0) {
+    my ($self, $c) = @_;
+    $c->stash( rest => { foo => 'bar' } );
+}
+
+# Normal case
+sub empty_serialized :Chained('empty') Args(0) ActionClass('Serialize') {
+}
+
+# Undef body
+sub empty_not_serialized_undef :Chained('empty') Args(0) ActionClass('Serialize') {
+    my ($self, $c) = @_;
+    $c->res->body(undef);
+}
+
+# Blank body
+sub empty_not_serialized_blank :Chained('empty') Args(0) ActionClass('Serialize') {
+    my ($self, $c) = @_;
+    $c->res->body('');
+}
+
 1;