From 73a10dbfc4769fcb47786b6c13ae13f4d64e739a Mon Sep 17 00:00:00 2001 From: Enrico Zimuel Date: Tue, 7 Nov 2023 14:43:45 +0100 Subject: [PATCH] Fixed userInfo in host --- src/Transport.php | 10 +++++++++- tests/TransportTest.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Transport.php b/src/Transport.php index faed39b..28eb0f3 100644 --- a/src/Transport.php +++ b/src/Transport.php @@ -221,7 +221,15 @@ private function setupConnectionUri(Node $node, RequestInterface $request): Requ if (!empty($nodePath)) { $path = sprintf("%s/%s", rtrim($nodePath, '/'), ltrim($path,'/')); } - + // If the user information is not in the request, we check if it is present in the node uri + // @see https://github.com/elastic/elastic-transport-php/issues/18 + if (empty($request->getUri()->getUserInfo()) && !empty($uri->getUserInfo())) { + $userInfo = explode(':', $uri->getUserInfo()); + $request = $request->withUri( + $request->getUri() + ->withUserInfo($userInfo[0], $userInfo[1] ?? null) + ); + } return $request->withUri( $request->getUri() ->withHost($uri->getHost()) diff --git a/tests/TransportTest.php b/tests/TransportTest.php index c6ec241..db2b21d 100644 --- a/tests/TransportTest.php +++ b/tests/TransportTest.php @@ -427,6 +427,40 @@ public function testSetRetries() $this->assertEquals(1, $this->transport->getRetries()); } + public function testSendRequestWithUserAndPasswordInHost() + { + $url = 'http://user:password@localhost/subfolder'; + $expectedResponse = $this->responseFactory->createResponse(200); + $this->client->addResponse($expectedResponse); + + $this->node->method('getUri')->willReturn($this->uriFactory->createUri($url)); + $this->nodePool->method('nextNode')->willReturn($this->node); + + $request = $this->requestFactory->createRequest('GET', '/'); + $response = $this->transport->sendRequest($request); + + $lastRequest = $this->client->getLastRequest(); + + $this->assertEquals('user:password', $lastRequest->getUri()->getUserInfo()); + } + + public function testSendRequestWithUserInHost() + { + $url = 'http://user@localhost/subfolder'; + $expectedResponse = $this->responseFactory->createResponse(200); + $this->client->addResponse($expectedResponse); + + $this->node->method('getUri')->willReturn($this->uriFactory->createUri($url)); + $this->nodePool->method('nextNode')->willReturn($this->node); + + $request = $this->requestFactory->createRequest('GET', '/'); + $response = $this->transport->sendRequest($request); + + $lastRequest = $this->client->getLastRequest(); + + $this->assertEquals('user', $lastRequest->getUri()->getUserInfo()); + } + /** * @group async */ @@ -477,4 +511,6 @@ public function testGetAsyncOnFailureReturnsDefault() $onFailure = $this->transport->getAsyncOnFailure(); $this->assertInstanceOf(OnFailureDefault::class, $onFailure); } + + } \ No newline at end of file