2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
namespace BlaxSoftware\LaravelWebSockets\Test\Mocks;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
|
use PHPUnit\Framework\Assert as PHPUnit;
|
2020-03-04 09:58:39 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
class Connection implements ConnectionInterface
|
|
|
|
|
{
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* The request instance.
|
|
|
|
|
*
|
|
|
|
|
* @var Request
|
|
|
|
|
*/
|
2018-11-25 22:43:43 +00:00
|
|
|
public $httpRequest;
|
|
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* The sent data through the connection.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2018-11-25 23:20:18 +00:00
|
|
|
public $sentData = [];
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* The raw (unencoded) sent data.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2018-12-03 09:21:35 +00:00
|
|
|
public $sentRawData = [];
|
|
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* Wether the connection has been closed.
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2018-11-26 23:00:26 +00:00
|
|
|
public $closed = false;
|
|
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* Send the data through the connection.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-28 22:59:58 +00:00
|
|
|
public function send($data)
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
|
|
|
|
$this->sentData[] = json_decode($data, true);
|
2018-12-03 09:21:35 +00:00
|
|
|
$this->sentRawData[] = $data;
|
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
|
|
|
/**
|
|
|
|
|
* The close code passed to close(), if any.
|
|
|
|
|
*
|
|
|
|
|
* @var int|null
|
|
|
|
|
*/
|
|
|
|
|
public $closeCode = null;
|
|
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* Mark the connection as closed.
|
|
|
|
|
*
|
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
|
|
|
* @param int|null $code
|
2020-08-30 17:00:45 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
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
|
|
|
public function close($code = null)
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2018-11-26 23:00:26 +00:00
|
|
|
$this->closed = true;
|
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->closeCode = $code;
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-16 08:02:58 +00:00
|
|
|
/**
|
|
|
|
|
* Reset the events for assertions.
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function resetEvents()
|
|
|
|
|
{
|
|
|
|
|
$this->sentData = [];
|
|
|
|
|
$this->sentRawData = [];
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dump & stop execution.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function dd()
|
|
|
|
|
{
|
|
|
|
|
dd([
|
|
|
|
|
'sentData' => $this->sentData,
|
|
|
|
|
'sentRawData' => $this->sentRawData,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* Assert that an event got sent.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $additionalParameters
|
2020-09-05 19:40:52 +00:00
|
|
|
* @return $this
|
2020-08-30 17:00:45 +00:00
|
|
|
*/
|
2018-11-25 23:20:18 +00:00
|
|
|
public function assertSentEvent(string $name, array $additionalParameters = [])
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2018-11-25 23:20:18 +00:00
|
|
|
$event = collect($this->sentData)->firstWhere('event', '=', $name);
|
|
|
|
|
|
2018-11-25 22:43:43 +00:00
|
|
|
PHPUnit::assertTrue(
|
2018-11-25 23:20:18 +00:00
|
|
|
! is_null($event)
|
2018-11-25 22:43:43 +00:00
|
|
|
);
|
2018-11-25 23:20:18 +00:00
|
|
|
|
|
|
|
|
foreach ($additionalParameters as $parameter => $value) {
|
|
|
|
|
PHPUnit::assertSame($event[$parameter], $value);
|
|
|
|
|
}
|
2020-09-05 19:40:52 +00:00
|
|
|
|
|
|
|
|
return $this;
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
2018-11-26 23:00:26 +00:00
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* Assert that an event got not sent.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
2020-09-05 19:40:52 +00:00
|
|
|
* @return $this
|
2020-08-30 17:00:45 +00:00
|
|
|
*/
|
2018-11-27 21:26:29 +00:00
|
|
|
public function assertNotSentEvent(string $name)
|
|
|
|
|
{
|
|
|
|
|
$event = collect($this->sentData)->firstWhere('event', '=', $name);
|
|
|
|
|
|
|
|
|
|
PHPUnit::assertTrue(
|
|
|
|
|
is_null($event)
|
|
|
|
|
);
|
2020-09-05 19:40:52 +00:00
|
|
|
|
|
|
|
|
return $this;
|
2018-11-27 21:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-12 14:45:07 +00:00
|
|
|
/**
|
|
|
|
|
* Assert that no events occured within the connection.
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function assertNothingSent()
|
|
|
|
|
{
|
|
|
|
|
PHPUnit::assertEquals([], $this->sentData);
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 17:00:45 +00:00
|
|
|
/**
|
|
|
|
|
* Assert the connection is closed.
|
|
|
|
|
*
|
2020-09-05 19:40:52 +00:00
|
|
|
* @return $this
|
2020-08-30 17:00:45 +00:00
|
|
|
*/
|
2018-11-26 23:00:26 +00:00
|
|
|
public function assertClosed()
|
|
|
|
|
{
|
|
|
|
|
PHPUnit::assertTrue($this->closed);
|
2020-09-05 19:40:52 +00:00
|
|
|
|
|
|
|
|
return $this;
|
2018-11-26 23:00:26 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|