Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Ssr/HttpGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ protected function ssrIsEnabled(): bool
*/
public function isHealthy(): bool
{
return Http::get($this->getUrl('/health'))->successful();
try {
return Http::get($this->getUrl('/health'))->successful();
} catch (Exception $e) {
if ($e instanceof StrayRequestException) {
throw $e;
}

return false;
}
}

/**
Expand Down
21 changes: 20 additions & 1 deletion tests/HttpGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Inertia\Tests;

use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Facades\Http;
use Inertia\Ssr\HttpGateway;

Expand Down Expand Up @@ -114,15 +118,30 @@ public function test_it_returns_null_when_invalid_json_is_returned(): void
$this->assertNull($this->gateway->dispatch(['page' => self::EXAMPLE_PAGE_OBJECT]));
}

/**
* Create a new connection exception for use during stubbing.
*
* This is copied over from Laravel's Http::failedConnection() helper
* method, which is only available in Laravel 11.32.0 and later.
*/
private static function rejectionForFailedConnection(): PromiseInterface
{
return Create::rejectionFor(
new ConnectException('Connection refused', new Request('GET', '/'))
);
}

public function test_health_check_the_ssr_server(): void
{
Http::fake([
$this->gateway->getUrl('health') => Http::sequence()
->push(status: 200)
->push(status: 500),
->push(status: 500)
->pushResponse(self::rejectionForFailedConnection()),
]);

$this->assertTrue($this->gateway->isHealthy());
$this->assertFalse($this->gateway->isHealthy());
$this->assertFalse($this->gateway->isHealthy());
}
}