Skip to content

Commit

Permalink
Merge pull request #232 from rbibby/main
Browse files Browse the repository at this point in the history
Allow status code of Json results controller to be changed on failure
  • Loading branch information
freekmurze authored Jul 19, 2024
2 parents b44e807 + 1505d28 commit 4041062
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
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 } }

0 comments on commit 4041062

Please sign in to comment.