Skip to content

Commit 79b0a9f

Browse files
authored
Fixed bug that could't get the valid uri when using Hyperf\Testing\Client. (#3356)
1 parent dadffe6 commit 79b0a9f

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/Client.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace Hyperf\Testing;
1313

14+
use Hyperf\Contract\ConfigInterface;
1415
use Hyperf\Contract\PackerInterface;
1516
use Hyperf\Dispatcher\HttpDispatcher;
1617
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
@@ -43,12 +44,18 @@ class Client extends Server
4344
*/
4445
protected $waitTimeout = 10.0;
4546

47+
/**
48+
* @var string
49+
*/
50+
protected $baseUri = 'http://127.0.0.1/';
51+
4652
public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http')
4753
{
4854
parent::__construct($container, $container->get(HttpDispatcher::class), $container->get(ExceptionHandlerDispatcher::class), $container->get(ResponseEmitter::class));
4955
$this->packer = $packer ?? new JsonPacker();
5056

5157
$this->initCoreMiddleware($server);
58+
$this->initBaseUri($server);
5259
}
5360

5461
public function get($uri, $data = [], $headers = [])
@@ -155,6 +162,20 @@ public function request(string $method, string $path, array $options = [])
155162
}, $this->waitTimeout);
156163
}
157164

165+
protected function initBaseUri($server): void
166+
{
167+
if ($this->container->has(ConfigInterface::class)) {
168+
$config = $this->container->get(ConfigInterface::class);
169+
$servers = $config->get('server.servers', []);
170+
foreach ($servers as $item) {
171+
if ($item['name'] == $server) {
172+
$this->baseUri = sprintf('http://127.0.0.1:%d/', (int) $item['port']);
173+
break;
174+
}
175+
}
176+
}
177+
}
178+
158179
protected function init(string $method, string $path, array $options = []): array
159180
{
160181
$query = $options['query'] ?? [];
@@ -166,7 +187,7 @@ protected function init(string $method, string $path, array $options = []): arra
166187
$data = $params;
167188

168189
// Initialize PSR-7 Request and Response objects.
169-
$uri = (new Uri())->withPath($path)->withQuery(http_build_query($query));
190+
$uri = (new Uri($this->baseUri . ltrim($path, '/')))->withQuery(http_build_query($query));
170191

171192
$content = http_build_query($params);
172193
if ($method == 'POST' && data_get($headers, 'Content-Type') == 'application/json') {

tests/ClientTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Hyperf\HttpServer\ResponseEmitter;
2525
use Hyperf\HttpServer\Router\DispatcherFactory;
2626
use Hyperf\HttpServer\Router\Router;
27+
use Hyperf\Server\Event;
28+
use Hyperf\Server\Server;
2729
use Hyperf\Testing\Client;
2830
use Hyperf\Utils\ApplicationContext;
2931
use Hyperf\Utils\Coroutine;
@@ -78,6 +80,27 @@ public function testClientException()
7880
$this->assertSame('Server Error', $data['message']);
7981
}
8082

83+
public function testClientGetUri()
84+
{
85+
$container = $this->getContainer();
86+
87+
$client = new Client($container);
88+
89+
$data = $client->get('/request', [
90+
'id' => $id = uniqid(),
91+
]);
92+
93+
$this->assertSame($data['uri'], [
94+
'scheme' => 'http',
95+
'host' => '127.0.0.1',
96+
'port' => 9501,
97+
'path' => '/request',
98+
'query' => 'id=' . $id,
99+
]);
100+
101+
$this->assertSame($id, $data['params']['id']);
102+
}
103+
81104
public function getContainer()
82105
{
83106
$container = Mockery::mock(Container::class);
@@ -97,6 +120,20 @@ public function getContainer()
97120
],
98121
],
99122
],
123+
'server' => [
124+
'servers' => [
125+
[
126+
'name' => 'http',
127+
'type' => Server::SERVER_HTTP,
128+
'host' => '0.0.0.0',
129+
'port' => 9501,
130+
'sock_type' => SWOOLE_SOCK_TCP,
131+
'callbacks' => [
132+
Event::ON_REQUEST => [Server::class, 'onRequest'],
133+
],
134+
],
135+
],
136+
],
100137
]));
101138
$container->shouldReceive('get')->with(Filesystem::class)->andReturn(new Filesystem());
102139
$container->shouldReceive('get')->with(FooController::class)->andReturn(new FooController());
@@ -112,6 +149,7 @@ public function getContainer()
112149
Router::get('/', [FooController::class, 'index']);
113150
Router::get('/exception', [FooController::class, 'exception']);
114151
Router::get('/id', [FooController::class, 'id']);
152+
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);
115153

116154
return $container;
117155
}

tests/Stub/FooController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
*/
1212
namespace HyperfTest\Testing\Stub;
1313

14+
use Hyperf\Utils\Context;
1415
use Hyperf\Utils\Coroutine;
16+
use Psr\Http\Message\ServerRequestInterface;
1517

1618
class FooController
1719
{
@@ -29,4 +31,20 @@ public function id()
2931
{
3032
return ['code' => 0, 'data' => Coroutine::id()];
3133
}
34+
35+
public function request()
36+
{
37+
$request = Context::get(ServerRequestInterface::class);
38+
$uri = $request->getUri();
39+
return [
40+
'uri' => [
41+
'scheme' => $uri->getScheme(),
42+
'host' => $uri->getHost(),
43+
'port' => $uri->getPort(),
44+
'path' => $uri->getPath(),
45+
'query' => $uri->getQuery(),
46+
],
47+
'params' => $request->getQueryParams(),
48+
];
49+
}
3250
}

0 commit comments

Comments
 (0)