From: Chris Andrews Date: Thu, 4 Jun 2009 13:30:21 +0000 (+0100) Subject: Handle "no type" in messages better - attempt to reply saying so. X-Git-Tag: 0.05~16^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Engine-STOMP.git;a=commitdiff_plain;h=0e4b5a7d470856adf7d8ee70fcb185ed5bff012e Handle "no type" in messages better - attempt to reply saying so. --- diff --git a/lib/Catalyst/Controller/MessageDriven.pm b/lib/Catalyst/Controller/MessageDriven.pm index 9b5718e..1969bfd 100644 --- a/lib/Catalyst/Controller/MessageDriven.pm +++ b/lib/Catalyst/Controller/MessageDriven.pm @@ -74,7 +74,8 @@ sub end : Private { my $error = join "\n", @{$c->error}; $c->stash->{response} = { status => 'ERROR', error => $error }; $output = $s->serialize( $c->stash->{response} ); - $c->error(0); # clear errors, so our response isn't clobbered + $c->clear_errors; + $c->response->status(400); } # Serialize the response @@ -83,9 +84,9 @@ sub end : Private { }; if ($@) { my $error = "exception in serialize: $@"; - $c->error($error); $c->stash->{response} = { status => 'ERROR', error => $error }; $output = $s->serialize( $c->stash->{response} ); + $c->response->status(400); } $c->response->output( $output ); @@ -97,7 +98,12 @@ sub default : Private { # Forward the request to the appropriate action, based on the # message type. my $action = $c->stash->{request}->{type}; - $c->forward($action, [$c->stash->{request}]); + if (defined $action) { + $c->forward($action, [$c->stash->{request}]); + } + else { + $c->error('no message type specified'); + } } __PACKAGE__->meta->make_immutable; diff --git a/t/04-message-driven-request.t b/t/04-message-driven-request.t index 7d169ed..d194e7d 100644 --- a/t/04-message-driven-request.t +++ b/t/04-message-driven-request.t @@ -1,13 +1,20 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 5; use FindBin; use lib "$FindBin::Bin/../testapp/lib"; BEGIN { use_ok 'Catalyst::Test::MessageDriven', 'StompTestApp' }; +# successful request - type is minimum attributes my $req = "---\ntype: ping\n"; my $res = request('testcontroller', $req); ok($res, 'response to ping message'); ok($res->is_success, 'successful response'); + +# unsuccessful empty request - no type +$req = "--- ~\n"; +$res = request('testcontroller', $req); +ok($res, 'response to empty message'); +ok($res->is_error, 'unsuccessful response');