Skip to content

Commit

Permalink
Fixed bug that could't get the valid uri when using `Hyperf\Testing…
Browse files Browse the repository at this point in the history
…\Client`. (#3356)
  • Loading branch information
limingxinleo authored Mar 11, 2021
1 parent dadffe6 commit 79b0a9f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace Hyperf\Testing;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\PackerInterface;
use Hyperf\Dispatcher\HttpDispatcher;
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
Expand Down Expand Up @@ -43,12 +44,18 @@ class Client extends Server
*/
protected $waitTimeout = 10.0;

/**
* @var string
*/
protected $baseUri = 'http://127.0.0.1/';

public function __construct(ContainerInterface $container, PackerInterface $packer = null, $server = 'http')
{
parent::__construct($container, $container->get(HttpDispatcher::class), $container->get(ExceptionHandlerDispatcher::class), $container->get(ResponseEmitter::class));
$this->packer = $packer ?? new JsonPacker();

$this->initCoreMiddleware($server);
$this->initBaseUri($server);
}

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

protected function initBaseUri($server): void
{
if ($this->container->has(ConfigInterface::class)) {
$config = $this->container->get(ConfigInterface::class);
$servers = $config->get('server.servers', []);
foreach ($servers as $item) {
if ($item['name'] == $server) {
$this->baseUri = sprintf('http://127.0.0.1:%d/', (int) $item['port']);
break;
}
}
}
}

protected function init(string $method, string $path, array $options = []): array
{
$query = $options['query'] ?? [];
Expand All @@ -166,7 +187,7 @@ protected function init(string $method, string $path, array $options = []): arra
$data = $params;

// Initialize PSR-7 Request and Response objects.
$uri = (new Uri())->withPath($path)->withQuery(http_build_query($query));
$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') {
Expand Down
38 changes: 38 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Hyperf\HttpServer\ResponseEmitter;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Router\Router;
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Hyperf\Testing\Client;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
Expand Down Expand Up @@ -78,6 +80,27 @@ public function testClientException()
$this->assertSame('Server Error', $data['message']);
}

public function testClientGetUri()
{
$container = $this->getContainer();

$client = new Client($container);

$data = $client->get('/request', [
'id' => $id = uniqid(),
]);

$this->assertSame($data['uri'], [
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => 9501,
'path' => '/request',
'query' => 'id=' . $id,
]);

$this->assertSame($id, $data['params']['id']);
}

public function getContainer()
{
$container = Mockery::mock(Container::class);
Expand All @@ -97,6 +120,20 @@ public function getContainer()
],
],
],
'server' => [
'servers' => [
[
'name' => 'http',
'type' => Server::SERVER_HTTP,
'host' => '0.0.0.0',
'port' => 9501,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
Event::ON_REQUEST => [Server::class, 'onRequest'],
],
],
],
],
]));
$container->shouldReceive('get')->with(Filesystem::class)->andReturn(new Filesystem());
$container->shouldReceive('get')->with(FooController::class)->andReturn(new FooController());
Expand All @@ -112,6 +149,7 @@ public function getContainer()
Router::get('/', [FooController::class, 'index']);
Router::get('/exception', [FooController::class, 'exception']);
Router::get('/id', [FooController::class, 'id']);
Router::addRoute(['GET', 'POST'], '/request', [FooController::class, 'request']);

return $container;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Stub/FooController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
*/
namespace HyperfTest\Testing\Stub;

use Hyperf\Utils\Context;
use Hyperf\Utils\Coroutine;
use Psr\Http\Message\ServerRequestInterface;

class FooController
{
Expand All @@ -29,4 +31,20 @@ public function id()
{
return ['code' => 0, 'data' => Coroutine::id()];
}

public function request()
{
$request = Context::get(ServerRequestInterface::class);
$uri = $request->getUri();
return [
'uri' => [
'scheme' => $uri->getScheme(),
'host' => $uri->getHost(),
'port' => $uri->getPort(),
'path' => $uri->getPath(),
'query' => $uri->getQuery(),
],
'params' => $request->getQueryParams(),
];
}
}

0 comments on commit 79b0a9f

Please sign in to comment.