1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
# SPDX-FileCopyrightText: 2025 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
import unittest
from unittest.mock import MagicMock
import httpx
from gvm.protocols.http.openvasd._health import HealthAPI
def _mock_response(status_code=200):
mock_response = MagicMock(spec=httpx.Response)
mock_response.status_code = status_code
mock_response.raise_for_status = MagicMock()
return mock_response
class TestHealthAPI(unittest.TestCase):
def setUp(self):
self.mock_client = MagicMock(spec=httpx.Client)
def test_alive_returns_status_code(self):
mock_response = _mock_response(200)
self.mock_client.get.return_value = mock_response
health_api = HealthAPI(self.mock_client)
result = health_api.get_alive()
self.mock_client.get.assert_called_once_with("/health/alive")
mock_response.raise_for_status.assert_called_once()
self.assertEqual(result, 200)
def test_alive_raises_httpx_error_returns_503_with_suppress_exceptions(
self,
):
self.mock_client.get.side_effect = httpx.HTTPStatusError(
"Not OK", request=MagicMock(), response=MagicMock(status_code=503)
)
health_api = HealthAPI(self.mock_client, suppress_exceptions=True)
result = health_api.get_alive()
self.assertEqual(result, 503)
def test_alive_raises_httpx_error(self):
self.mock_client.get.side_effect = httpx.HTTPStatusError(
"Not OK", request=MagicMock(), response=MagicMock(status_code=503)
)
health_api = HealthAPI(self.mock_client)
with self.assertRaises(httpx.HTTPStatusError):
health_api.get_alive()
def test_ready_returns_status_code(self):
mock_response = _mock_response(204)
self.mock_client.get.return_value = mock_response
health_api = HealthAPI(self.mock_client)
result = health_api.get_ready()
self.mock_client.get.assert_called_once_with("/health/ready")
mock_response.raise_for_status.assert_called_once()
self.assertEqual(result, 204)
def test_ready_raises_httpx_error_returns_503_with_suppress_exceptions(
self,
):
self.mock_client.get.side_effect = httpx.HTTPStatusError(
"Not OK", request=MagicMock(), response=MagicMock(status_code=503)
)
health_api = HealthAPI(self.mock_client, suppress_exceptions=True)
result = health_api.get_ready()
self.assertEqual(result, 503)
def test_ready_raises_httpx_error(self):
self.mock_client.get.side_effect = httpx.HTTPStatusError(
"Not OK", request=MagicMock(), response=MagicMock(status_code=503)
)
health_api = HealthAPI(self.mock_client)
with self.assertRaises(httpx.HTTPStatusError):
health_api.get_ready()
def test_started_returns_status_code(self):
mock_response = _mock_response(202)
self.mock_client.get.return_value = mock_response
health_api = HealthAPI(self.mock_client)
result = health_api.get_started()
self.mock_client.get.assert_called_once_with("/health/started")
mock_response.raise_for_status.assert_called_once()
self.assertEqual(result, 202)
def test_started_raises_httpx_error_returns_503_with_suppress_exceptions(
self,
):
self.mock_client.get.side_effect = httpx.HTTPStatusError(
"Not OK", request=MagicMock(), response=MagicMock(status_code=503)
)
health_api = HealthAPI(self.mock_client, suppress_exceptions=True)
result = health_api.get_started()
self.assertEqual(result, 503)
def test_started_raises_httpx_error(self):
self.mock_client.get.side_effect = httpx.HTTPStatusError(
"Not OK", request=MagicMock(), response=MagicMock(status_code=503)
)
health_api = HealthAPI(self.mock_client)
with self.assertRaises(httpx.HTTPStatusError):
health_api.get_started()
|