@@ -2,7 +2,7 @@ use Test;
22use Test::IO::Socket::Async;
33use Stomp;
44
5- plan 15 ;
5+ plan 28 ;
66
77constant $test-socket = Test::IO::Socket::Async.new;
88my \TestableServer = Stomp::Server but role {
@@ -38,25 +38,100 @@ dies-ok { TestableServer.new(port => $test-port) }, "Must provide host and port
3838 constant $test-password = 'correcthorsebatterystaple';
3939
4040 my $test-server = TestableServer.new(host => $test-host, port => $test-port);
41- my $listen-tap = $test-server.listen().tap(-> $conn { });
41+ my $client-connection;
42+ my $listen-tap = $test-server.listen().tap(-> $conn { $client-connection = $conn });
4243 my $socket-listener = await $test-socket.start-listening;
4344 my $test-conn = $socket-listener.incoming-connection;
4445
4546 $test-conn.receive-data: Stomp::Message.new(
4647 command => 'CONNECT',
4748 headers => (
48- login => $test-login,
49- passcode => $test-password,
50- accept-version => '1.2'
49+ host => 'localhost',
50+ login => $test-login,
51+ passcode => $test-password,
52+ accept-version => '1.2'
5153 ));
5254
5355 my $message-text = await $test-conn.sent-data;
5456 my $parsed-message = Stomp::Parser.parse($message-text);
5557 ok $parsed-message, "Server responded to CONNECT with valid message";
5658 my $message = $parsed-message.made;
5759 is $message.command, "CONNECTED", "Server sent CONNECTED command";
58- ok $message.headers<accept- version>:exists, "Server sent accept- version header";
60+ ok $message.headers<version>:exists, "Server sent version header";
5961 is $message.body, "", "Server sent no message body";
62+ isa-ok $client-connection, 'Stomp::Server::Connection', "and the tap received the correct object";
63+ }
64+
65+ {
66+ constant $test-login = 'user';
67+ constant $test-password = 'correcthorsebatterystaple';
68+
69+ my $test-server = TestableServer.new(host => $test-host, port => $test-port);
70+ my $client-connection;
71+ my $listen-tap = $test-server.listen().tap(-> $conn { $client-connection = $conn });
72+ my $socket-listener = await $test-socket.start-listening;
73+ my $test-conn = $socket-listener.incoming-connection;
74+
75+ $test-conn.receive-data: Stomp::Message.new(
76+ command => 'CONNECT',
77+ headers => (
78+ login => $test-login,
79+ passcode => $test-password,
80+ accept-version => '1.2'
81+ ));
82+
83+ my $message-text = await $test-conn.sent-data;
84+
85+ is $client-connection.subscriptions.elems, 0, "new connection doesn't have a subscription";
86+
87+ my $destination = '/queue/testqueue';
88+ my $id = 1;
89+ $test-conn.receive-data: Stomp::Message.new(
90+ command => 'SUBSCRIBE',
91+ headers => (
92+ :$destination,
93+ :$id,
94+ ));
95+
96+ my $match-message = Stomp::Message.new(
97+ command => 'SEND',
98+ headers => (
99+ :$destination
100+ ),
101+ body => 'Test Message'
102+ );
103+
104+ is $client-connection.subscriptions.elems, 1, "now have one subscription";
105+ is $client-connection.subscriptions.first.id, $id, "and it has the right id";
106+ is $client-connection.subscriptions.first.destination, $destination, "and it has the right destination";
107+ is $client-connection.subscriptions.first.ack, 'auto', "and the ack is 'auto'";
108+ ok $match-message ~~ $client-connection.subscriptions.first , "subscription matches message to that destination";
109+ ok $client-connection.subscription-for-message($match-message), "subscription-for-message";
110+ ok $match-message ~~ $client-connection, "connection matches message to that destination";
111+
112+ my $rec-message;
113+ my $rec-promise = Promise.new;
114+ $client-connection.published-messages.tap({
115+ $rec-message = $_;
116+ $rec-promise.keep: True;
117+ });
118+
119+ $test-conn.receive-data: $match-message;
120+
121+ await Promise.anyof($rec-promise, Promise.in(5));
122+
123+ is $rec-message.body, $match-message.body, "got the message from published-messages";
124+
125+ $test-conn.receive-data: Stomp::Message.new(
126+ command => 'UNSUBSCRIBE',
127+ headers => (
128+ :$destination,
129+ :$id,
130+ ));
131+ is $client-connection.subscriptions.elems, 0, "now have no subscription after unsubscribe";
132+ ok $match-message !~~ $client-connection.subscriptions.first , "subscription no longer matches message to that destination";
133+ ok $match-message !~~ $client-connection, "connection no longer matches message to that destination";
134+
60135}
61136
62137{
0 commit comments