Skip to content

Commit

Permalink
Merge branch 'master' into 3.1-merge
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/service-governance-nacos/src/ClientFactory.php
  • Loading branch information
limingxinleo committed Jul 21, 2023
2 parents 9d788a0 + fa9d8c1 commit a482cf1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ public function file(string $uri, array $data = [], array $headers = [])
return $this->packer->unpack((string) $response->getBody());
}

public function request(string $method, string $path, array $options = [])
public function request(string $method, string $path, array $options = [], ?callable $callable = null)
{
return wait(function () use ($method, $path, $options) {
return wait(function () use ($method, $path, $options, $callable) {
$callable && $callable();
return $this->execute($this->initRequest($method, $path, $options));
}, $this->waitTimeout);
}

public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterface
public function sendRequest(ServerRequestInterface $psr7Request, ?callable $callable = null): ResponseInterface
{
return wait(function () use ($psr7Request) {
return wait(function () use ($psr7Request, $callable) {
$callable && $callable();
return $this->execute($psr7Request);
}, $this->waitTimeout);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Hyperf\Codec\Json;
use Hyperf\Config\Config;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\Coroutine\Coroutine;
Expand Down Expand Up @@ -121,6 +122,19 @@ public function testClientGetUri()
$this->assertSame($id, $data['params']['id']);
}

public function testClientCallable()
{
$container = $this->getContainer();

$client = new Client($container);

$id = uniqid();

$res = $client->request('GET', '/context', callable: fn () => Context::set('request_id', $id));

$this->assertSame(['request_id' => $id], Json::decode((string) $res->getBody()));
}

public function getContainer()
{
$container = Mockery::mock(Container::class);
Expand Down Expand Up @@ -177,6 +191,7 @@ public function getContainer()
Router::get('/', [FooController::class, 'index']);
Router::get('/exception', [FooController::class, 'exception']);
Router::get('/id', [FooController::class, 'id']);
Router::get('/context', [FooController::class, 'context']);
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);

return $container;
Expand Down
7 changes: 7 additions & 0 deletions tests/Stub/FooController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function id()
return ['code' => 0, 'data' => Coroutine::id()];
}

public function context()
{
return [
'request_id' => Context::getOrSet('request_id', uniqid()),
];
}

public function request()
{
/** @var ServerRequestInterface $request */
Expand Down

0 comments on commit a482cf1

Please sign in to comment.