更新 tests/CalculatorTest.php
Some checks failed
Run Unit Tests / test (pull_request) Failing after 12s
Some checks failed
Run Unit Tests / test (pull_request) Failing after 12s
This commit is contained in:
parent
db32f06578
commit
4533291324
@ -1,110 +1,50 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use src\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);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user