File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ import pytest
2+ from fastapi import FastAPI
3+ from fastapi .testclient import TestClient
4+ from typing import Awaitable
5+
6+ from health_check import HealthCheckMiddleware , HealthCheckResult
7+
8+ # A sample health check function
9+ async def sample_check () -> HealthCheckResult :
10+ return HealthCheckResult (True , "Sample check passed" )
11+
12+ # A failing health check function
13+ async def failing_check () -> HealthCheckResult :
14+ return HealthCheckResult (False , "Sample check failed" )
15+
16+ def create_app ():
17+ app = FastAPI ()
18+
19+ checks = {
20+ "SampleCheck" : sample_check ,
21+ "FailingCheck" : failing_check ,
22+ }
23+
24+ app .add_middleware (HealthCheckMiddleware , checks = checks , password = "testpassword" )
25+
26+ @app .get ("/" )
27+ async def read_root ():
28+ return {"message" : "Hello World" }
29+
30+ return app
31+
32+ @pytest .fixture
33+ def client ():
34+ app = create_app ()
35+ return TestClient (app )
36+
37+ def test_healthcheck_default (client ):
38+ response = client .get ("/healthz" )
39+ assert response .status_code == 503 # Failing check should result in 503
40+ assert response .text == "Service Unavailable"
41+
42+
43+ def test_non_healthz_endpoint (client ):
44+ response = client .get ("/" )
45+ assert response .status_code == 200
46+ assert response .json () == {"message" : "Hello World" }
You can’t perform that action at this time.
0 commit comments