|
19 | 19 | ) |
20 | 20 | from vws.exceptions.custom_exceptions import ( |
21 | 21 | RequestEntityTooLargeError, |
| 22 | + UnexpectedQueryResponseError, |
22 | 23 | ) |
| 24 | +from vws.response import Response |
23 | 25 |
|
24 | 26 |
|
25 | 27 | @pytest.mark.asyncio |
@@ -118,3 +120,59 @@ async def test_inactive_project( |
118 | 120 | response = exc.value.response |
119 | 121 | assert response.status_code == HTTPStatus.FORBIDDEN |
120 | 122 | assert response.tell_position != 0 |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.asyncio |
| 126 | +@pytest.mark.parametrize( |
| 127 | + argnames="response_text", |
| 128 | + argvalues=["", "not-json"], |
| 129 | +) |
| 130 | +async def test_unexpected_query_response( |
| 131 | + *, |
| 132 | + high_quality_image: io.BytesIO, |
| 133 | + response_text: str, |
| 134 | +) -> None: |
| 135 | + """ |
| 136 | + An ``UnexpectedQueryResponseError`` is raised for empty or non-JSON |
| 137 | + 4xx Cloud Query responses. |
| 138 | + """ |
| 139 | + |
| 140 | + class _NonJSONAsyncTransport: |
| 141 | + """Async transport that returns a non-JSON 4xx response.""" |
| 142 | + |
| 143 | + async def aclose(self) -> None: |
| 144 | + """Close the transport.""" |
| 145 | + |
| 146 | + async def __call__( |
| 147 | + self, |
| 148 | + *, |
| 149 | + method: str, |
| 150 | + url: str, |
| 151 | + headers: dict[str, str], |
| 152 | + data: bytes, |
| 153 | + request_timeout: float | tuple[float, float], |
| 154 | + ) -> Response: |
| 155 | + """Return a non-JSON error response.""" |
| 156 | + del method, headers, request_timeout |
| 157 | + return Response( |
| 158 | + text=response_text, |
| 159 | + url=url, |
| 160 | + status_code=HTTPStatus.BAD_REQUEST, |
| 161 | + headers={}, |
| 162 | + request_body=data, |
| 163 | + tell_position=0, |
| 164 | + content=response_text.encode(), |
| 165 | + ) |
| 166 | + |
| 167 | + async with AsyncCloudRecoService( |
| 168 | + client_access_key=uuid.uuid4().hex, |
| 169 | + client_secret_key=uuid.uuid4().hex, |
| 170 | + transport=_NonJSONAsyncTransport(), |
| 171 | + ) as async_cloud_reco_client: |
| 172 | + with pytest.raises( |
| 173 | + expected_exception=UnexpectedQueryResponseError, |
| 174 | + ) as exc: |
| 175 | + await async_cloud_reco_client.query(image=high_quality_image) |
| 176 | + |
| 177 | + assert exc.value.response.status_code == HTTPStatus.BAD_REQUEST |
| 178 | + assert exc.value.response.text == response_text |
0 commit comments