Added document about how to use cookies for testing client. (#4427)

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
半桶水 2022-01-07 20:12:58 +08:00 committed by GitHub
parent 89b030a75c
commit 3e44fd5d2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 36 deletions

View File

@ -100,6 +100,23 @@ $result = $client->json('/user/0',[
]);
```
### 使用 Cookies
```php
<?php
use Hyperf\Testing\Client;
use Hyperf\Utils\Codec\Json;
$client = make(Client::class);
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
'X-CODE' => $id = uniqid(),
]));
$data = Json::decode((string) $response->getBody());
```
## 示例
让我们写个小 DEMO 来测试一下。

View File

@ -137,7 +137,7 @@ class Client extends Server
public function request(string $method, string $path, array $options = [])
{
return wait(function () use ($method, $path, $options) {
return $this->execute($this->init($method, $path, $options));
return $this->execute($this->initRequest($method, $path, $options));
}, $this->waitTimeout);
}
@ -148,6 +148,50 @@ class Client extends Server
}, $this->waitTimeout);
}
public function initRequest(string $method, string $path, array $options = []): ServerRequestInterface
{
$query = $options['query'] ?? [];
$params = $options['form_params'] ?? [];
$json = $options['json'] ?? [];
$headers = $options['headers'] ?? [];
$multipart = $options['multipart'] ?? [];
$parsePath = parse_url($path);
$path = $parsePath['path'];
$uriPathQuery = $parsePath['query'] ?? [];
if (! empty($uriPathQuery)) {
parse_str($uriPathQuery, $pathQuery);
$query = array_merge($pathQuery, $query);
}
$data = $params;
// Initialize PSR-7 Request and Response objects.
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
$content = http_build_query($params);
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
$data = $json;
}
$body = new SwooleStream($content);
$request = new Psr7Request($method, $uri, $headers, $body);
return $request->withQueryParams($query)
->withParsedBody($data)
->withUploadedFiles($this->normalizeFiles($multipart));
}
/**
* @deprecated It will be removed in v3.0
*/
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
{
return $this->initRequest($method, $path, $options);
}
protected function execute(ServerRequestInterface $psr7Request): ResponseInterface
{
$this->persistToContext($psr7Request, new Psr7Response());
@ -191,41 +235,6 @@ class Client extends Server
}
}
protected function init(string $method, string $path, array $options = []): ServerRequestInterface
{
$query = $options['query'] ?? [];
$params = $options['form_params'] ?? [];
$json = $options['json'] ?? [];
$headers = $options['headers'] ?? [];
$multipart = $options['multipart'] ?? [];
$parsePath = parse_url($path);
$path = $parsePath['path'];
$uriPathQuery = $parsePath['query'] ?? [];
if (! empty($uriPathQuery)) {
parse_str($uriPathQuery, $pathQuery);
$query = array_merge($pathQuery, $query);
}
$data = $params;
// Initialize PSR-7 Request and Response objects.
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
$content = http_build_query($params);
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {
$content = json_encode($json, JSON_UNESCAPED_UNICODE);
$data = $json;
}
$body = new SwooleStream($content);
$request = new Psr7Request($method, $uri, $headers, $body);
return $request->withQueryParams($query)
->withParsedBody($data)
->withUploadedFiles($this->normalizeFiles($multipart));
}
protected function normalizeFiles(array $multipart): array
{
$files = [];

View File

@ -28,6 +28,7 @@ use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Hyperf\Testing\Client;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Codec\Json;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Filesystem\Filesystem;
use Hyperf\Utils\Serializer\SimpleNormalizer;
@ -55,6 +56,21 @@ class ClientTest extends TestCase
$this->assertSame('Hello Hyperf!', $data['data']);
}
public function testSendCookies()
{
$container = $this->getContainer();
$client = new Client($container);
$response = $client->sendRequest($client->initRequest('POST', '/request')->withCookieParams([
'X-CODE' => $id = uniqid(),
]));
$data = Json::decode((string) $response->getBody());
$this->assertSame($id, $data['cookies']['X-CODE']);
}
public function testClientReturnCoroutineId()
{
$container = $this->getContainer();

View File

@ -34,6 +34,7 @@ class FooController
public function request()
{
/** @var ServerRequestInterface $request */
$request = Context::get(ServerRequestInterface::class);
$uri = $request->getUri();
return [
@ -45,6 +46,7 @@ class FooController
'query' => $uri->getQuery(),
],
'params' => $request->getQueryParams(),
'cookies' => $request->getCookieParams(),
];
}
}