t/00_use.t
t/01-good-message.t
t/02-bad-action.t
+t/03-json-message.t
t/server.pl
testapp/lib/StompTestApp.pm
testapp/lib/StompTestApp/Controller/TestController.pm
+testapp/lib/StompTestApp/Controller/TestJsonController.pm
testapp/lib/StompTestApp/stomptestapp.yml
testapp/script/stomptestapp_stomp.pl
testapp/script/stomptestapp_stomp_workers.pl
requires 'MooseX::Workers' => '0.05';
requires 'Net::Stomp' => '0.34';
requires 'YAML::XS' => '0.32';
+requires 'Data::Serializer' => '0.49';
requires 'namespace::autoclean' => '0.05';
test_requires 'Alien::ActiveMQ' => '0.00003';
no_index package => 'StompTestApp';
no_index package => 'StompTestApp::Controller::TestController';
+no_index package => 'StompTestApp::Controller::TestJsonController';
auto_install;
WriteAll;
Moose
Net::Stomp
YAML::XS
+ Data::Serializer
Catalyst::Engine::Embeddable
Catalyst::Runtime 5.80003
namespace::autoclean
-The supplied workers script requires MooseX::Workers.
+The supplied workers script requires MooseX::Workers, and you'll need
+the relevant modules to support Data::Serializer if you choose
+something other than YAML as a message format.
COPYRIGHT AND LICENCE
package Catalyst::Controller::MessageDriven;
use Moose;
-use YAML::XS qw/ LoadFile Dump /;
+use Data::Serializer;
BEGIN { extends 'Catalyst::Controller' }
=cut
+__PACKAGE__->config( serializer => 'YAML' );
+
sub begin : Private {
my ($self, $c) = @_;
# Deserialize the request message
-
my $message;
+ my $serializer = $self->config->{serializer};
+ my $s = Data::Serializer->new( serializer => $serializer );
eval {
my $body = $c->request->body;
- $message = LoadFile( "$body" );
+ open my $IN, "$body" or die "can't open temp file $body";
+ $message = $s->raw_deserialize(do { local $/; <$IN> });
};
if ($@) {
# can't reply - reply_to is embedded in the message
# Serialize the response
my $output;
+ my $serializer = $self->config->{serializer};
+ my $s = Data::Serializer->new( serializer => $serializer );
eval {
- $output = Dump( $c->stash->{response} );
+ $output = $s->raw_serialize( $c->stash->{response} );
};
if ($@) {
my $error = "exception in serialize: $@";
--- /dev/null
+use Test::More;
+
+# Tests which expect a STOMP server like ActiveMQ to exist on
+# localhost:61613, which is what you get if you just get the ActiveMQ
+# distro and run its out-of-the-box config.
+
+use Net::Stomp;
+
+eval {
+ use JSON;
+};
+if ($@) {
+ plan 'skip_all' => 'JSON not installed, skipping JSON-format test';
+}
+
+use FindBin;
+use lib "$FindBin::Bin";
+require 'server.pl';
+
+plan tests => 11;
+
+my $frame = $stomp->connect();
+ok($frame, 'connect to MQ server ok');
+
+my $reply_to = sprintf '%s:1', $frame->headers->{session};
+ok($frame->headers->{session}, 'got a session');
+ok(length $reply_to > 2, 'valid-looking reply_to queue');
+
+ok($stomp->subscribe( { destination => '/temp-queue/reply' } ), 'subscribe to temp queue');
+
+my $message = {
+ payload => { foo => 1, bar => 2 },
+ reply_to => $reply_to,
+ type => 'testaction',
+ };
+my $text = to_json($message);
+ok($text, 'compose message');
+
+$stomp->send( { destination => '/queue/testjsoncontroller', body => $text } );
+
+my $reply_frame = $stomp->receive_frame();
+ok($reply_frame, 'got a reply');
+ok($reply_frame->headers->{destination} eq "/remote-temp-queue/$reply_to", 'came to correct temp queue');
+ok($reply_frame->body, 'has a body');
+
+my $response = from_json($reply_frame->body);
+
+ok($response, 'JSON response ok');
+ok($response->{type} eq 'testaction_response', 'correct type');
+
+ok($stomp->disconnect, 'disconnected');
+
}
}
-# First fire off the server
$SIG{CHLD} = 'IGNORE';
unless (fork()) {
system("$^X -Ilib -Itestapp/lib testapp/script/stomptestapp_stomp.pl --oneshot");
exit 0;
}
print STDERR "server started, waiting for spinup...";
-sleep 2;
+sleep 20;
--- /dev/null
+package # Hide from PAUSE
+ StompTestApp::Controller::TestJsonController;
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'Catalyst::Controller::MessageDriven' };
+
+__PACKAGE__->config( serializer => 'JSON' );
+
+sub testaction : Local {
+ my ($self, $c, $request) = @_;
+
+ # Reply with a minimal response message
+ my $response = { type => 'testaction_response' };
+ $c->stash->{response} = $response;
+}
+
+1;