Fixed unexpected behavior for middlewares when using rpc-server. (#3204)

* Fixed unexpected behavior for `middlewares` when using `rpc-server`.

* Added test cases

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
lionXu 2021-01-26 15:41:44 +08:00 committed by GitHub
parent 9da13bc9d4
commit b7687d998d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 4 deletions

View File

@ -1,5 +1,9 @@
# v2.1.5 - TBD
## Fixed
- [#3204](https://github.com/hyperf/hyperf/pull/3204) Fixed unexpected behavior for `middlewares` when using `rpc-server`.
# v2.1.4 - 2021-01-25
## Fixed

View File

@ -126,15 +126,16 @@ class DispatcherFactory
$methodName,
]);
$methodMiddlewares = $middlewares;
// Handle method level middlewares.
if (isset($methodMetadata[$methodName])) {
$methodMiddlewares = $this->handleMiddleware($methodMetadata[$methodName]);
$middlewares = array_merge($methodMiddlewares, $middlewares);
$methodMiddlewares = array_merge($this->handleMiddleware($methodMetadata[$methodName]), $middlewares);
}
$middlewares = array_unique($middlewares);
// TODO: Remove array_unique from v3.0.
$methodMiddlewares = array_unique($methodMiddlewares);
// Register middlewares.
MiddlewareManager::addMiddlewares($annotation->server, $path, 'POST', $middlewares);
MiddlewareManager::addMiddlewares($annotation->server, $path, 'POST', $methodMiddlewares);
// Trigger the AfterPathRegister event.
$this->eventDispatcher->dispatch(new AfterPathRegister($path, $className, $methodName, $annotation));

View File

@ -11,6 +11,8 @@ declare(strict_types=1);
*/
namespace HyperfTest\RpcServer;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\MiddlewareManager;
use Hyperf\Rpc\Contract\PathGeneratorInterface;
use Hyperf\Rpc\PathGenerator\PathGenerator;
use Hyperf\RpcServer\Annotation\RpcService;
@ -18,6 +20,7 @@ use Hyperf\RpcServer\Event\AfterPathRegister;
use Hyperf\RpcServer\Router\DispatcherFactory;
use HyperfTest\RpcServer\Stub\ContainerStub;
use HyperfTest\RpcServer\Stub\IdGeneratorStub;
use HyperfTest\RpcServer\Stub\MiddlewareStub;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
@ -31,6 +34,7 @@ class RouterDispatcherFactoryTest extends TestCase
protected function tearDown(): void
{
Mockery::close();
MiddlewareManager::$container = [];
}
public function testHandleRpcService()
@ -55,4 +59,30 @@ class RouterDispatcherFactoryTest extends TestCase
$m->setAccessible(true);
$m->invokeArgs($factory, [IdGeneratorStub::class, new RpcService('IdGenerator'), [], []]);
}
public function testHandleRpcServiceWithMiddlewares()
{
$container = ContainerStub::getContainer();
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
$dispatcher->shouldReceive('dispatch')->withAnyArgs()->andReturn(null);
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn($dispatcher);
$container->shouldReceive('get')->with(PathGeneratorInterface::class)->andReturn(new PathGenerator());
$factory = new DispatcherFactory(
$container->get(EventDispatcherInterface::class),
$container->get(PathGeneratorInterface::class)
);
$ref = new \ReflectionClass($factory);
$m = $ref->getMethod('handleRpcService');
$m->setAccessible(true);
$m->invokeArgs($factory, [MiddlewareStub::class, new RpcService('IdGenerator'), [
'generate' => [
Middleware::class => new Middleware(['value' => 'Bar']),
],
], [
'Foo',
]]);
$this->assertSame(['Foo'], MiddlewareManager::get('jsonrpc-http', '/middleware_stub/foo', 'POST'));
$this->assertSame(['Bar', 'Foo'], MiddlewareManager::get('jsonrpc-http', '/middleware_stub/generate', 'POST'));
}
}

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\RpcServer\Stub;
class MiddlewareStub
{
public function generate(): string
{
return uniqid();
}
public function foo(): string
{
return 'foo';
}
}