16375c9df83da1e5f3e25a10b053fe9f17fbe078
[catagits/Catalyst-Engine-STOMP.git] / t / 03-json-message.t
1 use Test::More;
2
3 # Tests which expect a STOMP server like ActiveMQ to exist on
4 # localhost:61613, which is what you get if you just get the ActiveMQ
5 # distro and run its out-of-the-box config.
6
7 use Net::Stomp;
8
9 eval {
10         use JSON;
11 };
12 if ($@) {
13         plan 'skip_all' => 'JSON not installed, skipping JSON-format test';
14 }
15
16 use FindBin;
17 use lib "$FindBin::Bin";
18 require 'server.pl';
19
20 plan tests => 11;
21
22 my $frame = $stomp->connect();
23 ok($frame, 'connect to MQ server ok');
24
25 my $reply_to = sprintf '%s:1', $frame->headers->{session};
26 ok($frame->headers->{session}, 'got a session');
27 ok(length $reply_to > 2, 'valid-looking reply_to queue');
28
29 ok($stomp->subscribe( { destination => '/temp-queue/reply' } ), 'subscribe to temp queue');
30
31 my $message = {
32                payload => { foo => 1, bar => 2 },
33                reply_to => $reply_to,
34                type => 'testaction',
35               };
36 my $text = to_json($message);
37 ok($text, 'compose message');
38
39 $stomp->send( { destination => '/queue/testjsoncontroller', body => $text } );
40
41 my $reply_frame = $stomp->receive_frame();
42 ok($reply_frame, 'got a reply');
43 ok($reply_frame->headers->{destination} eq "/remote-temp-queue/$reply_to", 'came to correct temp queue');
44 ok($reply_frame->body, 'has a body');
45
46 my $response = from_json($reply_frame->body);
47
48 ok($response, 'JSON response ok');
49 ok($response->{type} eq 'testaction_response', 'correct type');
50
51 ok($stomp->disconnect, 'disconnected');
52