Skip to content

Commit a482cf1

Browse files
committed
Merge branch 'master' into 3.1-merge
# Conflicts: # src/service-governance-nacos/src/ClientFactory.php
2 parents 9d788a0 + fa9d8c1 commit a482cf1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Client.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ public function file(string $uri, array $data = [], array $headers = [])
145145
return $this->packer->unpack((string) $response->getBody());
146146
}
147147

148-
public function request(string $method, string $path, array $options = [])
148+
public function request(string $method, string $path, array $options = [], ?callable $callable = null)
149149
{
150-
return wait(function () use ($method, $path, $options) {
150+
return wait(function () use ($method, $path, $options, $callable) {
151+
$callable && $callable();
151152
return $this->execute($this->initRequest($method, $path, $options));
152153
}, $this->waitTimeout);
153154
}
154155

155-
public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterface
156+
public function sendRequest(ServerRequestInterface $psr7Request, ?callable $callable = null): ResponseInterface
156157
{
157-
return wait(function () use ($psr7Request) {
158+
return wait(function () use ($psr7Request, $callable) {
159+
$callable && $callable();
158160
return $this->execute($psr7Request);
159161
}, $this->waitTimeout);
160162
}

tests/ClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hyperf\Codec\Json;
1515
use Hyperf\Config\Config;
1616
use Hyperf\Context\ApplicationContext;
17+
use Hyperf\Context\Context;
1718
use Hyperf\Contract\ConfigInterface;
1819
use Hyperf\Contract\NormalizerInterface;
1920
use Hyperf\Coroutine\Coroutine;
@@ -121,6 +122,19 @@ public function testClientGetUri()
121122
$this->assertSame($id, $data['params']['id']);
122123
}
123124

125+
public function testClientCallable()
126+
{
127+
$container = $this->getContainer();
128+
129+
$client = new Client($container);
130+
131+
$id = uniqid();
132+
133+
$res = $client->request('GET', '/context', callable: fn () => Context::set('request_id', $id));
134+
135+
$this->assertSame(['request_id' => $id], Json::decode((string) $res->getBody()));
136+
}
137+
124138
public function getContainer()
125139
{
126140
$container = Mockery::mock(Container::class);
@@ -177,6 +191,7 @@ public function getContainer()
177191
Router::get('/', [FooController::class, 'index']);
178192
Router::get('/exception', [FooController::class, 'exception']);
179193
Router::get('/id', [FooController::class, 'id']);
194+
Router::get('/context', [FooController::class, 'context']);
180195
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);
181196

182197
return $container;

tests/Stub/FooController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function id()
3333
return ['code' => 0, 'data' => Coroutine::id()];
3434
}
3535

36+
public function context()
37+
{
38+
return [
39+
'request_id' => Context::getOrSet('request_id', uniqid()),
40+
];
41+
}
42+
3643
public function request()
3744
{
3845
/** @var ServerRequestInterface $request */

0 commit comments

Comments
 (0)