diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index 917ba8936..12ed4cc31 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -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 diff --git a/src/testing/src/HttpClient.php b/src/testing/src/HttpClient.php index 4600622d1..35a2c6d91 100644 --- a/src/testing/src/HttpClient.php +++ b/src/testing/src/HttpClient.php @@ -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, ]); } diff --git a/src/testing/tests/HttpClientTest.php b/src/testing/tests/HttpClientTest.php new file mode 100644 index 000000000..eddbdbbf9 --- /dev/null +++ b/src/testing/tests/HttpClientTest.php @@ -0,0 +1,55 @@ +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); + } +}