From ebb916e2d0618c98e45fc1071d7b72f7ee8acc98 Mon Sep 17 00:00:00 2001 From: Enrico Zimuel Date: Wed, 10 Mar 2021 12:46:34 +0100 Subject: [PATCH] Fixed issue with query params in sendRequest --- src/Transport.php | 12 +++++++++-- tests/TransportTest.php | 47 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Transport.php b/src/Transport.php index ec561c6..39c5c42 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -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 diff --git a/tests/TransportTest.php b/tests/TransportTest.php index c95bd26..4525339 100644 --- a/tests/TransportTest.php +++ b/tests/TransportTest.php @@ -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()