Fixed http client does not works when using guzzle 7.0 and curl hook for hyperf/testing. (#2624)

* Update HttpClient.php

* Create HttpClientTest.php

* Update HttpClientTest.php

* Update CHANGELOG-2.0.md
This commit is contained in:
李铭昕 2020-10-10 17:22:32 +08:00 committed by GitHub
parent df2ab77895
commit 38d12e4e5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 0 deletions

View File

@ -11,6 +11,7 @@
- [#2594](https://github.com/hyperf/hyperf/pull/2594) Fixed crontab does not stops when using signal.
- [#2601](https://github.com/hyperf/hyperf/pull/2601) Fixed `@property` will be replaced by `@property-read` when the property has `getter` and `setter` at the same time.
- [#2607](https://github.com/hyperf/hyperf/pull/2607) Fixed memory leak in `RetryAnnotationAspect`.
- [#2624](https://github.com/hyperf/hyperf/pull/2624) Fixed http client does not works when using guzzle 7.0 and curl hook for `hyperf/testing`.
## Optimized

View File

@ -13,7 +13,9 @@ namespace Hyperf\Testing;
use GuzzleHttp\Client;
use Hyperf\Contract\PackerInterface;
use Hyperf\Guzzle\CoroutineHandler;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Packer\JsonPacker;
use Psr\Container\ContainerInterface;
@ -38,9 +40,14 @@ class HttpClient
{
$this->container = $container;
$this->packer = $packer ?? new JsonPacker();
$handler = null;
if (Coroutine::inCoroutine()) {
$handler = new CoroutineHandler();
}
$this->client = new Client([
'base_uri' => $baseUri,
'timeout' => 2,
'handler' => $handler,
]);
}

View File

@ -0,0 +1,55 @@
<?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 tests;
use Hyperf\Testing\HttpClient;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
/**
* @internal
* @coversNothing
*/
class HttpClientTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
}
/**
* @group NonCoroutine
*/
public function testJsonRequest()
{
run(function () {
$client = new HttpClient(Mockery::mock(ContainerInterface::class), null, 'http://127.0.0.1:4151');
$data = $client->get('/stats', [
'format' => 'json',
]);
$this->assertIsArray($data);
}, SWOOLE_HOOK_ALL);
run(function () {
$client = new HttpClient(Mockery::mock(ContainerInterface::class), null, 'http://127.0.0.1:4151');
$data = $client->get('/stats', [
'format' => 'json',
]);
$this->assertIsArray($data);
}, SWOOLE_HOOK_ALL & ~SWOOLE_HOOK_CURL);
}
}