Use Data::Serializer, instead of hardcoding YAML. Allow the type of serializer
Chris Andrews [Sat, 30 May 2009 12:06:14 +0000 (13:06 +0100)]
to be overridden per-controller, in the Catalyst config. We still require YAML,
but now you can actually choose to use something else.

Also add a test for overriding to JSON.

MANIFEST
Makefile.PL
README
lib/Catalyst/Controller/MessageDriven.pm
t/03-json-message.t [new file with mode: 0644]
t/server.pl
testapp/lib/StompTestApp/Controller/TestJsonController.pm [new file with mode: 0644]

index 8d6be73..e5b1e6e 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -19,9 +19,11 @@ README
 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
index 1796805..95ab173 100644 (file)
@@ -12,12 +12,14 @@ requires 'Moose' => undef;
 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;
diff --git a/README b/README
index 00042fe..0ab7568 100644 (file)
--- a/README
+++ b/README
@@ -23,11 +23,14 @@ This module requires these other modules and libraries:
      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
 
index 10e645d..9e8d0d3 100644 (file)
@@ -1,6 +1,6 @@
 package Catalyst::Controller::MessageDriven;
 use Moose;
-use YAML::XS qw/ LoadFile Dump /;
+use Data::Serializer;
 
 BEGIN { extends 'Catalyst::Controller' }
 
@@ -32,15 +32,19 @@ YAML determines the action dispatched to.
 
 =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
@@ -67,8 +71,10 @@ sub end : Private {
 
        # 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: $@";
diff --git a/t/03-json-message.t b/t/03-json-message.t
new file mode 100644 (file)
index 0000000..16375c9
--- /dev/null
@@ -0,0 +1,52 @@
+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');
+
index 981f199..4bd7bdc 100644 (file)
@@ -22,12 +22,11 @@ if ($@) {
     }
 }
 
-# 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;
 
diff --git a/testapp/lib/StompTestApp/Controller/TestJsonController.pm b/testapp/lib/StompTestApp/Controller/TestJsonController.pm
new file mode 100644 (file)
index 0000000..daf9ffb
--- /dev/null
@@ -0,0 +1,18 @@
+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;