|
7 | 7 | from http import HTTPStatus |
8 | 8 |
|
9 | 9 | import pytest |
10 | | -from mock_vws import MockVWS |
| 10 | +from mock_vws import CloudQueryFailureResponse, MockVWS |
11 | 11 | from mock_vws.database import CloudDatabase |
12 | 12 | from mock_vws.states import States |
13 | 13 |
|
14 | 14 | from vws import AsyncCloudRecoService |
| 15 | +from vws.exceptions.base_exceptions import CloudRecoError |
15 | 16 | from vws.exceptions.cloud_reco_exceptions import ( |
16 | 17 | AuthenticationFailureError, |
17 | 18 | InactiveProjectError, |
@@ -118,3 +119,52 @@ async def test_inactive_project( |
118 | 119 | response = exc.value.response |
119 | 120 | assert response.status_code == HTTPStatus.FORBIDDEN |
120 | 121 | assert response.tell_position != 0 |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize( |
| 125 | + argnames=("body", "headers"), |
| 126 | + argvalues=[ |
| 127 | + ("", {"X-Query-Failure": "empty"}), |
| 128 | + ( |
| 129 | + "Arbitrary upstream failure", |
| 130 | + { |
| 131 | + "Content-Type": "application/json", |
| 132 | + "X-Query-Failure": "text", |
| 133 | + }, |
| 134 | + ), |
| 135 | + ], |
| 136 | + ids=["empty", "arbitrary-text"], |
| 137 | +) |
| 138 | +@pytest.mark.asyncio |
| 139 | +async def test_non_json_client_error( |
| 140 | + *, |
| 141 | + high_quality_image: io.BytesIO, |
| 142 | + body: str, |
| 143 | + headers: dict[str, str], |
| 144 | +) -> None: |
| 145 | + """Non-JSON 4xx responses raise a response-carrying error.""" |
| 146 | + database = CloudDatabase() |
| 147 | + failure_response = CloudQueryFailureResponse( |
| 148 | + status_code=HTTPStatus.BAD_REQUEST, |
| 149 | + headers=headers, |
| 150 | + body=body, |
| 151 | + ) |
| 152 | + cloud_reco_client = AsyncCloudRecoService( |
| 153 | + client_access_key=database.client_access_key, |
| 154 | + client_secret_key=database.client_secret_key, |
| 155 | + ) |
| 156 | + |
| 157 | + with MockVWS(cloud_query_failure_response=failure_response) as mock: |
| 158 | + mock.add_cloud_database(cloud_database=database) |
| 159 | + with pytest.raises(expected_exception=CloudRecoError) as exc: |
| 160 | + await cloud_reco_client.query(image=high_quality_image) |
| 161 | + |
| 162 | + response = exc.value.response |
| 163 | + assert response.status_code == HTTPStatus.BAD_REQUEST |
| 164 | + assert response.text == body |
| 165 | + assert response.content == body.encode() |
| 166 | + response_headers = { |
| 167 | + key.lower(): value for key, value in response.headers.items() |
| 168 | + } |
| 169 | + assert response_headers["x-query-failure"] == headers["X-Query-Failure"] |
| 170 | + assert response.request_body |
0 commit comments