Fixed tests to make sure the message is broadcasted properly both locally and across servers.

This commit is contained in:
Alex Renoki 2020-09-18 21:01:11 +03:00
parent 546c4fd0ef
commit 2066e803b8
4 changed files with 438 additions and 167 deletions

View File

@ -2,8 +2,12 @@
namespace BeyondCode\LaravelWebSockets\Test;
use BeyondCode\LaravelWebSockets\API\TriggerEvent;
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
use Carbon\Carbon;
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\JsonResponse;
use Pusher\Pusher;
use Ratchet\ConnectionInterface;
class PresenceChannelTest extends TestCase
@ -455,4 +459,146 @@ class PresenceChannelTest extends TestCase
$message->getPayload(),
]);
}
public function test_it_fires_the_event_to_presence_channel()
{
$this->newPresenceConnection('presence-channel');
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['presence-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
$this->statisticsCollector
->getAppStatistics('1234')
->then(function ($statistic) {
$this->assertEquals([
'peak_connections_count' => 1,
'websocket_messages_count' => 1,
'api_messages_count' => 1,
'app_id' => '1234',
], $statistic->toArray());
});
}
public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_presence_channel()
{
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['presence-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgsCount(1, 'publish', [
$this->channelManager->getRedisKey('1234', 'presence-channel'),
json_encode([
'event' => 'some-event',
'channel' => 'presence-channel',
'data' => json_encode(['some-data' => 'yes']),
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
}
public function test_it_fires_event_across_servers_when_there_are_users_locally_for_presence_channel()
{
$wsConnection = $this->newPresenceConnection('presence-channel');
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['presence-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgsCount(1, 'publish', [
$this->channelManager->getRedisKey('1234', 'presence-channel'),
json_encode([
'event' => 'some-event',
'channel' => 'presence-channel',
'data' => json_encode(['some-data' => 'yes']),
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
$wsConnection->assertSentEvent('some-event', [
'channel' => 'presence-channel',
'data' => json_encode(['some-data' => 'yes']),
]);
}
}

View File

@ -2,8 +2,12 @@
namespace BeyondCode\LaravelWebSockets\Test;
use BeyondCode\LaravelWebSockets\API\TriggerEvent;
use BeyondCode\LaravelWebSockets\Server\Exceptions\InvalidSignature;
use Carbon\Carbon;
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\JsonResponse;
use Pusher\Pusher;
use Ratchet\ConnectionInterface;
class PrivateChannelTest extends TestCase
@ -263,4 +267,146 @@ class PrivateChannelTest extends TestCase
$message->getPayload(),
]);
}
public function test_it_fires_the_event_to_private_channel()
{
$this->newPrivateConnection('private-channel');
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['private-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
$this->statisticsCollector
->getAppStatistics('1234')
->then(function ($statistic) {
$this->assertEquals([
'peak_connections_count' => 1,
'websocket_messages_count' => 1,
'api_messages_count' => 1,
'app_id' => '1234',
], $statistic->toArray());
});
}
public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_private_channel()
{
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['private-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgsCount(1, 'publish', [
$this->channelManager->getRedisKey('1234', 'private-channel'),
json_encode([
'event' => 'some-event',
'channel' => 'private-channel',
'data' => json_encode(['some-data' => 'yes']),
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
}
public function test_it_fires_event_across_servers_when_there_are_users_locally_for_private_channel()
{
$wsConnection = $this->newPrivateConnection('private-channel');
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['private-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgsCount(1, 'publish', [
$this->channelManager->getRedisKey('1234', 'private-channel'),
json_encode([
'event' => 'some-event',
'channel' => 'private-channel',
'data' => json_encode(['some-data' => 'yes']),
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
$wsConnection->assertSentEvent('some-event', [
'channel' => 'private-channel',
'data' => json_encode(['some-data' => 'yes']),
]);
}
}

View File

@ -2,7 +2,11 @@
namespace BeyondCode\LaravelWebSockets\Test;
use BeyondCode\LaravelWebSockets\API\TriggerEvent;
use Carbon\Carbon;
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\JsonResponse;
use Pusher\Pusher;
use Ratchet\ConnectionInterface;
class PublicChannelTest extends TestCase
@ -244,4 +248,146 @@ class PublicChannelTest extends TestCase
$message->getPayload(),
]);
}
public function test_it_fires_the_event_to_public_channel()
{
$this->newActiveConnection(['public-channel']);
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['public-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
$this->statisticsCollector
->getAppStatistics('1234')
->then(function ($statistic) {
$this->assertEquals([
'peak_connections_count' => 1,
'websocket_messages_count' => 1,
'api_messages_count' => 1,
'app_id' => '1234',
], $statistic->toArray());
});
}
public function test_it_fires_event_across_servers_when_there_are_not_users_locally_for_public_channel()
{
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['public-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgsCount(1, 'publish', [
$this->channelManager->getRedisKey('1234', 'public-channel'),
json_encode([
'event' => 'some-event',
'channel' => 'public-channel',
'data' => json_encode(['some-data' => 'yes']),
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
}
public function test_it_fires_event_across_servers_when_there_are_users_locally_for_public_channel()
{
$wsConnection = $this->newActiveConnection(['public-channel']);
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'POST', $requestPath, [
'name' => 'some-event',
'channels' => ['public-channel'],
'data' => json_encode(['some-data' => 'yes']),
],
);
$request = new Request('POST', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgsCount(1, 'publish', [
$this->channelManager->getRedisKey('1234', 'public-channel'),
json_encode([
'event' => 'some-event',
'channel' => 'public-channel',
'data' => json_encode(['some-data' => 'yes']),
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
$wsConnection->assertSentEvent('some-event', [
'channel' => 'public-channel',
'data' => json_encode(['some-data' => 'yes']),
]);
}
}

View File

@ -33,171 +33,4 @@ class TriggerEventTest extends TestCase
$controller->onOpen($connection, $request);
}
public function test_it_fires_the_event_to_public_channel()
{
$this->newActiveConnection(['public-channel']);
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'GET', $requestPath, [
'channels' => 'public-channel',
],
);
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
$this->statisticsCollector
->getAppStatistics('1234')
->then(function ($statistic) {
$this->assertEquals([
'peak_connections_count' => 1,
'websocket_messages_count' => 1,
'api_messages_count' => 1,
'app_id' => '1234',
], $statistic->toArray());
});
}
public function test_it_fires_the_event_to_presence_channel()
{
$this->newPresenceConnection('presence-channel');
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'GET', $requestPath, [
'channels' => 'presence-channel',
],
);
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
$this->statisticsCollector
->getAppStatistics('1234')
->then(function ($statistic) {
$this->assertEquals([
'peak_connections_count' => 1,
'websocket_messages_count' => 1,
'api_messages_count' => 1,
'app_id' => '1234',
], $statistic->toArray());
});
}
public function test_it_fires_the_event_to_private_channel()
{
$this->newPresenceConnection('private-channel');
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'GET', $requestPath, [
'channels' => 'private-channel',
],
);
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
$this->statisticsCollector
->getAppStatistics('1234')
->then(function ($statistic) {
$this->assertEquals([
'peak_connections_count' => 1,
'websocket_messages_count' => 1,
'api_messages_count' => 1,
'app_id' => '1234',
], $statistic->toArray());
});
}
public function test_it_fires_event_across_servers()
{
$connection = new Mocks\Connection;
$requestPath = '/apps/1234/events';
$routeParams = [
'appId' => '1234',
];
$queryString = Pusher::build_auth_query_string(
'TestKey', 'TestSecret', 'GET', $requestPath, [
'channels' => 'public-channel',
],
);
$request = new Request('GET', "{$requestPath}?{$queryString}&".http_build_query($routeParams));
$controller = app(TriggerEvent::class);
$controller->onOpen($connection, $request);
/** @var JsonResponse $response */
$response = array_pop($connection->sentRawData);
$this->assertSame([], json_decode($response->getContent(), true));
if (method_exists($this->channelManager, 'getPublishClient')) {
$this->channelManager
->getPublishClient()
->assertCalledWithArgs('publish', [
$this->channelManager->getRedisKey('1234', 'public-channel'),
json_encode([
'event' => null,
'channel' => 'public-channel',
'data' => null,
'appId' => '1234',
'socketId' => null,
'serverId' => $this->channelManager->getServerId(),
]),
]);
}
}
}