mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
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:
parent
9da13bc9d4
commit
b7687d998d
@ -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
|
||||
|
@ -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));
|
||||
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
25
src/rpc-server/tests/Stub/MiddlewareStub.php
Normal file
25
src/rpc-server/tests/Stub/MiddlewareStub.php
Normal 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';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user