From: Tomas Doran Date: Tue, 11 Jan 2011 23:07:31 +0000 (+0000) Subject: Fix warning and test blank body behavior X-Git-Tag: 1.08~107 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Action-Serialize-Data-Serializer.git;a=commitdiff_plain;h=786c212ff89e2b2e264efb1067a03505ab28b2b1 Fix warning and test blank body behavior --- diff --git a/Changes b/Changes index ca20d2b..cfbea6e 100644 --- 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 diff --git a/Makefile.PL b/Makefile.PL index da7f27d..fb4305d 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -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); diff --git a/lib/Catalyst/Action/REST.pm b/lib/Catalyst/Action/REST.pm index 10d7c32..f5f7d56 100644 --- a/lib/Catalyst/Action/REST.pm +++ b/lib/Catalyst/Action/REST.pm @@ -161,7 +161,7 @@ You likely want to look at L, which implements a sensible set of defaults for a controller doing REST. This class automatically adds the L 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 instead. diff --git a/lib/Catalyst/Action/Serialize.pm b/lib/Catalyst/Action/Serialize.pm index 29ee605..57f4bbb 100644 --- a/lib/Catalyst/Action/Serialize.pm +++ b/lib/Catalyst/Action/Serialize.pm @@ -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)$/; diff --git a/t/catalyst-action-serialize.t b/t/catalyst-action-serialize.t index 78ce12b..dfdbcec 100644 --- a/t/catalyst-action-serialize.t +++ b/t/catalyst-action-serialize.t @@ -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; + diff --git a/t/lib/Test/Catalyst/Action/REST.pm b/t/lib/Test/Catalyst/Action/REST.pm index c6422bd..0b992b7 100644 --- a/t/lib/Test/Catalyst/Action/REST.pm +++ b/t/lib/Test/Catalyst/Action/REST.pm @@ -17,6 +17,7 @@ __PACKAGE__->config( }, ); __PACKAGE__->setup; -__PACKAGE__->log( Test::Catalyst::Log->new ); +__PACKAGE__->log( Test::Catalyst::Log->new ) + unless __PACKAGE__->debug; 1; diff --git a/t/lib/Test/Catalyst/Action/REST/Controller/Serialize.pm b/t/lib/Test/Catalyst/Action/REST/Controller/Serialize.pm index 9d358c4..8e0c051 100644 --- a/t/lib/Test/Catalyst/Action/REST/Controller/Serialize.pm +++ b/t/lib/Test/Catalyst/Action/REST/Controller/Serialize.pm @@ -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;