Skip to content

Commit 3dca89a

Browse files
authored
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
1 parent a8aa1a3 commit 3dca89a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/HttpClient.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use GuzzleHttp\Client;
1515
use Hyperf\Contract\PackerInterface;
16+
use Hyperf\Guzzle\CoroutineHandler;
1617
use Hyperf\Utils\Arr;
18+
use Hyperf\Utils\Coroutine;
1719
use Hyperf\Utils\Packer\JsonPacker;
1820
use Psr\Container\ContainerInterface;
1921

@@ -38,9 +40,14 @@ public function __construct(ContainerInterface $container, PackerInterface $pack
3840
{
3941
$this->container = $container;
4042
$this->packer = $packer ?? new JsonPacker();
43+
$handler = null;
44+
if (Coroutine::inCoroutine()) {
45+
$handler = new CoroutineHandler();
46+
}
4147
$this->client = new Client([
4248
'base_uri' => $baseUri,
4349
'timeout' => 2,
50+
'handler' => $handler,
4451
]);
4552
}
4653

tests/HttpClientTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace tests;
13+
14+
use Hyperf\Testing\HttpClient;
15+
use Mockery;
16+
use PHPUnit\Framework\TestCase;
17+
use Psr\Container\ContainerInterface;
18+
19+
/**
20+
* @internal
21+
* @coversNothing
22+
*/
23+
class HttpClientTest extends TestCase
24+
{
25+
protected function tearDown()
26+
{
27+
Mockery::close();
28+
}
29+
30+
/**
31+
* @group NonCoroutine
32+
*/
33+
public function testJsonRequest()
34+
{
35+
run(function () {
36+
$client = new HttpClient(Mockery::mock(ContainerInterface::class), null, 'http://127.0.0.1:4151');
37+
38+
$data = $client->get('/stats', [
39+
'format' => 'json',
40+
]);
41+
42+
$this->assertIsArray($data);
43+
}, SWOOLE_HOOK_ALL);
44+
45+
run(function () {
46+
$client = new HttpClient(Mockery::mock(ContainerInterface::class), null, 'http://127.0.0.1:4151');
47+
48+
$data = $client->get('/stats', [
49+
'format' => 'json',
50+
]);
51+
52+
$this->assertIsArray($data);
53+
}, SWOOLE_HOOK_ALL & ~SWOOLE_HOOK_CURL);
54+
}
55+
}

0 commit comments

Comments
 (0)