Handle "no type" in messages better - attempt to reply saying so.
Chris Andrews [Thu, 4 Jun 2009 13:30:21 +0000 (14:30 +0100)]
lib/Catalyst/Controller/MessageDriven.pm
t/04-message-driven-request.t

index 9b5718e..1969bfd 100644 (file)
@@ -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;
index 7d169ed..d194e7d 100644 (file)
@@ -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');