-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(avatar): Use Nextcloud HTTP client for favicons
Signed-off-by: Christoph Wurst <[email protected]>
- Loading branch information
1 parent
fb04c03
commit 1e98e6a
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
namespace OCA\Mail\Service\Avatar; | ||
|
||
use Exception; | ||
use OCA\Mail\Vendor\Favicon\DataAccess; | ||
use OCP\Http\Client\IClientService; | ||
|
||
class FaviconDataAccess extends DataAccess { | ||
|
||
public function __construct( | ||
private IClientService $clientService, | ||
) { | ||
} | ||
|
||
public function retrieveUrl($url) { | ||
$client = $this->clientService->newClient(); | ||
try { | ||
$response = $client->get($url); | ||
} catch (Exception $e) { | ||
// Ignore any error, like the parent method | ||
return false; | ||
} | ||
return $response->getBody(); | ||
} | ||
|
||
public function retrieveHeader($url) { | ||
$client = $this->clientService->newClient(); | ||
try { | ||
$response = $client->get($url, [ | ||
'allow_redirects' => [ | ||
'max' => 1, | ||
], | ||
]); | ||
} catch (Exception $e) { | ||
// Ignore any error, like the parent method | ||
return false; | ||
} | ||
// Build the data structure get_headers returns. The status reason | ||
// and protocol are inaccurate, but the favicon lib will only extract | ||
// the status code. | ||
return [ | ||
0 => 'HTTP/1.1 ' . $response->getStatusCode() . ' FOO', | ||
...array_change_key_case($response->getHeaders()), | ||
Check failure on line 50 in lib/Service/Avatar/FaviconDataAccess.php GitHub Actions / static-psalm-analysis dev-stable29DuplicateArrayKey
Check failure on line 50 in lib/Service/Avatar/FaviconDataAccess.php GitHub Actions / static-psalm-analysis dev-stable28DuplicateArrayKey
Check failure on line 50 in lib/Service/Avatar/FaviconDataAccess.php GitHub Actions / static-psalm-analysis dev-stable27DuplicateArrayKey
|
||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
namespace OCA\Mail\Service\Avatar; | ||
|
||
use ChristophWurst\Nextcloud\Testing\TestCase; | ||
use OCP\Http\Client\IClient; | ||
use OCP\Http\Client\IClientService; | ||
use OCP\Http\Client\IResponse; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
class FaviconDataAccessTest extends TestCase { | ||
|
||
private IClientService|MockObject $clientService; | ||
private FaviconDataAccess $dataAccess; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->clientService = $this->createMock(IClientService::class); | ||
|
||
$this->dataAccess = new FaviconDataAccess( | ||
$this->clientService, | ||
); | ||
} | ||
|
||
public function testRetrieveUrl() { | ||
$client = $this->createMock(IClient::class); | ||
$this->clientService->expects(self::once()) | ||
->method('newClient') | ||
->willReturn($client); | ||
$response = $this->createMock(IResponse::class); | ||
$client->expects(self::once()) | ||
->method('get') | ||
->with('https://localhost/favicon.ico', self::anything()) | ||
->willReturn($response); | ||
$response->method('getBody') | ||
->willReturn('html'); | ||
|
||
$body = $this->dataAccess->retrieveUrl('https://localhost/favicon.ico'); | ||
|
||
self::assertNotNull($body); | ||
self::assertSame('html', $body); | ||
} | ||
|
||
public function testRetrieveHeader() { | ||
$client = $this->createMock(IClient::class); | ||
$this->clientService->expects(self::once()) | ||
->method('newClient') | ||
->willReturn($client); | ||
$response = $this->createMock(IResponse::class); | ||
$client->expects(self::once()) | ||
->method('get') | ||
->with('https://localhost/favicon.ico', self::anything()) | ||
->willReturn($response); | ||
$response->method('getStatusCode') | ||
->willReturn(200); | ||
$response->method('getHeaders') | ||
->willReturn([ | ||
'Content-Type' => 'image/png', | ||
]); | ||
|
||
$headers = $this->dataAccess->retrieveHeader('https://localhost/favicon.ico'); | ||
|
||
self::assertIsArray($headers); | ||
self::assertSame( | ||
[ | ||
0 => 'HTTP/1.1 200 FOO', | ||
'content-type' => 'image/png', | ||
], | ||
$headers, | ||
); | ||
} | ||
} |