2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Test;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Exceptions\{ OriginNotAllowed, UnknownAppKey, ConnectionsOverCapacity };
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
class ConnectionTest extends TestCase
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_cannot_connect_with_a_wrong_app_key()
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2018-11-26 23:13:22 +00:00
|
|
|
$this->expectException(UnknownAppKey::class);
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->newActiveConnection(['public-channel'], 'NonWorkingKey');
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_unconnected_app_cannot_store_statistics()
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->expectException(UnknownAppKey::class);
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->newActiveConnection(['public-channel'], 'NonWorkingKey');
|
2020-09-03 13:31:19 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->assertCount(0, $this->statisticsCollector->getStatistics());
|
2020-09-03 13:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_origin_validation_should_fail_for_no_origin()
|
2020-09-03 13:31:19 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->expectException(OriginNotAllowed::class);
|
2019-05-11 06:48:33 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = $this->newConnection('TestOrigin');
|
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
|
|
|
}
|
2018-11-25 23:14:50 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_origin_validation_should_fail_for_wrong_origin()
|
2018-11-25 23:14:50 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->expectException(OriginNotAllowed::class);
|
2018-11-25 23:14:50 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = $this->newConnection('TestOrigin', ['Origin' => 'https://google.ro']);
|
2018-11-25 23:14:50 +00:00
|
|
|
|
2018-11-26 22:58:28 +00:00
|
|
|
$this->pusherServer->onOpen($connection);
|
2018-11-25 23:14:50 +00:00
|
|
|
}
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_origin_validation_should_pass_for_the_right_origin()
|
2020-08-18 13:04:52 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = $this->newConnection('TestOrigin', ['Origin' => 'https://test.origin.com']);
|
2020-08-18 13:04:52 +00:00
|
|
|
|
|
|
|
|
$this->pusherServer->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('pusher:connection_established');
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_close_connection()
|
2020-08-18 13:04:52 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalChannels('1234')
|
|
|
|
|
->then(function ($channels) {
|
|
|
|
|
$this->assertCount(1, $channels);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(1, $total);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onClose($connection);
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(0, $total);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalChannels('1234')
|
|
|
|
|
->then(function ($channels) {
|
|
|
|
|
$this->assertCount(0, $channels);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_websocket_exceptions_are_sent()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->pusherServer->onError($connection, new UnknownAppKey('NonWorkingKey'));
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection->assertSentEvent('pusher:error', [
|
|
|
|
|
'data' => [
|
|
|
|
|
'message' => 'Could not find app key `NonWorkingKey`.',
|
|
|
|
|
'code' => 4001,
|
|
|
|
|
],
|
|
|
|
|
]);
|
2020-08-18 13:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_capacity_limit()
|
2020-08-18 13:04:52 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->app['config']->set('websockets.apps.0.capacity', 2);
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->newActiveConnection(['test-channel']);
|
|
|
|
|
$this->newActiveConnection(['test-channel']);
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$failedConnection = $this->newActiveConnection(['test-channel']);
|
|
|
|
|
|
|
|
|
|
$failedConnection
|
|
|
|
|
->assertSentEvent('pusher:error', ['data' => ['message' => 'Over capacity', 'code' => 4100]])
|
|
|
|
|
->assertClosed();
|
2020-08-18 13:04:52 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|