From 07c67cfed21de86e63c9ae5be4bb797c47bf7672 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Fri, 12 Aug 2022 18:18:48 -0300 Subject: [PATCH] Add the ability to provide extra custom user-agents --- src/Client.php | 5 ++-- src/Http/Client.php | 5 ++-- tests/Http/ClientTest.php | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index ff8cb271..9d7a2574 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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); diff --git a/src/Http/Client.php b/src/Http/Client.php index 65a1dc63..d07ebce1 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -39,7 +39,8 @@ 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; @@ -47,7 +48,7 @@ public function __construct( $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); diff --git a/tests/Http/ClientTest.php b/tests/Http/ClientTest.php index 433d95e3..3965f484 100644 --- a/tests/Http/ClientTest.php +++ b/tests/Http/ClientTest.php @@ -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);