Skip to content

Commit

Permalink
Fixed issue with query params in sendRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Mar 10, 2021
1 parent 0a47602 commit ebb916e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,17 @@ public function sendRequest(RequestInterface $request): ResponseInterface
{
// Set the host if empty
if (empty($request->getUri()->getHost())) {
$path = $request->getUri()->getPath();
$connection = $this->connectionPool->nextConnection();
$request = $request->withUri($connection->getUri()->withPath($path));
$host = $connection->getUri()->getHost();
$port = $connection->getUri()->getPort();
$scheme = $connection->getUri()->getScheme();

$request = $request->withUri(
$request->getUri()
->withHost($host)
->withPort($port)
->withScheme($scheme)
);
}

// Set the global headers, if not already set
Expand Down
47 changes: 46 additions & 1 deletion tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,62 @@ public function testSendRequestWithEmptyHost()
$this->assertEquals('localhost', $lastRequest->getUri()->getHost());
}

public function testSendRequestWithQueryAndEmptyHost()
{
$expectedResponse = new Response(200);
$this->mock->append($expectedResponse);

$this->connection->method('getUri')
->willReturn(new Uri('http://localhost'));

$request = new Request('GET', '/');
$request = $request->withUri($request->getUri()->withQuery('name=test'));
$response = $this->transport->sendRequest($request);

$lastRequest = $this->transport->getLastRequest();
$this->assertEquals('name=test', $lastRequest->getUri()->getQuery());
}

public function testSendRequestWithQueryAndHost()
{
$expectedResponse = new Response(200);
$this->mock->append($expectedResponse);

$request = new Request('GET', 'http://localhost');
$request = $request->withUri($request->getUri()->withQuery('name=test'));
$response = $this->transport->sendRequest($request);

$lastRequest = $this->transport->getLastRequest();
$this->assertEquals('name=test', $lastRequest->getUri()->getQuery());
}

public function testSendRequestWithHost()
{
$expectedResponse = new Response(200);
$this->mock->append($expectedResponse);

$request = new Request('GET', 'http://domain/path');
$request = new Request('GET', 'https://domain/path');
$response = $this->transport->sendRequest($request);

$lastRequest = $this->transport->getLastRequest();
$this->assertEquals('https', $lastRequest->getUri()->getScheme());
$this->assertEquals('domain', $lastRequest->getUri()->getHost());
$this->assertEquals('/path', $lastRequest->getUri()->getPath());
}

public function testSendRequestWithHostAndPort()
{
$expectedResponse = new Response(200);
$this->mock->append($expectedResponse);

$uri = new Uri('http://domain/path');
$request = new Request('GET', $uri->withPort(9200));
$response = $this->transport->sendRequest($request);

$lastRequest = $this->transport->getLastRequest();
$this->assertEquals('domain', $lastRequest->getUri()->getHost());
$this->assertEquals('/path', $lastRequest->getUri()->getPath());
$this->assertEquals(9200, $lastRequest->getUri()->getPort());
}

public function testLoggerWithSendRequest()
Expand Down

0 comments on commit ebb916e

Please sign in to comment.