Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow status code of Json results controller to be changed on failure #232

Merged
merged 4 commits into from
Jul 19, 2024
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
6 changes: 6 additions & 0 deletions config/health.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
5 changes: 5 additions & 0 deletions docs/viewing-results/as-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/Http/Controllers/HealthCheckJsonResultsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Http/Controllers/HealthCheckJsonResultsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,3 +57,18 @@
expect($storedCheckResults)->toBeInstanceOf(StoredCheckResults::class)
->and($storedCheckResults->storedCheckResults)->toHaveCount(1);
});

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);

artisan(RunHealthChecksCommand::class);

$json = getJson('/')
->assertStatus(Response::HTTP_SERVICE_UNAVAILABLE)
->json();

assertMatchesSnapshot($json);
});

8 changes: 8 additions & 0 deletions tests/TestClasses/FakeUsedDiskSpaceCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Health\Tests\TestClasses;

use Closure;
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;

class FakeUsedDiskSpaceCheck extends UsedDiskSpaceCheck
Expand All @@ -24,4 +25,11 @@ public function getFilesystemName(): ?string
{
return $this->filesystemName;
}

public function replyWith(Closure $closure): self
{
$this->closure = $closure;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -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 } }
Loading