|
11 | 11 | from typing import Any |
12 | 12 | from unittest.mock import AsyncMock, MagicMock, call, patch |
13 | 13 |
|
14 | | -from httpx import AsyncClient, Response |
| 14 | +from httpx import AsyncClient, RemoteProtocolError, Response |
15 | 15 |
|
16 | 16 | from pontos.nvd.api import ( |
17 | 17 | JSON, |
@@ -206,6 +206,43 @@ async def test_no_retry( |
206 | 206 | sleep_mock.assert_not_called() |
207 | 207 | self.assertFalse(result.is_server_error) |
208 | 208 |
|
| 209 | + @patch("pontos.nvd.api.asyncio.sleep", autospec=True) |
| 210 | + @patch("pontos.nvd.api.AsyncClient", spec=AsyncClient) |
| 211 | + async def test_exceed_attempts( |
| 212 | + self, |
| 213 | + async_client: MagicMock, |
| 214 | + sleep_mock: MagicMock, |
| 215 | + ): |
| 216 | + response_mocks = [ |
| 217 | + RemoteProtocolError("RIP connection"), |
| 218 | + MagicMock(spec=Response, is_server_error=True), |
| 219 | + ] |
| 220 | + http_client = AsyncMock() |
| 221 | + http_client.get.side_effect = response_mocks |
| 222 | + async_client.return_value = http_client |
| 223 | + |
| 224 | + api = NVDApi("https://foo.bar/baz", request_attempts=2) |
| 225 | + |
| 226 | + result = await api._get() |
| 227 | + |
| 228 | + calls = [call(2.0)] |
| 229 | + sleep_mock.assert_has_calls(calls) |
| 230 | + self.assertIsInstance(result, Response) |
| 231 | + |
| 232 | + @patch("pontos.nvd.api.AsyncClient", spec=AsyncClient) |
| 233 | + async def test_remote_protocol_error( |
| 234 | + self, |
| 235 | + async_client: MagicMock, |
| 236 | + ): |
| 237 | + http_client = AsyncMock() |
| 238 | + http_client.get.side_effect = RemoteProtocolError("RIP connection") |
| 239 | + async_client.return_value = http_client |
| 240 | + |
| 241 | + api = NVDApi("https://foo.bar/baz") |
| 242 | + |
| 243 | + with self.assertRaises(RemoteProtocolError): |
| 244 | + await api._get() |
| 245 | + |
209 | 246 |
|
210 | 247 | class Result: |
211 | 248 | def __init__(self, value: int) -> None: |
|
0 commit comments