laravel-websockets/tests/LegacyWebSocketHandlerTest.php

78 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace BlaxSoftware\LaravelWebSockets\Test;
use BlaxSoftware\LaravelWebSockets\Server\WebSocketHandler;
use GuzzleHttp\Psr7\Request;
/**
* The legacy Server\WebSocketHandler can still be selected through
* websockets.handlers.websocket. A connection it refuses must be closed.
*/
class LegacyWebSocketHandlerTest extends TestCase
{
private WebSocketHandler $legacy;
public function setUp(): void
{
parent::setUp();
$this->legacy = new WebSocketHandler($this->channelManager);
}
public function test_unknown_app_key_gets_4001_and_is_closed()
{
$connection = $this->newConnection('NonWorkingKey');
$this->legacy->onOpen($connection);
$connection
->assertSentEvent('websocket.error', [
'data' => ['message' => 'Could not find app key `NonWorkingKey`.', 'code' => 4001],
])
->assertNotSentEvent('websocket.connection_established')
->assertClosed();
$this->assertSame(4001, $connection->closeCode);
$this->assertFalse(isset($connection->app));
}
public function test_array_app_key_gets_4001_and_is_closed()
{
$connection = new Mocks\Connection;
$connection->httpRequest = new Request('GET', '/?appKey[]=x');
$this->legacy->onOpen($connection);
$connection
->assertSentEvent('websocket.error', ['data' => ['message' => 'Could not find app key ``.', 'code' => 4001]])
->assertClosed();
$this->assertSame(4001, $connection->closeCode);
}
public function test_disallowed_origin_gets_4009_and_is_closed()
{
$connection = $this->newConnection('TestOrigin', ['Origin' => 'https://google.ro']);
$this->legacy->onOpen($connection);
$connection
->assertSentEvent('websocket.error', [
'data' => ['message' => 'The origin is not allowed for `TestOrigin`.', 'code' => 4009],
])
->assertNotSentEvent('websocket.connection_established')
->assertClosed();
$this->assertSame(4009, $connection->closeCode);
}
public function test_known_app_key_is_established_and_stays_open()
{
$connection = $this->newConnection();
$this->legacy->onOpen($connection);
$connection->assertSentEvent('websocket.connection_established');
$this->assertFalse($connection->closed);
$this->assertSame('TestKey', $connection->app->key);
}
}