mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added testing.
This commit is contained in:
parent
551d25c758
commit
315ba20ad2
@ -185,6 +185,7 @@
|
||||
"HyperfTest\\Etcd\\": "src/etcd/tests/",
|
||||
"HyperfTest\\Event\\": "src/event/tests/",
|
||||
"HyperfTest\\GrpcClient\\": "src/grpc-client/tests/",
|
||||
"HyperfTest\\GrpcServer\\": "src/grpc-server/tests/",
|
||||
"HyperfTest\\Guzzle\\": "src/guzzle/tests/",
|
||||
"HyperfTest\\HttpMessage\\": "src/http-message/tests/",
|
||||
"HyperfTest\\HttpServer\\": "src/http-server/tests/",
|
||||
|
@ -23,6 +23,7 @@
|
||||
<directory suffix="Test.php">./src/elasticsearch/tests</directory>
|
||||
<directory suffix="Test.php">./src/event/tests</directory>
|
||||
<directory suffix="Test.php">./src/grpc-client/tests</directory>
|
||||
<directory suffix="Test.php">./src/grpc-server/tests</directory>
|
||||
<directory suffix="Test.php">./src/guzzle/tests</directory>
|
||||
<directory suffix="Test.php">./src/http-message/tests</directory>
|
||||
<directory suffix="Test.php">./src/http-server/tests</directory>
|
||||
|
@ -34,6 +34,7 @@
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"HyperfTest\\GrpcServer\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
|
@ -58,16 +58,14 @@ class GrpcExceptionHandler extends ExceptionHandler
|
||||
/**
|
||||
* Transfer the non-standard response content to a standard response object.
|
||||
*
|
||||
* @param int|string $code
|
||||
* @param int $code
|
||||
* @param string $message
|
||||
* @param ResponseInterface $response
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
protected function transferToResponse($code, $message, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
$response = $response->withAddedHeader('Content-Type', 'application/grpc')
|
||||
->withAddedHeader('trailer', 'grpc-status, grpc-message')
|
||||
->withStatus(StatusCode::HTTP_CODE_MAPPING[$code] ?: 500);
|
||||
->withStatus(StatusCode::HTTP_CODE_MAPPING[$code] ?? 500);
|
||||
|
||||
$response->getSwooleResponse()->trailer('grpc-status', (string) $code);
|
||||
$response->getSwooleResponse()->trailer('grpc-message', (string) $message);
|
||||
|
98
src/grpc-server/tests/GrpcExceptionHandlerTest.php
Normal file
98
src/grpc-server/tests/GrpcExceptionHandlerTest.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\GrpcServer;
|
||||
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
|
||||
use Hyperf\GrpcServer\StatusCode;
|
||||
use Hyperf\HttpMessage\Server\Response;
|
||||
use HyperfTest\GrpcServer\Stub\GrpcExceptionHandlerStub;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class GrpcExceptionHandlerTest extends TestCase
|
||||
{
|
||||
public function testTransferToResponse200()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$logger = $container->get(StdoutLoggerInterface::class);
|
||||
$formatter = $container->get(FormatterInterface::class);
|
||||
$swooleResponse = Mockery::mock(\Swoole\Http\Response::class);
|
||||
$data = [];
|
||||
$swooleResponse->shouldReceive('trailer')->andReturnUsing(function (...$args) use (&$data) {
|
||||
$data[] = $args;
|
||||
});
|
||||
$response = new Response($swooleResponse);
|
||||
$handler = new GrpcExceptionHandlerStub($logger, $formatter);
|
||||
$response = $handler->transferToResponse(StatusCode::OK, 'OK', $response);
|
||||
$this->assertSame([['grpc-status', '0'], ['grpc-message', 'OK']], $data);
|
||||
$this->assertSame(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testTransferToResponse499()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$logger = $container->get(StdoutLoggerInterface::class);
|
||||
$formatter = $container->get(FormatterInterface::class);
|
||||
$swooleResponse = Mockery::mock(\Swoole\Http\Response::class);
|
||||
$data = [];
|
||||
$swooleResponse->shouldReceive('trailer')->andReturnUsing(function (...$args) use (&$data) {
|
||||
$data[] = $args;
|
||||
});
|
||||
$response = new Response($swooleResponse);
|
||||
$handler = new GrpcExceptionHandlerStub($logger, $formatter);
|
||||
$response = $handler->transferToResponse(StatusCode::CANCELLED, 'The operation was cancelled', $response);
|
||||
$this->assertSame([['grpc-status', '1'], ['grpc-message', 'The operation was cancelled']], $data);
|
||||
$this->assertSame(499, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testTransferToResponseUnKnown()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$logger = $container->get(StdoutLoggerInterface::class);
|
||||
$formatter = $container->get(FormatterInterface::class);
|
||||
$swooleResponse = Mockery::mock(\Swoole\Http\Response::class);
|
||||
$data = [];
|
||||
$swooleResponse->shouldReceive('trailer')->andReturnUsing(function (...$args) use (&$data) {
|
||||
$data[] = $args;
|
||||
});
|
||||
$response = new Response($swooleResponse);
|
||||
$handler = new GrpcExceptionHandlerStub($logger, $formatter);
|
||||
$response = $handler->transferToResponse(123, 'UNKNOWN', $response);
|
||||
$this->assertSame([['grpc-status', '123'], ['grpc-message', 'UNKNOWN']], $data);
|
||||
$this->assertSame(500, $response->getStatusCode());
|
||||
}
|
||||
|
||||
protected function getContainer()
|
||||
{
|
||||
$container = Mockery::mock(ContainerInterface::class);
|
||||
|
||||
$logger = Mockery::mock(StdoutLoggerInterface::class);
|
||||
$logger->shouldReceive(Mockery::any())->with(Mockery::any())->andReturn(null);
|
||||
$container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturn($logger);
|
||||
|
||||
$formatter = Mockery::mock(FormatterInterface::class);
|
||||
$formatter->shouldReceive(Mockery::any())->with(Mockery::any())->andReturn('');
|
||||
$container->shouldReceive('get')->with(FormatterInterface::class)->andReturn($formatter);
|
||||
|
||||
return $container;
|
||||
}
|
||||
}
|
24
src/grpc-server/tests/Stub/GrpcExceptionHandlerStub.php
Normal file
24
src/grpc-server/tests/Stub/GrpcExceptionHandlerStub.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\GrpcServer\Stub;
|
||||
|
||||
use Hyperf\GrpcServer\Exception\Handler\GrpcExceptionHandler;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class GrpcExceptionHandlerStub extends GrpcExceptionHandler
|
||||
{
|
||||
public function transferToResponse($code, $message, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
return parent::transferToResponse($code, $message, $response);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user