From 42623748ef02492a91e209df5d063286cb6e0f37 Mon Sep 17 00:00:00 2001 From: Ryan Bibby Date: Thu, 4 Jul 2024 19:41:25 +0100 Subject: [PATCH 1/3] Allow HealthCheckJsonResultsController status code to be changed on failure --- config/health.php | 6 ++++++ .../HealthCheckJsonResultsController.php | 2 +- .../HealthCheckJsonResultsControllerTest.php | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/config/health.php b/config/health.php index dedf638a..c5dcffef 100644 --- a/config/health.php +++ b/config/health.php @@ -118,4 +118,10 @@ * in Horizon's silenced jobs screen. */ 'silence_health_queue_job' => true, + + /* + * The response code to use for HealthCheckJsonResultsController when a health + * check has failed + */ + 'json_results_failure_status' => 200, ]; diff --git a/src/Http/Controllers/HealthCheckJsonResultsController.php b/src/Http/Controllers/HealthCheckJsonResultsController.php index 897cb96e..38fc2191 100644 --- a/src/Http/Controllers/HealthCheckJsonResultsController.php +++ b/src/Http/Controllers/HealthCheckJsonResultsController.php @@ -18,7 +18,7 @@ public function __invoke(Request $request, ResultStore $resultStore): Response $checkResults = $resultStore->latestResults(); - return response($checkResults?->toJson() ?? '') + return response($checkResults?->toJson() ?? '', config('health.json_results_failure_status', 200)) ->header('Content-Type', 'application/json') ->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); } diff --git a/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php b/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php index c5e642db..7ae26442 100644 --- a/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php +++ b/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php @@ -56,3 +56,18 @@ expect($storedCheckResults)->toBeInstanceOf(StoredCheckResults::class) ->and($storedCheckResults->storedCheckResults)->toHaveCount(1); }); + +it('will return a 503 status for a unhealthy check', function () { + $this->check->replyWith(fn () => false); + + config()->set('health.json_results_failure_status', Response::HTTP_SERVICE_UNAVAILABLE); + + artisan(RunHealthChecksCommand::class); + + $json = getJson('/') + ->assertStatus(Response::HTTP_SERVICE_UNAVAILABLE) + ->json(); + + assertMatchesSnapshot($json); +}); + From a98336b450f73115993cbcbc59a0d51836a3dec8 Mon Sep 17 00:00:00 2001 From: Ryan Bibby Date: Thu, 4 Jul 2024 19:42:32 +0100 Subject: [PATCH 2/3] Update test description --- tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php b/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php index 7ae26442..e44079e9 100644 --- a/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php +++ b/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php @@ -57,7 +57,7 @@ ->and($storedCheckResults->storedCheckResults)->toHaveCount(1); }); -it('will return a 503 status for a unhealthy check', function () { +it('will return the configured status for a unhealthy check', function () { $this->check->replyWith(fn () => false); config()->set('health.json_results_failure_status', Response::HTTP_SERVICE_UNAVAILABLE); From 33275602fb4d412ed93904d0c32428ef4dc3efda Mon Sep 17 00:00:00 2001 From: Ryan Bibby Date: Thu, 4 Jul 2024 20:03:35 +0100 Subject: [PATCH 3/3] Update test snapshots and readme --- docs/viewing-results/as-json.md | 5 +++++ .../Controllers/HealthCheckJsonResultsControllerTest.php | 1 + tests/TestClasses/FakeUsedDiskSpaceCheck.php | 8 ++++++++ ...urn_the_configured_status_for_a_unhealthy_check__1.yml | 3 +++ 4 files changed, 17 insertions(+) create mode 100644 tests/__snapshots__/HealthCheckJsonResultsControllerTest__it_will_return_the_configured_status_for_a_unhealthy_check__1.yml diff --git a/docs/viewing-results/as-json.md b/docs/viewing-results/as-json.md index b8dafc3f..bacf2682 100644 --- a/docs/viewing-results/as-json.md +++ b/docs/viewing-results/as-json.md @@ -24,3 +24,8 @@ https://example.com/health?fresh ``` This way you'll see the latest results in the JSON. + +## Status codes + +By default, a 200 response will be returned regardless of the results. You can change the status code that +will be returned if a check fails using the `health.json_results_failure_status` config value. \ No newline at end of file diff --git a/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php b/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php index e44079e9..3ac9866f 100644 --- a/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php +++ b/tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php @@ -6,6 +6,7 @@ use Spatie\Health\Http\Controllers\HealthCheckJsonResultsController; use Spatie\Health\ResultStores\StoredCheckResults\StoredCheckResults; use Spatie\Health\Tests\TestClasses\FakeUsedDiskSpaceCheck; +use Symfony\Component\HttpFoundation\Response; use function Pest\Laravel\artisan; use function Pest\Laravel\getJson; diff --git a/tests/TestClasses/FakeUsedDiskSpaceCheck.php b/tests/TestClasses/FakeUsedDiskSpaceCheck.php index 664170be..eaad98d9 100644 --- a/tests/TestClasses/FakeUsedDiskSpaceCheck.php +++ b/tests/TestClasses/FakeUsedDiskSpaceCheck.php @@ -2,6 +2,7 @@ namespace Spatie\Health\Tests\TestClasses; +use Closure; use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck; class FakeUsedDiskSpaceCheck extends UsedDiskSpaceCheck @@ -24,4 +25,11 @@ public function getFilesystemName(): ?string { return $this->filesystemName; } + + public function replyWith(Closure $closure): self + { + $this->closure = $closure; + + return $this; + } } diff --git a/tests/__snapshots__/HealthCheckJsonResultsControllerTest__it_will_return_the_configured_status_for_a_unhealthy_check__1.yml b/tests/__snapshots__/HealthCheckJsonResultsControllerTest__it_will_return_the_configured_status_for_a_unhealthy_check__1.yml new file mode 100644 index 00000000..830d33f5 --- /dev/null +++ b/tests/__snapshots__/HealthCheckJsonResultsControllerTest__it_will_return_the_configured_status_for_a_unhealthy_check__1.yml @@ -0,0 +1,3 @@ +finishedAt: 1609459200 +checkResults: + - { name: FakeUsedDiskSpace, label: 'Fake Used Disk Space', notificationMessage: 'The disk is almost full (100% used).', shortSummary: 100%, status: failed, meta: { disk_space_used_percentage: 100 } }