|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +pytest.importorskip("openai", reason="openai is required for server tests") |
| 19 | +from fastapi.testclient import TestClient |
| 20 | + |
| 21 | +from nemoguardrails.server import api |
| 22 | + |
| 23 | +client = TestClient(api.app) |
| 24 | + |
| 25 | +ENDPOINT = "/v1/health" |
| 26 | + |
| 27 | + |
| 28 | +def test_health_returns_200_pass_status(): |
| 29 | + """GET /v1/health returns HTTP 200 with a JSON body of {"status": "pass"}.""" |
| 30 | + response = client.get(ENDPOINT) |
| 31 | + |
| 32 | + assert response.status_code == 200 |
| 33 | + assert response.json() == {"status": "pass"} |
| 34 | + |
| 35 | + |
| 36 | +def test_health_uses_health_json_media_type(): |
| 37 | + """GET /v1/health responds with exactly the application/health+json media type.""" |
| 38 | + response = client.get(ENDPOINT) |
| 39 | + |
| 40 | + assert response.headers["content-type"] == "application/health+json" |
| 41 | + |
| 42 | + |
| 43 | +def test_health_rejects_post_with_405(): |
| 44 | + """POST /v1/health returns HTTP 405 because the endpoint is GET-only.""" |
| 45 | + response = client.post(ENDPOINT) |
| 46 | + |
| 47 | + assert response.status_code == 405 |
| 48 | + |
| 49 | + |
| 50 | +def test_health_ok_without_config_or_cached_rails(monkeypatch): |
| 51 | + """GET /v1/health returns 200 with no default config and no cached rails instances.""" |
| 52 | + monkeypatch.setattr(api.app, "default_config_id", None) |
| 53 | + monkeypatch.setattr(api, "llm_rails_instances", {}) |
| 54 | + |
| 55 | + response = client.get(ENDPOINT) |
| 56 | + |
| 57 | + assert response.status_code == 200 |
| 58 | + assert response.json() == {"status": "pass"} |
| 59 | + |
| 60 | + |
| 61 | +def test_healthz_alias_matches_health_contract(): |
| 62 | + """GET /healthz returns the same 200 application/health+json {"status": "pass"} response as /v1/health.""" |
| 63 | + response = client.get("/healthz") |
| 64 | + |
| 65 | + assert response.status_code == 200 |
| 66 | + assert response.headers["content-type"] == "application/health+json" |
| 67 | + assert response.json() == {"status": "pass"} |
0 commit comments