update workerman1

This commit is contained in:
james 2025-06-03 15:40:37 +08:00
parent 9c8b4e25f1
commit d5d6cddcab
95 changed files with 45 additions and 98 deletions

View File

@ -5,7 +5,7 @@ require_once __DIR__ . '/libs/MyWebSocketHandler.php';
use Workerman\Worker;
use App\MyWebSocketHandler;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
$handler = new MyWebSocketHandler();

View File

@ -6,3 +6,7 @@
2025-06-03 15:11:56 pid:6213 Workerman[start.php] received signal SIGINT
2025-06-03 15:11:56 pid:6213 Workerman[start.php] stopping
2025-06-03 15:11:56 pid:6213 Workerman[start.php] has been stopped
2025-06-03 15:39:55 pid:455 Workerman[start.php] start in DEBUG mode
2025-06-03 15:39:56 pid:455 Workerman[start.php] received signal SIGINT
2025-06-03 15:39:56 pid:455 Workerman[start.php] stopping
2025-06-03 15:39:56 pid:455 Workerman[start.php] has been stopped

View File

@ -1,110 +1,53 @@
<?php
// 手动加载类文件
require_once __DIR__ . '/../src/libs/MyWebSocketHandler.php';
use PHPUnit\Framework\TestCase;
use App\MyWebSocketHandler;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
use Workerman\Worker;
it('customizes request class', function () {
//backup old request class
$oldRequestClass = Http::requestClass();
class MyWebSocketHandlerTest extends TestCase
{
private $handler;
//actual test
$class = new class {
};
Http::requestClass($class::class);
expect(Http::requestClass())->toBe($class::class);
protected function setUp(): void
{
$this->handler = new MyWebSocketHandler();
}
//restore old request class
Http::requestClass($oldRequestClass);
});
public function testOnConnect()
{
$connection = $this->createMock(TcpConnection::class);
$worker = $this->createMock(Worker::class);
it('tests ::input', function () {
//test 413 payload too large
testWithConnectionClose(function (TcpConnection $tcpConnection) {
expect(Http::input(str_repeat('jhdxr', 3333), $tcpConnection))
->toBe(0);
}, '413 Payload Too Large');
// 使用 ob_start + ob_get_clean 捕获输出
ob_start();
$this->handler->onConnect($connection);
$output = ob_get_clean();
//example request from ChatGPT :)
$buffer = "POST /path/to/resource HTTP/1.1\r\n" .
"Host: example.com\r\n" .
"Content-Type: application/json\r\n" .
"Content-Length: 27\r\n" .
"\r\n" .
'{"key": "value", "foo": "bar"}';
$this->assertStringContainsString('New connection', $output);
}
//unrecognized method
testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
expect(Http::input(str_replace('POST', 'MIAOWU', $buffer), $tcpConnection))
->toBe(0);
}, '400 Bad Request');
public function testOnMessage()
{
$connection = $this->createMock(TcpConnection::class);
$connection->expects($this->once())
->method('send')
->with($this->equalTo('Hello world'));
//content-length exceeds connection max package size
testWithConnectionClose(function (TcpConnection $tcpConnection) use ($buffer) {
$tcpConnection->maxPackageSize = 10;
expect(Http::input($buffer, $tcpConnection))
->toBe(0);
}, '413 Payload Too Large');
});
$this->handler->onMessage($connection, 'world');
}
it('tests ::encode for non-object response', function () {
/** @var TcpConnection $tcpConnection */
$tcpConnection = Mockery::mock(TcpConnection::class);
$tcpConnection->headers = [
'foo' => 'bar',
'jhdxr' => ['a', 'b'],
];
$extHeader = "foo: bar\r\n" .
"jhdxr: a\r\n" .
"jhdxr: b\r\n";
public function testOnClose()
{
$connection = $this->createMock(TcpConnection::class);
expect(Http::encode('xiami', $tcpConnection))
->toBe("HTTP/1.1 200 OK\r\n" .
"Server: workerman\r\n" .
"{$extHeader}Connection: keep-alive\r\n" .
"Content-Type: text/html;charset=utf-8\r\n" .
"Content-Length: 5\r\n\r\nxiami");
});
ob_start();
$this->handler->onClose($connection);
$output = ob_get_clean();
it('tests ::encode for ' . Response::class, function () {
/** @var TcpConnection $tcpConnection */
$tcpConnection = Mockery::mock(TcpConnection::class);
$tcpConnection->headers = [
'foo' => 'bar',
'jhdxr' => ['a', 'b'],
];
$extHeader = "foo: bar\r\n" .
"jhdxr: a\r\n" .
"jhdxr: b\r\n";
$response = new Response(body: 'xiami');
expect(Http::encode($response, $tcpConnection))
->toBe("HTTP/1.1 200 OK\r\n" .
"Server: workerman\r\n" .
"{$extHeader}Connection: keep-alive\r\n" .
"Content-Type: text/html;charset=utf-8\r\n" .
"Content-Length: 5\r\n\r\nxiami");
});
it('tests ::decode', function () {
/** @var TcpConnection $tcpConnection */
$tcpConnection = Mockery::mock(TcpConnection::class);
//example request from ChatGPT :)
$buffer = "POST /path/to/resource HTTP/1.1\r\n" .
"Host: example.com\r\n" .
"Content-Type: application/json\r\n" .
"Content-Length: 27\r\n" .
"\r\n" .
'{"key": "value", "foo": "bar"}';
$value = expect(Http::decode($buffer, $tcpConnection))
->toBeInstanceOf(Request::class)
->value;
//test cache
expect($value == Http::decode($buffer, $tcpConnection))
->toBeTrue();
});
$this->assertStringContainsString('Connection closed', $output);
}
}