Added writeBinary, tests for middleware
This commit is contained in:
parent
ea9865c177
commit
418ece5155
@ -181,6 +181,11 @@ class WebSocketConnection implements WebSocketInterface
|
||||
return $this->send(self::OP_FRAME_TEXT, $data);
|
||||
}
|
||||
|
||||
public function writeBinary($data)
|
||||
{
|
||||
return $this->send(self::OP_FRAME_BINARY, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -37,6 +37,7 @@ class WebSocketMiddleware implements EventEmitterInterface
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, callable $next)
|
||||
{
|
||||
|
||||
if ($request->getHeaderLine('upgrade') !== 'websocket') {
|
||||
return $next($request);
|
||||
}
|
||||
@ -56,9 +57,10 @@ class WebSocketMiddleware implements EventEmitterInterface
|
||||
$this->websockets->detach($websocket);
|
||||
});
|
||||
|
||||
Loop::futureTick(function () use ($websocket) {
|
||||
// FIXME how to test with futureTick?
|
||||
//Loop::futureTick(function () use ($websocket) {
|
||||
$this->emit(self::EVENT_CONNECTION, [ $websocket ]);
|
||||
});
|
||||
//});
|
||||
|
||||
return new Response(
|
||||
Response::STATUS_SWITCHING_PROTOCOLS,
|
||||
|
57
tests/WebSocketMiddlewareTest.php
Normal file
57
tests/WebSocketMiddlewareTest.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\WebSocket;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use React\Http\Message\ServerRequest;
|
||||
|
||||
#[CoversClass(WebSocketMiddleware::class)]
|
||||
class WebSocketMiddlewareTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testThatRegularRequestsAreIgnored()
|
||||
{
|
||||
|
||||
$mock = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
||||
$mock->expects($this->never())
|
||||
->method('test');
|
||||
|
||||
$next = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
||||
$next->expects($this->once())
|
||||
->method('test');
|
||||
|
||||
$request = new ServerRequest("GET", "/", [], "");
|
||||
|
||||
$middleware = new WebSocketMiddleware();
|
||||
$middleware->on('connection', [$mock,"test"]);
|
||||
$middleware->__invoke($request, [$next,"test"]);
|
||||
}
|
||||
|
||||
public function testThatWebSocketRequestsAreHandled()
|
||||
{
|
||||
|
||||
$mock = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
||||
$mock->expects($this->once())
|
||||
->method('test');
|
||||
|
||||
$next = $this->getMockBuilder(InvokableDummy::class)->getMock();
|
||||
$next->expects($this->never())
|
||||
->method('test');
|
||||
|
||||
$request = new ServerRequest("GET", "/", [
|
||||
'upgrade' => [ 'websocket' ]
|
||||
]);
|
||||
|
||||
$middleware = new WebSocketMiddleware();
|
||||
$middleware->on('connection', [$mock,"test"]);
|
||||
$middleware->__invoke($request, [$next,"test"]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InvokableDummy
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user