2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
namespace BlaxSoftware\LaravelWebSockets\Test;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Exceptions\UnknownAppKey;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
class ConnectionTest extends TestCase
|
|
|
|
|
{
|
2026-01-24 12:34:29 +00:00
|
|
|
/**
|
|
|
|
|
* @group integration
|
|
|
|
|
* @group requires-server
|
|
|
|
|
*/
|
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
|
|
|
{
|
2026-01-24 12:34:29 +00:00
|
|
|
$this->skipIfServerUnavailable();
|
2022-10-06 11:46:54 +00:00
|
|
|
$this->startServer();
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$response = $this->await($this->joinWebSocketServer(['public-channel'], 'NonWorkingKey'));
|
2025-01-18 16:06:52 +00:00
|
|
|
$this->assertSame('{"event":"pusher.error","data":{"message":"Could not find app key `NonWorkingKey`.","code":4001}}', (string) $response);
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-24 12:34:29 +00:00
|
|
|
/**
|
|
|
|
|
* @group integration
|
|
|
|
|
* @group requires-server
|
|
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
public function test_unconnected_app_cannot_store_statistics()
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2026-01-24 12:34:29 +00:00
|
|
|
$this->skipIfServerUnavailable();
|
2022-10-06 11:46:54 +00:00
|
|
|
$this->startServer();
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$response = $this->await($this->joinWebSocketServer(['public-channel'], 'NonWorkingKey'));
|
2025-01-18 16:06:52 +00:00
|
|
|
$this->assertSame('{"event":"pusher.error","data":{"message":"Could not find app key `NonWorkingKey`.","code":4001}}', (string) $response);
|
2020-09-03 13:31:19 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$count = $this->await($this->statisticsCollector->getStatistics());
|
|
|
|
|
$this->assertCount(0, $count);
|
2020-09-03 13:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-24 12:34:29 +00:00
|
|
|
/**
|
|
|
|
|
* @group integration
|
|
|
|
|
* @group requires-server
|
|
|
|
|
*/
|
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
|
|
|
{
|
2026-01-24 12:34:29 +00:00
|
|
|
$this->skipIfServerUnavailable();
|
2022-10-06 11:46:54 +00:00
|
|
|
$this->startServer();
|
2019-05-11 06:48:33 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$response = $this->await($this->joinWebSocketServer(['public-channel'], 'TestOrigin'));
|
2025-01-18 16:06:52 +00:00
|
|
|
$this->assertSame('{"event":"pusher.error","data":{"message":"The origin is not allowed for `TestOrigin`.","code":4009}}', (string) $response);
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
2018-11-25 23:14:50 +00:00
|
|
|
|
2026-01-24 12:34:29 +00:00
|
|
|
/**
|
|
|
|
|
* @group integration
|
|
|
|
|
* @group requires-server
|
|
|
|
|
*/
|
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
|
|
|
{
|
2026-01-24 12:34:29 +00:00
|
|
|
$this->skipIfServerUnavailable();
|
2022-10-06 11:46:54 +00:00
|
|
|
$this->startServer();
|
2018-11-25 23:14:50 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$response = $this->await($this->joinWebSocketServer(['public-channel'], 'TestOrigin', ['Origin' => 'https://google.ro']));
|
2025-01-18 16:06:52 +00:00
|
|
|
$this->assertSame('{"event":"pusher.error","data":{"message":"The origin is not allowed for `TestOrigin`.","code":4009}}', (string) $response);
|
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);
|
|
|
|
|
|
2025-01-18 16:06:52 +00:00
|
|
|
$connection->assertSentEvent('pusher.connection_established');
|
2020-08-18 13:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
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']);
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalChannels('1234')
|
|
|
|
|
->then(function ($channels) {
|
|
|
|
|
$this->assertCount(1, $channels);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(1, $total);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
$this->pusherServer->onClose($connection);
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(0, $total);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalChannels('1234')
|
|
|
|
|
->then(function ($channels) {
|
|
|
|
|
$this->assertCount(0, $channels);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
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
|
|
|
|
2025-01-18 16:06:52 +00:00
|
|
|
$connection->assertSentEvent('pusher.error', [
|
2020-09-10 19:59:26 +00:00
|
|
|
'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
|
2025-01-18 16:06:52 +00:00
|
|
|
->assertSentEvent('pusher.error', ['data' => ['message' => 'Over capacity', 'code' => 4100]])
|
2020-09-10 19:59:26 +00:00
|
|
|
->assertClosed();
|
2020-08-18 13:04:52 +00:00
|
|
|
}
|
2020-09-12 14:45:07 +00:00
|
|
|
|
|
|
|
|
public function test_close_all_new_connections_after_stating_the_server_does_not_accept_new_connections()
|
|
|
|
|
{
|
2020-09-14 10:25:21 +00:00
|
|
|
$this->newActiveConnection(['test-channel'])
|
2025-01-18 16:06:52 +00:00
|
|
|
->assertSentEvent('pusher.connection_established')
|
2020-09-12 14:45:07 +00:00
|
|
|
->assertSentEvent('pusher_internal:subscription_succeeded');
|
|
|
|
|
|
|
|
|
|
$this->channelManager->declineNewConnections();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
$this->channelManager->acceptsNewConnections()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->newActiveConnection(['test-channel'])
|
|
|
|
|
->assertNothingSent()
|
|
|
|
|
->assertClosed();
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|