Files
laravel-video-extractor/tests/Unit/HttpClientTest.php
2026-07-16 21:56:58 +03:00

91 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Apoloz\VideoExtractor\Tests\Unit;
use Apoloz\VideoExtractor\Http\HttpClient;
use Apoloz\VideoExtractor\Tests\TestCase;
use Illuminate\Support\Facades\Http;
class HttpClientTest extends TestCase
{
public function test_converts_cp1251_body_to_utf8_from_header(): void
{
// Cyrillic "Трамп" encoded as Windows-1251 bytes.
$cp1251 = "\xd2\xf0\xe0\xec\xef";
Http::fake([
'example.com/*' => Http::response(
$cp1251,
200,
['Content-Type' => 'application/json; charset=windows-1251'],
),
]);
$client = new HttpClient();
$response = $client->get('https://example.com/test');
self::assertSame('Трамп', $client->bodyUtf8($response));
}
public function test_force_charset_overrides_header_detection(): void
{
// "Алена" encoded as Windows-1251 bytes, but the server declares UTF-8.
$cp1251 = "\xc0\xeb\xe5\xed\xe0";
Http::fake([
'example.com/*' => Http::response(
$cp1251,
200,
['Content-Type' => 'application/json; charset=utf-8'],
),
]);
$client = new HttpClient();
$response = $client->get('https://example.com/test');
// Forcing cp1251 recovers correct Cyrillic regardless of the header.
self::assertSame('Алена', $client->bodyUtf8($response, 'Windows-1251'));
}
public function test_leaves_utf8_body_untouched(): void
{
Http::fake([
'example.com/*' => Http::response('Привет', 200, ['Content-Type' => 'application/json; charset=utf-8']),
]);
$client = new HttpClient();
$response = $client->get('https://example.com/test');
self::assertSame('Привет', $client->bodyUtf8($response));
}
public function test_returns_body_verbatim_when_no_charset(): void
{
Http::fake([
'example.com/*' => Http::response('plain ascii', 200, ['Content-Type' => 'text/html']),
]);
$client = new HttpClient();
$response = $client->get('https://example.com/test');
self::assertSame('plain ascii', $client->bodyUtf8($response));
}
public function test_detects_challenge_redirect(): void
{
// VK answers 429 on the challenge URL itself; in Http::fake the
// effective URI mirrors the request URL, so we hit it directly.
Http::fake([
'vk.com/challenge.html*' => Http::response('challenge', 429),
]);
$client = new HttpClient();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('JS challenge');
$client->get('https://vk.com/challenge.html?hash429=abc');
}
}