2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests;
|
|
|
|
|
|
|
|
|
|
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
|
2018-11-25 23:28:57 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException;
|
2018-11-25 22:43:43 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\PusherServer;
|
2018-11-25 23:14:50 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
class ConnectionTest extends TestCase
|
|
|
|
|
{
|
2018-11-26 22:58:28 +00:00
|
|
|
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\PusherServer */
|
|
|
|
|
protected $pusherServer;
|
|
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->pusherServer = app(PusherServer::class);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 22:43:43 +00:00
|
|
|
/** @test */
|
|
|
|
|
public function unknown_app_keys_can_not_connect()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(UnknownAppKeyException::class);
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($this->getWebSocketConnection('/?appKey=test'));
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function known_app_keys_can_connect()
|
|
|
|
|
{
|
2018-11-25 23:14:50 +00:00
|
|
|
$connection = $this->getWebSocketConnection();
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher:connection_established');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function successful_connections_have_the_client_attached()
|
|
|
|
|
{
|
2018-11-25 23:14:50 +00:00
|
|
|
$connection = $this->getWebSocketConnection();
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Client::class, $connection->client);
|
|
|
|
|
$this->assertSame(1234, $connection->client->appId);
|
|
|
|
|
$this->assertSame('TestKey', $connection->client->appKey);
|
|
|
|
|
$this->assertSame('TestSecret', $connection->client->appSecret);
|
|
|
|
|
$this->assertSame('Test Client', $connection->client->name);
|
|
|
|
|
}
|
2018-11-25 23:14:50 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function ping_returns_pong()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Message('{"event": "pusher:ping"}');
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:14:50 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2018-11-25 23:14:50 +00:00
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher:pong');
|
|
|
|
|
}
|
2018-11-25 23:20:18 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
2018-11-25 23:20:33 +00:00
|
|
|
public function clients_can_subscribe_to_basic_channels()
|
2018-11-25 23:20:18 +00:00
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'basic-channel'
|
|
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:20:18 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2018-11-25 23:20:18 +00:00
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'basic-channel'
|
|
|
|
|
]);
|
|
|
|
|
}
|
2018-11-25 23:28:57 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function clients_need_valid_auth_signatures_for_private_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(InvalidSignatureException::class);
|
|
|
|
|
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => 'invalid',
|
|
|
|
|
'channel' => 'private-channel'
|
|
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:28:57 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2018-11-25 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function clients_can_subscribe_to_private_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:28:57 +00:00
|
|
|
|
|
|
|
|
$signature = "{$connection->socketId}:private-channel";
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret),
|
|
|
|
|
'channel' => 'private-channel'
|
|
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2018-11-25 23:28:57 +00:00
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'private-channel'
|
|
|
|
|
]);
|
|
|
|
|
}
|
2018-11-25 23:33:07 +00:00
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function clients_need_valid_auth_signatures_for_presence_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(InvalidSignatureException::class);
|
|
|
|
|
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => 'invalid',
|
|
|
|
|
'channel' => 'presence-channel'
|
|
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:33:07 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2018-11-25 23:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
public function clients_can_subscribe_to_presence_channels()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:33:07 +00:00
|
|
|
|
|
|
|
|
$channelData = [
|
|
|
|
|
'user_id' => 1,
|
|
|
|
|
'user_info' => [
|
|
|
|
|
'name' => 'Marcel'
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$signature = "{$connection->socketId}:presence-channel:".json_encode($channelData);
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret),
|
|
|
|
|
'channel' => 'presence-channel',
|
|
|
|
|
'channel_data' => json_encode($channelData)
|
|
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
2018-11-25 23:33:07 +00:00
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher_internal:subscription_succeeded', [
|
|
|
|
|
'channel' => 'presence-channel',
|
|
|
|
|
]);
|
|
|
|
|
}
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|