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
|
|
|
|
BF refuse a broken app key or failed app lookup without stopping the server
Ratchet's router merges the query over the route parameters, so
/app/<anything>?appKey[]=x reached verifyAppKey() (and WebSocketsLogger's
onOpen when logging is on) with an array. QueryParameters::get(): string
threw a TypeError, onOpen only caught WebSocketException and Ratchet's
IoServer only catches \Exception, so the Error left the event loop and the
daemon exited. Anyone could do this before authenticating.
- QueryParameters::get() reads a non-string parameter as '', so the key
becomes an unknown key: websocket.error 4001 and a closed socket.
- onOpen catches any other Throwable, logs it with the trace, sends
websocket.error "Internal server error" (1011) and closes with 1011.
- verifyAppKey() settles the findByKey() promise in place instead of
await(). await() on a pending promise would run the server's own loop
again from a fiber inside onOpen. A rejected lookup is rethrown, a
pending one is refused; the AppManager contract now says findByKey()
must settle synchronously. ConfigAppManager already does.
- A refused connection (4001 unknown key, 4009 origin) closes with that
code instead of 1000. 4000-4099 tells Pusher-style clients not to
reconnect. ConnectionLogger::close() passes the code on.
- The refusal is logged at warning level without a trace, and the
client-supplied key goes into the JSON-encoded context, not the log line.
Tests: the array key with and without the logger, QueryParameters on an
array, an app manager that rejects, one that never settles and one that
throws an Error, the close codes of the 4001/4009 refusals, and the server
test now checks the 4001 close code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:25:54 +00:00
|
|
|
use BlaxSoftware\LaravelWebSockets\Apps\ConfigAppManager;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Contracts\AppManager;
|
2025-01-16 07:54:02 +00:00
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Exceptions\UnknownAppKey;
|
BF refuse a broken app key or failed app lookup without stopping the server
Ratchet's router merges the query over the route parameters, so
/app/<anything>?appKey[]=x reached verifyAppKey() (and WebSocketsLogger's
onOpen when logging is on) with an array. QueryParameters::get(): string
threw a TypeError, onOpen only caught WebSocketException and Ratchet's
IoServer only catches \Exception, so the Error left the event loop and the
daemon exited. Anyone could do this before authenticating.
- QueryParameters::get() reads a non-string parameter as '', so the key
becomes an unknown key: websocket.error 4001 and a closed socket.
- onOpen catches any other Throwable, logs it with the trace, sends
websocket.error "Internal server error" (1011) and closes with 1011.
- verifyAppKey() settles the findByKey() promise in place instead of
await(). await() on a pending promise would run the server's own loop
again from a fiber inside onOpen. A rejected lookup is rethrown, a
pending one is refused; the AppManager contract now says findByKey()
must settle synchronously. ConfigAppManager already does.
- A refused connection (4001 unknown key, 4009 origin) closes with that
code instead of 1000. 4000-4099 tells Pusher-style clients not to
reconnect. ConnectionLogger::close() passes the code on.
- The refusal is logged at warning level without a trace, and the
client-supplied key goes into the JSON-encoded context, not the log line.
Tests: the array key with and without the logger, QueryParameters on an
array, an app manager that rejects, one that never settles and one that
throws an Error, the close codes of the 4001/4009 refusals, and the server
test now checks the 4001 close code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:25:54 +00:00
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Loggers\ConnectionLogger;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Loggers\WebSocketsLogger;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\QueryParameters;
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use React\Promise\Deferred;
|
|
|
|
|
use React\Promise\PromiseInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\BufferedOutput;
|
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
|
|
|
|
2026-11-04 10:56:48 +00:00
|
|
|
$messages = [];
|
|
|
|
|
$closed = new \React\Promise\Deferred();
|
|
|
|
|
|
|
|
|
|
\Ratchet\Client\connect('ws://localhost:4000/app/NonWorkingKey', [], [], $this->loop)
|
|
|
|
|
->then(function ($conn) use (&$messages, $closed) {
|
|
|
|
|
$conn->on('message', function ($msg) use (&$messages) {
|
|
|
|
|
$messages[] = (string) $msg;
|
|
|
|
|
});
|
|
|
|
|
$conn->on('close', function ($code) use ($closed) {
|
|
|
|
|
$closed->resolve($code);
|
|
|
|
|
});
|
|
|
|
|
}, function ($e) use ($closed) {
|
|
|
|
|
$closed->reject($e);
|
|
|
|
|
});
|
|
|
|
|
|
BF refuse a broken app key or failed app lookup without stopping the server
Ratchet's router merges the query over the route parameters, so
/app/<anything>?appKey[]=x reached verifyAppKey() (and WebSocketsLogger's
onOpen when logging is on) with an array. QueryParameters::get(): string
threw a TypeError, onOpen only caught WebSocketException and Ratchet's
IoServer only catches \Exception, so the Error left the event loop and the
daemon exited. Anyone could do this before authenticating.
- QueryParameters::get() reads a non-string parameter as '', so the key
becomes an unknown key: websocket.error 4001 and a closed socket.
- onOpen catches any other Throwable, logs it with the trace, sends
websocket.error "Internal server error" (1011) and closes with 1011.
- verifyAppKey() settles the findByKey() promise in place instead of
await(). await() on a pending promise would run the server's own loop
again from a fiber inside onOpen. A rejected lookup is rethrown, a
pending one is refused; the AppManager contract now says findByKey()
must settle synchronously. ConfigAppManager already does.
- A refused connection (4001 unknown key, 4009 origin) closes with that
code instead of 1000. 4000-4099 tells Pusher-style clients not to
reconnect. ConnectionLogger::close() passes the code on.
- The refusal is logged at warning level without a trace, and the
client-supplied key goes into the JSON-encoded context, not the log line.
Tests: the array key with and without the logger, QueryParameters on an
array, an app manager that rejects, one that never settles and one that
throws an Error, the close codes of the 4001/4009 refusals, and the server
test now checks the 4001 close code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:25:54 +00:00
|
|
|
// The server closes the socket after the error frame, with the same
|
|
|
|
|
// code so Pusher-style clients do not reconnect.
|
|
|
|
|
$code = $this->await($closed->promise());
|
2026-11-04 10:56:48 +00:00
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
['{"event":"websocket.error","data":{"message":"Could not find app key `NonWorkingKey`.","code":4001}}'],
|
|
|
|
|
$messages
|
|
|
|
|
);
|
BF refuse a broken app key or failed app lookup without stopping the server
Ratchet's router merges the query over the route parameters, so
/app/<anything>?appKey[]=x reached verifyAppKey() (and WebSocketsLogger's
onOpen when logging is on) with an array. QueryParameters::get(): string
threw a TypeError, onOpen only caught WebSocketException and Ratchet's
IoServer only catches \Exception, so the Error left the event loop and the
daemon exited. Anyone could do this before authenticating.
- QueryParameters::get() reads a non-string parameter as '', so the key
becomes an unknown key: websocket.error 4001 and a closed socket.
- onOpen catches any other Throwable, logs it with the trace, sends
websocket.error "Internal server error" (1011) and closes with 1011.
- verifyAppKey() settles the findByKey() promise in place instead of
await(). await() on a pending promise would run the server's own loop
again from a fiber inside onOpen. A rejected lookup is rethrown, a
pending one is refused; the AppManager contract now says findByKey()
must settle synchronously. ConfigAppManager already does.
- A refused connection (4001 unknown key, 4009 origin) closes with that
code instead of 1000. 4000-4099 tells Pusher-style clients not to
reconnect. ConnectionLogger::close() passes the code on.
- The refusal is logged at warning level without a trace, and the
client-supplied key goes into the JSON-encoded context, not the log line.
Tests: the array key with and without the logger, QueryParameters on an
array, an app manager that rejects, one that never settles and one that
throws an Error, the close codes of the 4001/4009 refusals, and the server
test now checks the 4001 close code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:25:54 +00:00
|
|
|
$this->assertSame(4001, $code);
|
2026-11-04 10:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_unknown_app_key_gets_4001_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newConnection('NonWorkingKey');
|
|
|
|
|
|
|
|
|
|
$this->wsHandler->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$connection
|
|
|
|
|
->assertSentEvent('websocket.error', [
|
|
|
|
|
'data' => [
|
|
|
|
|
'message' => 'Could not find app key `NonWorkingKey`.',
|
|
|
|
|
'code' => 4001,
|
|
|
|
|
],
|
|
|
|
|
])
|
|
|
|
|
->assertNotSentEvent('websocket.connection_established')
|
|
|
|
|
->assertClosed();
|
BF refuse a broken app key or failed app lookup without stopping the server
Ratchet's router merges the query over the route parameters, so
/app/<anything>?appKey[]=x reached verifyAppKey() (and WebSocketsLogger's
onOpen when logging is on) with an array. QueryParameters::get(): string
threw a TypeError, onOpen only caught WebSocketException and Ratchet's
IoServer only catches \Exception, so the Error left the event loop and the
daemon exited. Anyone could do this before authenticating.
- QueryParameters::get() reads a non-string parameter as '', so the key
becomes an unknown key: websocket.error 4001 and a closed socket.
- onOpen catches any other Throwable, logs it with the trace, sends
websocket.error "Internal server error" (1011) and closes with 1011.
- verifyAppKey() settles the findByKey() promise in place instead of
await(). await() on a pending promise would run the server's own loop
again from a fiber inside onOpen. A rejected lookup is rethrown, a
pending one is refused; the AppManager contract now says findByKey()
must settle synchronously. ConfigAppManager already does.
- A refused connection (4001 unknown key, 4009 origin) closes with that
code instead of 1000. 4000-4099 tells Pusher-style clients not to
reconnect. ConnectionLogger::close() passes the code on.
- The refusal is logged at warning level without a trace, and the
client-supplied key goes into the JSON-encoded context, not the log line.
Tests: the array key with and without the logger, QueryParameters on an
array, an app manager that rejects, one that never settles and one that
throws an Error, the close codes of the 4001/4009 refusals, and the server
test now checks the 4001 close code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:25:54 +00:00
|
|
|
$this->assertSame(4001, $connection->closeCode);
|
2026-11-04 10:56:48 +00:00
|
|
|
$this->assertFalse(isset($connection->app));
|
|
|
|
|
|
|
|
|
|
// Nothing to clean up, and closing must not throw.
|
|
|
|
|
$this->wsHandler->onClose($connection);
|
|
|
|
|
|
|
|
|
|
$connection->resetEvents();
|
|
|
|
|
$this->wsHandler->onMessage($connection, new Mocks\Message([
|
|
|
|
|
'event' => 'websocket.subscribe',
|
|
|
|
|
'data' => ['channel' => 'public-channel'],
|
|
|
|
|
]));
|
|
|
|
|
$connection->assertNothingSent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_disallowed_origin_gets_4009_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newConnection('TestOrigin', ['Origin' => 'https://google.ro']);
|
|
|
|
|
|
|
|
|
|
$this->wsHandler->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$connection
|
|
|
|
|
->assertSentEvent('websocket.error', [
|
|
|
|
|
'data' => [
|
|
|
|
|
'message' => 'The origin is not allowed for `TestOrigin`.',
|
|
|
|
|
'code' => 4009,
|
|
|
|
|
],
|
|
|
|
|
])
|
|
|
|
|
->assertNotSentEvent('websocket.connection_established')
|
|
|
|
|
->assertClosed();
|
BF refuse a broken app key or failed app lookup without stopping the server
Ratchet's router merges the query over the route parameters, so
/app/<anything>?appKey[]=x reached verifyAppKey() (and WebSocketsLogger's
onOpen when logging is on) with an array. QueryParameters::get(): string
threw a TypeError, onOpen only caught WebSocketException and Ratchet's
IoServer only catches \Exception, so the Error left the event loop and the
daemon exited. Anyone could do this before authenticating.
- QueryParameters::get() reads a non-string parameter as '', so the key
becomes an unknown key: websocket.error 4001 and a closed socket.
- onOpen catches any other Throwable, logs it with the trace, sends
websocket.error "Internal server error" (1011) and closes with 1011.
- verifyAppKey() settles the findByKey() promise in place instead of
await(). await() on a pending promise would run the server's own loop
again from a fiber inside onOpen. A rejected lookup is rethrown, a
pending one is refused; the AppManager contract now says findByKey()
must settle synchronously. ConfigAppManager already does.
- A refused connection (4001 unknown key, 4009 origin) closes with that
code instead of 1000. 4000-4099 tells Pusher-style clients not to
reconnect. ConnectionLogger::close() passes the code on.
- The refusal is logged at warning level without a trace, and the
client-supplied key goes into the JSON-encoded context, not the log line.
Tests: the array key with and without the logger, QueryParameters on an
array, an app manager that rejects, one that never settles and one that
throws an Error, the close codes of the 4001/4009 refusals, and the server
test now checks the 4001 close code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:25:54 +00:00
|
|
|
$this->assertSame(4009, $connection->closeCode);
|
|
|
|
|
|
|
|
|
|
$this->wsHandler->onClose($connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_array_app_key_gets_4001_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
// Ratchet's router merges the query over the route parameters, so
|
|
|
|
|
// /app/<key>?appKey[]=x reaches the handler with an array. It used
|
|
|
|
|
// to throw a TypeError out of QueryParameters::get(), which nothing
|
|
|
|
|
// caught, and the whole server stopped.
|
|
|
|
|
$connection = new Mocks\Connection;
|
|
|
|
|
$connection->httpRequest = new Request('GET', '/?appKey[]=x');
|
|
|
|
|
|
|
|
|
|
$this->wsHandler->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$connection
|
|
|
|
|
->assertSentEvent('websocket.error', [
|
|
|
|
|
'data' => [
|
|
|
|
|
'message' => 'Could not find app key ``.',
|
|
|
|
|
'code' => 4001,
|
|
|
|
|
],
|
|
|
|
|
])
|
|
|
|
|
->assertNotSentEvent('websocket.connection_established')
|
|
|
|
|
->assertClosed();
|
|
|
|
|
$this->assertSame(4001, $connection->closeCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_array_app_key_with_logging_on_gets_4001_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
// With logging on, WebSocketsLogger reads the key before the handler
|
|
|
|
|
// and hands it a ConnectionLogger.
|
|
|
|
|
$this->app->bind(ConnectionLogger::class, function () {
|
|
|
|
|
return (new ConnectionLogger(new BufferedOutput()))->enable(true);
|
|
|
|
|
});
|
|
|
|
|
$logger = WebSocketsLogger::decorate($this->wsHandler);
|
|
|
|
|
$logger->enable(true);
|
|
|
|
|
|
|
|
|
|
$connection = new Mocks\Connection;
|
|
|
|
|
$connection->httpRequest = new Request('GET', '/?appKey[]=x');
|
|
|
|
|
|
|
|
|
|
$logger->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$connection
|
|
|
|
|
->assertSentEvent('websocket.error', ['data' => ['message' => 'Could not find app key ``.', 'code' => 4001]])
|
|
|
|
|
->assertClosed();
|
|
|
|
|
// ConnectionLogger passes the close code on.
|
|
|
|
|
$this->assertSame(4001, $connection->closeCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_query_parameters_read_an_array_as_an_empty_string()
|
|
|
|
|
{
|
|
|
|
|
$query = QueryParameters::create(new Request('GET', '/?appKey[]=x&other=y'));
|
|
|
|
|
|
|
|
|
|
$this->assertSame('', $query->get('appKey'));
|
|
|
|
|
$this->assertSame('y', $query->get('other'));
|
|
|
|
|
$this->assertSame('', $query->get('missing'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_app_manager_that_rejects_gets_1011_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
$this->app->instance(AppManager::class, new class extends ConfigAppManager {
|
|
|
|
|
public function findByKey($appKey): PromiseInterface
|
|
|
|
|
{
|
|
|
|
|
return \React\Promise\reject(new \RuntimeException('app store down'));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertConnectionRefusedWithInternalError($this->newConnection());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_app_manager_that_does_not_settle_gets_1011_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
$this->app->instance(AppManager::class, new class extends ConfigAppManager {
|
|
|
|
|
public function findByKey($appKey): PromiseInterface
|
|
|
|
|
{
|
|
|
|
|
return (new Deferred())->promise();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertConnectionRefusedWithInternalError($this->newConnection());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_error_thrown_while_opening_gets_1011_and_is_closed()
|
|
|
|
|
{
|
|
|
|
|
$this->app->instance(AppManager::class, new class extends ConfigAppManager {
|
|
|
|
|
public function findByKey($appKey): PromiseInterface
|
|
|
|
|
{
|
|
|
|
|
throw new \TypeError('broken app manager');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertConnectionRefusedWithInternalError($this->newConnection());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* onOpen must neither throw nor leave the socket open when opening fails
|
|
|
|
|
* for a reason other than a WebSocketException.
|
|
|
|
|
*/
|
|
|
|
|
private function assertConnectionRefusedWithInternalError(Mocks\Connection $connection): void
|
|
|
|
|
{
|
|
|
|
|
$this->wsHandler->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$connection
|
|
|
|
|
->assertSentEvent('websocket.error', ['data' => ['message' => 'Internal server error', 'code' => 1011]])
|
|
|
|
|
->assertNotSentEvent('websocket.connection_established')
|
|
|
|
|
->assertClosed();
|
|
|
|
|
$this->assertSame(1011, $connection->closeCode);
|
|
|
|
|
$this->assertFalse(isset($connection->app));
|
2026-11-04 10:56:48 +00:00
|
|
|
|
|
|
|
|
$this->wsHandler->onClose($connection);
|
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'));
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->assertSame('{"event":"websocket.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']));
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->assertSame('{"event":"websocket.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
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onOpen($connection);
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$connection->assertSentEvent('websocket.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
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onClose($connection);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
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
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onError($connection, new UnknownAppKey('NonWorkingKey'));
|
2020-08-18 13:04:52 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$connection->assertSentEvent('websocket.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
|
2026-04-02 10:44:16 +00:00
|
|
|
->assertSentEvent('websocket.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'])
|
2026-04-02 10:44:16 +00:00
|
|
|
->assertSentEvent('websocket.connection_established')
|
|
|
|
|
->assertSentEvent('websocket_internal.subscription_succeeded');
|
2020-09-12 14:45:07 +00:00
|
|
|
|
|
|
|
|
$this->channelManager->declineNewConnections();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
$this->channelManager->acceptsNewConnections()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->newActiveConnection(['test-channel'])
|
|
|
|
|
->assertNothingSent()
|
|
|
|
|
->assertClosed();
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|