Skip to content

Commit

Permalink
Merge #369
Browse files Browse the repository at this point in the history
369: Add the ability to provide extra custom user-agents r=brunoocasali a=brunoocasali

Related to meilisearch/integration-guides#150

Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
  • Loading branch information
bors[bot] and brunoocasali authored Aug 16, 2022
2 parents 43a5d1e + 07c67cf commit fb434e7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
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

0 comments on commit fb434e7

Please sign in to comment.