Skip to content

Commit

Permalink
Merge pull request #21 from mikevrind/add/path-to-connection-uri
Browse files Browse the repository at this point in the history
Add/path to connection uri
  • Loading branch information
ezimuel authored Nov 7, 2023
2 parents 4d7937f + 491a889 commit a722bb6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
level: max
level: 8
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
paths:
Expand Down
9 changes: 9 additions & 0 deletions src/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,21 @@ private function setupUserInfo(RequestInterface $request): RequestInterface
private function setupConnectionUri(Node $node, RequestInterface $request): RequestInterface
{
$uri = $node->getUri();
$path = $request->getUri()->getPath();

$nodePath = $uri->getPath();
// If the node has a path we need to use it as prefix for the existing path
// @see https://github.com/elastic/elastic-transport-php/pull/20
if (!empty($nodePath)) {
$path = sprintf("%s/%s", rtrim($nodePath, '/'), ltrim($path,'/'));
}

return $request->withUri(
$request->getUri()
->withHost($uri->getHost())
->withPort($uri->getPort())
->withScheme($uri->getScheme())
->withPath($path)
);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ public function testSendRequestWith200Response()
$this->assertEquals($expectedResponse, $response);
}

public function testSendRequestWithUriThatContainsAPath()
{
$url = 'http://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', '/test');
$response = $this->transport->sendRequest($request);

$this->assertEquals($url . '/test', (string) $this->client->getLastRequest()->getUri());
$this->assertInstanceOf(ResponseInterface::class, $response);
$this->assertEquals($expectedResponse, $response);
}

public function testSendRequestWithNetworkException()
{
$request = $this->requestFactory->createRequest('GET', '/');
Expand Down

0 comments on commit a722bb6

Please sign in to comment.