Files
pterodactyl-panel/tests/Unit/Exceptions/Http/Connection/DaemonConnectionExceptionTest.php
2026-10-12 05:18:57 -04:00

49 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Unit\Exceptions\Http\Connection\DaemonConnectionExceptionTest;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Monolog\Handler\TestHandler;
use Monolog\Level;
use Monolog\Logger;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Tests\TestCase;
uses(TestCase::class);
test('reports warning with request context for client errors', function () {
$handler = new TestHandler();
Log::swap(new \Illuminate\Log\Logger(new Logger('test', [$handler])));
$previous = Http::failedRequest(['error' => 'bad request'], 400, ['X-Request-Id' => 'request-123']);
(new DaemonConnectionException($previous))->report();
expect($handler->hasRecords(Level::Warning))->toBeTrue();
expect($handler->hasRecords(Level::Error))->toBeFalse();
$records = $handler->getRecords();
expect($records)->toHaveCount(1);
expect($records[0]->message)->toBe($previous->getMessage());
expect($records[0]->context['request_id'])->toBe('request-123');
expect($records[0]->context['exception'])->toBe($previous);
});
test('reports error for server errors', function () {
$handler = new TestHandler();
Log::swap(new \Illuminate\Log\Logger(new Logger('test', [$handler])));
$previous = Http::failedRequest([], 502);
(new DaemonConnectionException($previous))->report();
expect($handler->hasRecords(Level::Error))->toBeTrue();
expect($handler->hasRecords(Level::Warning))->toBeFalse();
$records = $handler->getRecords();
expect($records)->toHaveCount(1);
expect($records[0]->message)->toBe($previous->getMessage());
expect($records[0]->context['exception'])->toBe($previous);
});
test('is reported by the exception handler unlike other http exceptions', function () {
$handler = app(ExceptionHandler::class);
expect($handler->shouldReport(new DaemonConnectionException(Http::failedRequest([], 502))))->toBeTrue()
->and($handler->shouldReport(new DisplayException('Shown to the caller.')))->toBeFalse();
});