Skip to content

Commit

Permalink
Fixed userInfo in host
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Nov 7, 2023
1 parent a722bb6 commit 73a10db
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
36 changes: 36 additions & 0 deletions tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -477,4 +511,6 @@ public function testGetAsyncOnFailureReturnsDefault()
$onFailure = $this->transport->getAsyncOnFailure();
$this->assertInstanceOf(OnFailureDefault::class, $onFailure);
}


}

0 comments on commit 73a10db

Please sign in to comment.