100 lines
2.8 KiB
PHP
100 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\React\WebSocket;
|
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
#[CoversClass(WebSocketProtocol::class)]
|
|
class WebSocketProtocolTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
|
|
public function testEncodingFrames()
|
|
{
|
|
$codec = new WebSocketProtocol();
|
|
|
|
$msg = $codec->encode([
|
|
'opcode'=>WebSocketConnection::OP_PING,
|
|
'payload'=>"ping"
|
|
]);
|
|
$this->assertEquals("\x89\x04ping", $msg);
|
|
|
|
$msg = $codec->encode([
|
|
'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
|
|
'payload'=>"abcdefgh"]);
|
|
$this->assertEquals("\x81\x08abcdefgh", $msg);
|
|
|
|
$msg = $codec->encode([
|
|
'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
|
|
'payload'=>"abcdefgh",
|
|
'masked'=>true,
|
|
'mask'=>"\x00\x00\x00\x00"
|
|
]);
|
|
$this->assertEquals("\x81\x88\x00\x00\x00\x00abcdefgh", $msg);
|
|
|
|
$msg = $codec->encode([
|
|
'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
|
|
'payload'=>"abcdefgh",
|
|
'masked'=>true,
|
|
'mask'=>"\x00\xFF\x00\xFF"
|
|
]);
|
|
$this->assertEquals("\x81\x88\x00\xFF\x00\xFFa\x9dc\x9be\x99g\x97", $msg);
|
|
|
|
}
|
|
|
|
public function testDecodingFrames()
|
|
{
|
|
$codec = new WebSocketProtocol();
|
|
|
|
$msg = $codec->decode("\x89\x04ping");
|
|
$this->assertEquals([
|
|
'opcode'=>WebSocketConnection::OP_PING,
|
|
'payload'=>"ping",
|
|
'final'=>true,
|
|
'rsv1'=>false,
|
|
'rsv2'=>false,
|
|
'rsv3'=>false,
|
|
'length'=>4,
|
|
'masked'=>false
|
|
], $msg);
|
|
|
|
$msg = $codec->decode("\x81\x08abcdefgh");
|
|
$this->assertEquals([
|
|
'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
|
|
'payload'=>"abcdefgh",
|
|
'final'=>true,
|
|
'rsv1'=>false,
|
|
'rsv2'=>false,
|
|
'rsv3'=>false,
|
|
'length'=>8,
|
|
'masked'=>false
|
|
], $msg);
|
|
|
|
$msg = $codec->decode("\x81\x88\x00\x00\x00\x00abcdefgh");
|
|
$this->assertEquals([
|
|
'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
|
|
'payload'=>"abcdefgh",
|
|
'final'=>true,
|
|
'rsv1'=>false,
|
|
'rsv2'=>false,
|
|
'rsv3'=>false,
|
|
'length'=>8,
|
|
'masked'=>true,
|
|
'mask'=>"\x00\x00\x00\x00"
|
|
], $msg);
|
|
|
|
$msg = $codec->decode("\x81\x88\x00\xFF\x00\xFFa\x9dc\x9be\x99g\x97");
|
|
$this->assertEquals([
|
|
'opcode'=>WebSocketConnection::OP_FRAME_TEXT,
|
|
'payload'=>"abcdefgh",
|
|
'final'=>true,
|
|
'rsv1'=>false,
|
|
'rsv2'=>false,
|
|
'rsv3'=>false,
|
|
'length'=>8,
|
|
'masked'=>true,
|
|
'mask'=>"\x00\xFF\x00\xFF"
|
|
], $msg);
|
|
|
|
}
|
|
|
|
} |