Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactory = null
RequestFactoryInterface $requestFactory = null,
array $clientAgents = []
) {
$this->http = new Http\Client($url, $apiKey, $httpClient, $requestFactory);
$this->http = new Http\Client($url, $apiKey, $httpClient, $requestFactory, $clientAgents);
$this->index = new Indexes($this->http);
$this->health = new Health($this->http);
$this->version = new Version($this->http);
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $reqFactory = null
RequestFactoryInterface $reqFactory = null,
array $clientAgents = []
) {
$this->baseUrl = $url;
$this->apiKey = $apiKey;
$this->http = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = $reqFactory ?? Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->headers = array_filter([
'User-Agent' => MeiliSearch::qualifiedVersion(),
'User-Agent' => implode(';', array_merge($clientAgents, [MeiliSearch::qualifiedVersion()])),
]);
if (null != $this->apiKey) {
$this->headers['Authorization'] = sprintf('Bearer %s', $this->apiKey);
Expand Down
59 changes: 59 additions & 0 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,65 @@ public function testClientHasDefaultUserAgent(): void
$client->health();
}

public function testClientHasCustomUserAgent(): void
{
$customAgent = 'Meilisearch Symfony (v0.10.0)';
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

$requestStub->expects($this->any())
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('User-Agent'),
$this->equalTo($customAgent.';'.MeiliSearch::qualifiedVersion()),
],
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->method('createRequest')
->willReturn($requestStub);

$client = new \MeiliSearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory, [$customAgent]);

$client->health();
}

public function testClientHasEmptyCustomUserAgentArray(): void
{
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

$requestStub->expects($this->any())
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('User-Agent'),
$this->equalTo(MeiliSearch::qualifiedVersion()),
],
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->method('createRequest')
->willReturn($requestStub);

$client = new \MeiliSearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory, []);

$client->health();
}

public function testParseResponseReturnsNullForNoContent(): void
{
$response = $this->createMock(ResponseInterface::class);
Expand Down