Skip to content

Commit

Permalink
Merge branch 'master' into 3.0-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Jan 8, 2022
2 parents 5114a36 + 6337737 commit 2c816b4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 37 deletions.
81 changes: 45 additions & 36 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function file(string $uri, array $data = [], array $headers = [])
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);
}

Expand All @@ -145,6 +145,50 @@ public function sendRequest(ServerRequestInterface $psr7Request): ResponseInterf
}, $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());
Expand Down Expand Up @@ -188,41 +232,6 @@ protected function initBaseUri(string $server): void
}
}

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 = [];
Expand Down
5 changes: 4 additions & 1 deletion src/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

class Debug
{
public static function getRefCount($object): string
/**
* Get object's ref count.
*/
public static function getRefCount(object $object): string
{
ob_start();
debug_zval_dump($object);
Expand Down
16 changes: 16 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -55,6 +56,21 @@ public function testClientRequest()
$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();
Expand Down
36 changes: 36 additions & 0 deletions tests/DebugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Testing;

use Hyperf\Testing\Debug;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
class DebugTest extends TestCase
{
public function testGetRefCount()
{
$this->assertSame('1', Debug::getRefCount(new \stdClass()));
$obj = new \stdClass();
$this->assertSame('2', Debug::getRefCount($obj));
$obj2 = new \stdClass();
$obj2->obj = $obj;
$this->assertSame('2', Debug::getRefCount($obj2));
$this->assertSame('3', Debug::getRefCount($obj));
$fun = static function () {};
$this->assertSame('2', Debug::getRefCount($fun));
$this->assertSame('1', Debug::getRefCount(function () {}));
}
}
2 changes: 2 additions & 0 deletions tests/Stub/FooController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function id()

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

0 comments on commit 2c816b4

Please sign in to comment.