From b7687d998d79b4b66f180dac1227ed7c9399e769 Mon Sep 17 00:00:00 2001 From: lionXu <1556147829@qq.com> Date: Tue, 26 Jan 2021 15:41:44 +0800 Subject: [PATCH] Fixed unexpected behavior for `middlewares` when using `rpc-server`. (#3204) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed unexpected behavior for `middlewares` when using `rpc-server`. * Added test cases Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-2.1.md | 4 +++ .../src/Router/DispatcherFactory.php | 9 +++--- .../tests/RouterDispatcherFactoryTest.php | 30 +++++++++++++++++++ src/rpc-server/tests/Stub/MiddlewareStub.php | 25 ++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/rpc-server/tests/Stub/MiddlewareStub.php diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index bd0ffe548..fb2849ec3 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -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 diff --git a/src/rpc-server/src/Router/DispatcherFactory.php b/src/rpc-server/src/Router/DispatcherFactory.php index 988985690..544cda9e6 100644 --- a/src/rpc-server/src/Router/DispatcherFactory.php +++ b/src/rpc-server/src/Router/DispatcherFactory.php @@ -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)); diff --git a/src/rpc-server/tests/RouterDispatcherFactoryTest.php b/src/rpc-server/tests/RouterDispatcherFactoryTest.php index b8cfbdb58..eecfc0770 100644 --- a/src/rpc-server/tests/RouterDispatcherFactoryTest.php +++ b/src/rpc-server/tests/RouterDispatcherFactoryTest.php @@ -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')); + } } diff --git a/src/rpc-server/tests/Stub/MiddlewareStub.php b/src/rpc-server/tests/Stub/MiddlewareStub.php new file mode 100644 index 000000000..680962707 --- /dev/null +++ b/src/rpc-server/tests/Stub/MiddlewareStub.php @@ -0,0 +1,25 @@ +