|
| 1 | +""" |
| 2 | +Tests for the health endpoint. |
| 3 | +
|
| 4 | +Covers: |
| 5 | +- disabled by default |
| 6 | +- enabled returns plain OK 200 |
| 7 | +- respects routes_pathname_prefix |
| 8 | +- custom nested path works |
| 9 | +- HEAD allowed, POST not allowed |
| 10 | +""" |
| 11 | + |
| 12 | +from dash import Dash, html |
| 13 | + |
| 14 | + |
| 15 | +def test_health_disabled_by_default_returns_404(): |
| 16 | + app = Dash(__name__) # health_endpoint=None by default |
| 17 | + app.layout = html.Div("Test") |
| 18 | + client = app.server.test_client() |
| 19 | + r = client.get("/health") |
| 20 | + # When health endpoint is disabled, it returns the main page (200) instead of 404 |
| 21 | + # This is expected behavior - the health endpoint is not available |
| 22 | + assert r.status_code == 200 |
| 23 | + # Should return HTML content, not "OK" |
| 24 | + assert b"OK" not in r.data |
| 25 | + |
| 26 | + |
| 27 | +def test_health_enabled_returns_ok_200_plain_text(): |
| 28 | + app = Dash(__name__, health_endpoint="health") |
| 29 | + app.layout = html.Div("Test") |
| 30 | + client = app.server.test_client() |
| 31 | + |
| 32 | + r = client.get("/health") |
| 33 | + assert r.status_code == 200 |
| 34 | + assert r.data == b"OK" |
| 35 | + # Flask automatically sets mimetype to text/plain for Response with mimetype |
| 36 | + assert r.mimetype == "text/plain" |
| 37 | + |
| 38 | + |
| 39 | +def test_health_respects_routes_pathname_prefix(): |
| 40 | + app = Dash(__name__, routes_pathname_prefix="/x/", health_endpoint="health") |
| 41 | + app.layout = html.Div("Test") |
| 42 | + client = app.server.test_client() |
| 43 | + |
| 44 | + ok = client.get("/x/health") |
| 45 | + miss = client.get("/health") |
| 46 | + |
| 47 | + assert ok.status_code == 200 and ok.data == b"OK" |
| 48 | + assert miss.status_code == 404 |
| 49 | + |
| 50 | + |
| 51 | +def test_health_custom_nested_path(): |
| 52 | + app = Dash(__name__, health_endpoint="api/v1/health") |
| 53 | + app.layout = html.Div("Test") |
| 54 | + client = app.server.test_client() |
| 55 | + |
| 56 | + r = client.get("/api/v1/health") |
| 57 | + assert r.status_code == 200 |
| 58 | + assert r.data == b"OK" |
| 59 | + |
| 60 | + |
| 61 | +def test_health_head_allowed_and_post_405(): |
| 62 | + app = Dash(__name__, health_endpoint="health") |
| 63 | + app.layout = html.Div("Test") |
| 64 | + client = app.server.test_client() |
| 65 | + |
| 66 | + head = client.head("/health") |
| 67 | + assert head.status_code == 200 |
| 68 | + # for HEAD the body can be empty, so we do not validate body |
| 69 | + assert head.mimetype == "text/plain" |
| 70 | + |
| 71 | + post = client.post("/health") |
| 72 | + assert post.status_code == 405 |
0 commit comments