Skip to content

Commit

Permalink
Merge #585
Browse files Browse the repository at this point in the history
585: Fix passing empty masterkey r=norkunas a=mmachatschek

# Pull Request

## Related issue
Fixes #584

## What does this PR do?
- Fix the issue by also checking if the provided masterkey is an empty string

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Markus Machatschek <markus.machatschek@hey.com>
Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
  • Loading branch information
3 people authored Oct 25, 2023
2 parents 00ac8b6 + 0646c63 commit 048eb90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(
$this->headers = array_filter([
'User-Agent' => implode(';', array_merge($clientAgents, [Meilisearch::qualifiedVersion()])),
]);
if (null !== $apiKey) {
if (null !== $apiKey && '' !== $apiKey) {
$this->headers['Authorization'] = sprintf('Bearer %s', $apiKey);
}
$this->json = new Json();
Expand Down
22 changes: 22 additions & 0 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ public function testClientHasDefaultUserAgent(): void
$this->assertTrue($client->isHealthy());
}

public function testEmptyMasterkeyRemovesAuthHeader(): void
{
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);
$requestStub->expects($this->once())
->method('withAddedHeader')
->willReturnCallback(function ($name, $value) use ($requestStub) {
$this->assertSame('User-Agent', $name);
$this->assertSame(Meilisearch::qualifiedVersion(), $value);

return $requestStub;
});
$reqFactory->expects($this->once())
->method('createRequest')
->willReturn($requestStub);

$client = new \Meilisearch\Client('http://localhost:7070', '', $httpClient, $reqFactory);

$this->assertTrue($client->isHealthy());
}

public function testClientHasCustomUserAgent(): void
{
$customAgent = 'Meilisearch Symfony (v0.10.0)';
Expand Down

0 comments on commit 048eb90

Please sign in to comment.