Creating SignedMessage class for testing
This commit is contained in:
parent
df45ee89ff
commit
b41f8b7b75
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebSockets\Test\Mocks;
|
||||||
|
|
||||||
|
use Ratchet\ConnectionInterface;
|
||||||
|
|
||||||
|
class SignedMessage extends Message
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a new signed message instance.
|
||||||
|
*
|
||||||
|
* @param array $payload
|
||||||
|
* @param ConnectionInterface $connection
|
||||||
|
* @param string $channelName
|
||||||
|
* @param string|null $encodedUser
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(array $payload, ConnectionInterface $connection, string $channelName, string $encodedUser = null)
|
||||||
|
{
|
||||||
|
parent::__construct($payload);
|
||||||
|
|
||||||
|
$signature = "{$connection->socketId}:{$channelName}";
|
||||||
|
|
||||||
|
if ($encodedUser) {
|
||||||
|
$signature .= ":{$encodedUser}";
|
||||||
|
}
|
||||||
|
|
||||||
|
$hash = hash_hmac('sha256', $signature, $connection->app->secret);
|
||||||
|
|
||||||
|
$this->payload['data']['auth'] = "{$connection->app->key}:{$hash}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -40,17 +40,13 @@ class PresenceChannelTest extends TestCase
|
||||||
|
|
||||||
$encodedUser = json_encode($user);
|
$encodedUser = json_encode($user);
|
||||||
|
|
||||||
$signature = "{$connection->socketId}:presence-channel:".$encodedUser;
|
$message = new Mocks\SignedMessage([
|
||||||
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret);
|
|
||||||
|
|
||||||
$message = new Mocks\Message([
|
|
||||||
'event' => 'pusher:subscribe',
|
'event' => 'pusher:subscribe',
|
||||||
'data' => [
|
'data' => [
|
||||||
'auth' => "{$connection->app->key}:{$hashedAppSecret}",
|
|
||||||
'channel' => 'presence-channel',
|
'channel' => 'presence-channel',
|
||||||
'channel_data' => json_encode($user),
|
'channel_data' => $encodedUser,
|
||||||
],
|
],
|
||||||
]);
|
], $connection, 'presence-channel', $encodedUser);
|
||||||
|
|
||||||
$this->pusherServer->onMessage($connection, $message);
|
$this->pusherServer->onMessage($connection, $message);
|
||||||
|
|
||||||
|
|
@ -187,7 +183,7 @@ class PresenceChannelTest extends TestCase
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_local_connections_for_private_channels()
|
public function test_local_connections_for_presence_channels()
|
||||||
{
|
{
|
||||||
$this->newPresenceConnection('presence-channel', ['user_id' => 1]);
|
$this->newPresenceConnection('presence-channel', ['user_id' => 1]);
|
||||||
$this->newPresenceConnection('presence-channel-2', ['user_id' => 2]);
|
$this->newPresenceConnection('presence-channel-2', ['user_id' => 2]);
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,32 @@ namespace BeyondCode\LaravelWebSockets\Test;
|
||||||
|
|
||||||
class ReplicationTest extends TestCase
|
class ReplicationTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->runOnlyOnRedisReplication();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_publishing_client_gets_subscribed()
|
||||||
|
{
|
||||||
|
$this->newActiveConnection(['public-channel']);
|
||||||
|
|
||||||
|
$this->getSubscribeClient()
|
||||||
|
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234')])
|
||||||
|
->assertCalledWithArgs('subscribe', [$this->channelManager->getRedisKey('1234', 'public-channel')]);
|
||||||
|
}
|
||||||
|
|
||||||
public function test_events_get_replicated_across_connections()
|
public function test_events_get_replicated_across_connections()
|
||||||
{
|
{
|
||||||
$this->runOnlyOnRedisReplication();
|
|
||||||
|
|
||||||
$connection = $this->newActiveConnection(['public-channel']);
|
$connection = $this->newActiveConnection(['public-channel']);
|
||||||
|
|
||||||
$message = [
|
$message = [
|
||||||
'appId' => '1234',
|
'appId' => '1234',
|
||||||
'serverId' => 0,
|
'serverId' => $this->channelManager->getServerId(),
|
||||||
'event' => 'some-event',
|
'event' => 'some-event',
|
||||||
'data' => [
|
'data' => [
|
||||||
'channel' => 'public-channel',
|
'channel' => 'public-channel',
|
||||||
|
|
@ -31,12 +48,19 @@ class ReplicationTest extends TestCase
|
||||||
'serverId' => $this->channelManager->getServerId(),
|
'serverId' => $this->channelManager->getServerId(),
|
||||||
'data' => ['channel' => 'public-channel', 'test' => 'yes'],
|
'data' => ['channel' => 'public-channel', 'test' => 'yes'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->getSubscribeClient()
|
||||||
|
->assertNothingDispatched();
|
||||||
|
|
||||||
|
$this->getPublishClient()
|
||||||
|
->assertCalledWithArgs('publish', [
|
||||||
|
$this->channelManager->getRedisKey('1234', 'public-channel'),
|
||||||
|
json_encode($message),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_not_ponged_connections_do_get_removed_for_public_channels()
|
public function test_not_ponged_connections_do_get_removed_for_public_channels()
|
||||||
{
|
{
|
||||||
$this->runOnlyOnRedisReplication();
|
|
||||||
|
|
||||||
$connection = $this->newActiveConnection(['public-channel']);
|
$connection = $this->newActiveConnection(['public-channel']);
|
||||||
|
|
||||||
// Make the connection look like it was lost 1 day ago.
|
// Make the connection look like it was lost 1 day ago.
|
||||||
|
|
@ -65,8 +89,6 @@ class ReplicationTest extends TestCase
|
||||||
|
|
||||||
public function test_not_ponged_connections_do_get_removed_for_private_channels()
|
public function test_not_ponged_connections_do_get_removed_for_private_channels()
|
||||||
{
|
{
|
||||||
$this->runOnlyOnRedisReplication();
|
|
||||||
|
|
||||||
$connection = $this->newPrivateConnection('private-channel');
|
$connection = $this->newPrivateConnection('private-channel');
|
||||||
|
|
||||||
// Make the connection look like it was lost 1 day ago.
|
// Make the connection look like it was lost 1 day ago.
|
||||||
|
|
@ -95,8 +117,6 @@ class ReplicationTest extends TestCase
|
||||||
|
|
||||||
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
|
public function test_not_ponged_connections_do_get_removed_for_presence_channels()
|
||||||
{
|
{
|
||||||
$this->runOnlyOnRedisReplication();
|
|
||||||
|
|
||||||
$connection = $this->newPresenceConnection('presence-channel');
|
$connection = $this->newPresenceConnection('presence-channel');
|
||||||
|
|
||||||
// Make the connection look like it was lost 1 day ago.
|
// Make the connection look like it was lost 1 day ago.
|
||||||
|
|
|
||||||
|
|
@ -331,18 +331,15 @@ abstract class TestCase extends Orchestra
|
||||||
'user_info' => ['name' => 'Rick'],
|
'user_info' => ['name' => 'Rick'],
|
||||||
];
|
];
|
||||||
|
|
||||||
$signature = "{$connection->socketId}:{$channel}:".json_encode($user);
|
$encodedUser = json_encode($user);
|
||||||
|
|
||||||
$hash = hash_hmac('sha256', $signature, $connection->app->secret);
|
$message = new Mocks\SignedMessage([
|
||||||
|
|
||||||
$message = new Mocks\Message([
|
|
||||||
'event' => 'pusher:subscribe',
|
'event' => 'pusher:subscribe',
|
||||||
'data' => [
|
'data' => [
|
||||||
'auth' => "{$connection->app->key}:{$hash}",
|
|
||||||
'channel' => $channel,
|
'channel' => $channel,
|
||||||
'channel_data' => json_encode($user),
|
'channel_data' => $encodedUser,
|
||||||
],
|
],
|
||||||
]);
|
], $connection, $channel, $encodedUser);
|
||||||
|
|
||||||
$this->pusherServer->onMessage($connection, $message);
|
$this->pusherServer->onMessage($connection, $message);
|
||||||
|
|
||||||
|
|
@ -363,17 +360,12 @@ abstract class TestCase extends Orchestra
|
||||||
|
|
||||||
$this->pusherServer->onOpen($connection);
|
$this->pusherServer->onOpen($connection);
|
||||||
|
|
||||||
$signature = "{$connection->socketId}:{$channel}";
|
$message = new Mocks\SignedMessage([
|
||||||
|
|
||||||
$hash = hash_hmac('sha256', $signature, $connection->app->secret);
|
|
||||||
|
|
||||||
$message = new Mocks\Message([
|
|
||||||
'event' => 'pusher:subscribe',
|
'event' => 'pusher:subscribe',
|
||||||
'data' => [
|
'data' => [
|
||||||
'auth' => "{$connection->app->key}:{$hash}",
|
|
||||||
'channel' => $channel,
|
'channel' => $channel,
|
||||||
],
|
],
|
||||||
]);
|
], $connection, $channel);
|
||||||
|
|
||||||
$this->pusherServer->onMessage($connection, $message);
|
$this->pusherServer->onMessage($connection, $message);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue