Skip to content

Commit af12cc3

Browse files
authored
Added method Hyperf\Testing\Client::sendRequest(), you can use your own server request. (#3403)
1 parent c28d027 commit af12cc3

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/Client.php

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,46 @@ public function file($uri, $data = [], $headers = [])
137137
public function request(string $method, string $path, array $options = [])
138138
{
139139
return wait(function () use ($method, $path, $options) {
140-
/*
141-
* @var Psr7Request
142-
*/
143-
[$psr7Request, $psr7Response] = $this->init($method, $path, $options);
144-
145-
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
146-
/** @var Dispatched $dispatched */
147-
$dispatched = $psr7Request->getAttribute(Dispatched::class);
148-
$middlewares = $this->middlewares;
149-
if ($dispatched->isFound()) {
150-
$registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
151-
$middlewares = array_merge($middlewares, $registeredMiddlewares);
152-
}
153-
154-
try {
155-
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
156-
} catch (\Throwable $throwable) {
157-
// Delegate the exception to exception handler.
158-
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
159-
}
140+
return $this->execute($this->init($method, $path, $options));
141+
}, $this->waitTimeout);
142+
}
160143

161-
return $psr7Response;
144+
public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterface
145+
{
146+
return wait(function () use ($psr7Request) {
147+
return $this->execute($psr7Request);
162148
}, $this->waitTimeout);
163149
}
164150

151+
protected function execute(ServerRequestInterface $psr7Request): ResponseInterface
152+
{
153+
$this->persistToContext($psr7Request, new Psr7Response());
154+
155+
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
156+
/** @var Dispatched $dispatched */
157+
$dispatched = $psr7Request->getAttribute(Dispatched::class);
158+
$middlewares = $this->middlewares;
159+
if ($dispatched->isFound()) {
160+
$registeredMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
161+
$middlewares = array_merge($middlewares, $registeredMiddlewares);
162+
}
163+
164+
try {
165+
$psr7Response = $this->dispatcher->dispatch($psr7Request, $middlewares, $this->coreMiddleware);
166+
} catch (\Throwable $throwable) {
167+
// Delegate the exception to exception handler.
168+
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
169+
}
170+
171+
return $psr7Response;
172+
}
173+
174+
protected function persistToContext(ServerRequestInterface $request, ResponseInterface $response)
175+
{
176+
Context::set(ServerRequestInterface::class, $request);
177+
Context::set(ResponseInterface::class, $response);
178+
}
179+
165180
protected function initBaseUri($server): void
166181
{
167182
if ($this->container->has(ConfigInterface::class)) {
@@ -176,7 +191,7 @@ protected function initBaseUri($server): void
176191
}
177192
}
178193

179-
protected function init(string $method, string $path, array $options = []): array
194+
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
180195
{
181196
$query = $options['query'] ?? [];
182197
$params = $options['form_params'] ?? [];
@@ -198,14 +213,9 @@ protected function init(string $method, string $path, array $options = []): arra
198213
$body = new SwooleStream($content);
199214

200215
$request = new Psr7Request($method, $uri, $headers, $body);
201-
$request = $request->withQueryParams($query)
216+
return $request->withQueryParams($query)
202217
->withParsedBody($data)
203218
->withUploadedFiles($this->normalizeFiles($multipart));
204-
205-
Context::set(ServerRequestInterface::class, $psr7Request = $request);
206-
Context::set(ResponseInterface::class, $psr7Response = new Psr7Response());
207-
208-
return [$psr7Request, $psr7Response];
209219
}
210220

211221
protected function normalizeFiles(array $multipart): array

src/HttpClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,9 @@ public function file($uri, $data = [], $headers = [])
106106

107107
return $this->packer->unpack((string) $response->getBody());
108108
}
109+
110+
public function client()
111+
{
112+
return $this->client;
113+
}
109114
}

0 commit comments

Comments
 (0)