|
4 | 4 |
|
5 | 5 | import httpx |
6 | 6 |
|
7 | | -from ai21.errors import ServiceUnavailable |
| 7 | +from ai21.errors import ServiceUnavailable, Unauthorized |
8 | 8 | from ai21.http_client.base_http_client import RETRY_ERROR_CODES |
9 | 9 | from ai21.http_client.http_client import AI21HTTPClient |
10 | 10 | from ai21.http_client.async_http_client import AsyncAI21HTTPClient |
@@ -42,6 +42,17 @@ def test__execute_http_request__when_retry_error__should_retry_and_stop(mock_htt |
42 | 42 | assert mock_httpx_client.send.call_count == retries |
43 | 43 |
|
44 | 44 |
|
| 45 | +def test__execute_http_request__when_streaming__should_handle_non_200_response_code(mock_httpx_client: Mock) -> None: |
| 46 | + error_details = "test_error" |
| 47 | + request = Request(method=_METHOD, url=_URL) |
| 48 | + response = httpx.Response(status_code=401, request=request, text=error_details) |
| 49 | + mock_httpx_client.send.return_value = response |
| 50 | + |
| 51 | + client = AI21HTTPClient(client=mock_httpx_client, base_url=_URL, api_key=_API_KEY) |
| 52 | + with pytest.raises(Unauthorized, match=error_details): |
| 53 | + client.execute_http_request(method=_METHOD, stream=True) |
| 54 | + |
| 55 | + |
45 | 56 | @pytest.mark.asyncio |
46 | 57 | async def test__execute_async_http_request__when_retry_error_code_once__should_retry_and_succeed( |
47 | 58 | mock_httpx_async_client: Mock, |
@@ -74,3 +85,17 @@ async def test__execute_async_http_request__when_retry_error__should_retry_and_s |
74 | 85 | await client.execute_http_request(method=_METHOD) |
75 | 86 |
|
76 | 87 | assert mock_httpx_async_client.send.call_count == retries |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +async def test__execute_async_http_request__when_streaming__should_handle_non_200_response_code( |
| 92 | + mock_httpx_async_client: Mock, |
| 93 | +) -> None: |
| 94 | + error_details = "test_error" |
| 95 | + request = Request(method=_METHOD, url=_URL) |
| 96 | + response = httpx.Response(status_code=401, request=request, text=error_details) |
| 97 | + mock_httpx_async_client.send.return_value = response |
| 98 | + |
| 99 | + client = AsyncAI21HTTPClient(client=mock_httpx_async_client, base_url=_URL, api_key=_API_KEY) |
| 100 | + with pytest.raises(Unauthorized, match=error_details): |
| 101 | + await client.execute_http_request(method=_METHOD, stream=True) |
0 commit comments