laravel-websockets/tests/ConnectionTest.php

127 lines
3.9 KiB
PHP
Raw Normal View History

2018-11-25 22:43:43 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
2018-12-01 12:57:02 +00:00
use BeyondCode\LaravelWebSockets\Apps\App;
2018-11-25 23:14:50 +00:00
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\ConnectionsOverCapacity;
2020-08-18 13:04:52 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\OriginNotAllowed;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
2020-09-04 06:47:23 +00:00
use Illuminate\Support\Facades\Redis;
2018-11-25 22:43:43 +00:00
class ConnectionTest extends TestCase
{
/** @test */
public function unknown_app_keys_can_not_connect()
{
2018-11-26 23:13:22 +00:00
$this->expectException(UnknownAppKey::class);
2018-11-25 22:43:43 +00:00
2020-08-18 13:04:52 +00:00
$this->pusherServer->onOpen($this->getWebSocketConnection('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 app_can_not_exceed_maximum_capacity()
{
2020-09-03 13:31:19 +00:00
$this->runOnlyOnLocalReplication();
$this->app['config']->set('websockets.apps.0.capacity', 2);
$this->getConnectedWebSocketConnection(['test-channel']);
$this->getConnectedWebSocketConnection(['test-channel']);
$this->expectException(ConnectionsOverCapacity::class);
$this->getConnectedWebSocketConnection(['test-channel']);
}
/** @test */
public function app_can_not_exceed_maximum_capacity_on_redis_replication()
{
$this->runOnlyOnRedisReplication();
2020-09-04 06:47:23 +00:00
Redis::hdel('laravel_database_1234', 'connections');
2020-09-03 13:31:19 +00:00
$this->app['config']->set('websockets.apps.0.capacity', 2);
$this->getConnectedWebSocketConnection(['test-channel']);
$this->getConnectedWebSocketConnection(['test-channel']);
2020-09-04 06:47:23 +00:00
$this->getPublishClient()
->assertCalledWithArgsCount(2, 'hincrby', ['laravel_database_1234', 'connections', 1]);
$this->expectException(ConnectionsOverCapacity::class);
2020-09-04 06:47:23 +00:00
$this->getConnectedWebSocketConnection(['test-channel']);
}
2018-11-25 22:43:43 +00:00
/** @test */
2018-12-01 13:12:15 +00:00
public function successful_connections_have_the_app_attached()
2018-11-25 22:43:43 +00:00
{
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
2018-12-01 13:12:15 +00:00
$this->assertInstanceOf(App::class, $connection->app);
$this->assertSame('1234', $connection->app->id);
2018-12-01 13:12:15 +00:00
$this->assertSame('TestKey', $connection->app->key);
$this->assertSame('TestSecret', $connection->app->secret);
$this->assertSame('Test App', $connection->app->name);
2018-11-25 22:43:43 +00:00
}
2018-11-25 23:14:50 +00:00
/** @test */
public function ping_returns_pong()
{
$connection = $this->getWebSocketConnection();
2020-08-22 17:53:33 +00:00
$message = new Message(['event' => 'pusher:ping']);
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
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');
}
2020-08-18 13:04:52 +00:00
/** @test */
public function origin_validation_should_fail_for_no_origin()
{
$this->expectException(OriginNotAllowed::class);
$connection = $this->getWebSocketConnection('TestOrigin');
$this->pusherServer->onOpen($connection);
$connection->assertSentEvent('pusher:connection_established');
}
/** @test */
public function origin_validation_should_fail_for_wrong_origin()
{
$this->expectException(OriginNotAllowed::class);
$connection = $this->getWebSocketConnection('TestOrigin', ['Origin' => 'https://google.ro']);
$this->pusherServer->onOpen($connection);
$connection->assertSentEvent('pusher:connection_established');
}
/** @test */
public function origin_validation_should_pass_for_the_right_origin()
{
$connection = $this->getWebSocketConnection('TestOrigin', ['Origin' => 'https://test.origin.com']);
$this->pusherServer->onOpen($connection);
$connection->assertSentEvent('pusher:connection_established');
}
2018-12-04 21:22:33 +00:00
}