|
1 | 1 | """Tests for HTTP transport implementations.""" |
2 | 2 |
|
| 3 | +import io |
| 4 | +import uuid |
3 | 5 | from http import HTTPStatus |
4 | 6 |
|
5 | 7 | import httpx |
6 | 8 | import pytest |
7 | 9 | import respx |
8 | 10 |
|
| 11 | +from vws import ( |
| 12 | + VWS, |
| 13 | + AsyncCloudRecoService, |
| 14 | + AsyncVuMarkService, |
| 15 | + AsyncVWS, |
| 16 | + CloudRecoService, |
| 17 | + VuMarkService, |
| 18 | +) |
9 | 19 | from vws.response import Response |
10 | 20 | from vws.transports import AsyncHTTPXTransport, HTTPXTransport |
| 21 | +from vws.vumark_accept import VuMarkAccept |
11 | 22 |
|
12 | 23 |
|
13 | 24 | class TestHTTPXTransport: |
@@ -206,3 +217,193 @@ async def test_context_manager() -> None: |
206 | 217 | assert route.called |
207 | 218 | assert isinstance(response, Response) |
208 | 219 | assert response.status_code == HTTPStatus.OK |
| 220 | + |
| 221 | + |
| 222 | +class _FalsyTransport: |
| 223 | + """A sync transport that is falsy but protocol-conforming.""" |
| 224 | + |
| 225 | + def __bool__(self) -> bool: |
| 226 | + """Return ``False`` so truthiness checks would skip this |
| 227 | + transport. |
| 228 | + """ |
| 229 | + return False |
| 230 | + |
| 231 | + def close(self) -> None: |
| 232 | + """Close the transport.""" |
| 233 | + |
| 234 | + def __call__( |
| 235 | + self, |
| 236 | + *, |
| 237 | + method: str, |
| 238 | + url: str, |
| 239 | + headers: dict[str, str], |
| 240 | + data: bytes, |
| 241 | + request_timeout: float | tuple[float, float], |
| 242 | + ) -> Response: |
| 243 | + """Return a successful API response for the requested URL.""" |
| 244 | + del method, headers, request_timeout |
| 245 | + if url.endswith("/query"): |
| 246 | + body = '{"result_code":"Success","results":[]}' |
| 247 | + return Response( |
| 248 | + text=body, |
| 249 | + url=url, |
| 250 | + status_code=HTTPStatus.OK, |
| 251 | + headers={}, |
| 252 | + request_body=data, |
| 253 | + tell_position=0, |
| 254 | + content=body.encode(), |
| 255 | + ) |
| 256 | + if "/instances" in url: |
| 257 | + content = b"vumark-bytes" |
| 258 | + return Response( |
| 259 | + text="", |
| 260 | + url=url, |
| 261 | + status_code=HTTPStatus.OK, |
| 262 | + headers={}, |
| 263 | + request_body=data, |
| 264 | + tell_position=0, |
| 265 | + content=content, |
| 266 | + ) |
| 267 | + body = '{"result_code":"Success","results":[]}' |
| 268 | + return Response( |
| 269 | + text=body, |
| 270 | + url=url, |
| 271 | + status_code=HTTPStatus.OK, |
| 272 | + headers={}, |
| 273 | + request_body=data, |
| 274 | + tell_position=0, |
| 275 | + content=body.encode(), |
| 276 | + ) |
| 277 | + |
| 278 | + |
| 279 | +class _FalsyAsyncTransport: |
| 280 | + """An async transport that is falsy but protocol-conforming.""" |
| 281 | + |
| 282 | + def __bool__(self) -> bool: |
| 283 | + """Return ``False`` so truthiness checks would skip this |
| 284 | + transport. |
| 285 | + """ |
| 286 | + return False |
| 287 | + |
| 288 | + async def aclose(self) -> None: |
| 289 | + """Close the transport.""" |
| 290 | + |
| 291 | + async def __call__( |
| 292 | + self, |
| 293 | + *, |
| 294 | + method: str, |
| 295 | + url: str, |
| 296 | + headers: dict[str, str], |
| 297 | + data: bytes, |
| 298 | + request_timeout: float | tuple[float, float], |
| 299 | + ) -> Response: |
| 300 | + """Return a successful API response for the requested URL.""" |
| 301 | + del method, headers, request_timeout |
| 302 | + if url.endswith("/query"): |
| 303 | + body = '{"result_code":"Success","results":[]}' |
| 304 | + return Response( |
| 305 | + text=body, |
| 306 | + url=url, |
| 307 | + status_code=HTTPStatus.OK, |
| 308 | + headers={}, |
| 309 | + request_body=data, |
| 310 | + tell_position=0, |
| 311 | + content=body.encode(), |
| 312 | + ) |
| 313 | + if "/instances" in url: |
| 314 | + content = b"vumark-bytes" |
| 315 | + return Response( |
| 316 | + text="", |
| 317 | + url=url, |
| 318 | + status_code=HTTPStatus.OK, |
| 319 | + headers={}, |
| 320 | + request_body=data, |
| 321 | + tell_position=0, |
| 322 | + content=content, |
| 323 | + ) |
| 324 | + body = '{"result_code":"Success","results":[]}' |
| 325 | + return Response( |
| 326 | + text=body, |
| 327 | + url=url, |
| 328 | + status_code=HTTPStatus.OK, |
| 329 | + headers={}, |
| 330 | + request_body=data, |
| 331 | + tell_position=0, |
| 332 | + content=body.encode(), |
| 333 | + ) |
| 334 | + |
| 335 | + |
| 336 | +def test_falsy_sync_transport_is_retained( |
| 337 | + high_quality_image: io.BytesIO, |
| 338 | +) -> None: |
| 339 | + """Falsy custom sync transports are not replaced by the default.""" |
| 340 | + access_key = uuid.uuid4().hex |
| 341 | + secret_key = uuid.uuid4().hex |
| 342 | + transport = _FalsyTransport() |
| 343 | + |
| 344 | + assert ( |
| 345 | + VWS( |
| 346 | + server_access_key=access_key, |
| 347 | + server_secret_key=secret_key, |
| 348 | + transport=transport, |
| 349 | + ).list_targets() |
| 350 | + == [] |
| 351 | + ) |
| 352 | + assert ( |
| 353 | + CloudRecoService( |
| 354 | + client_access_key=access_key, |
| 355 | + client_secret_key=secret_key, |
| 356 | + transport=transport, |
| 357 | + ).query(image=high_quality_image) |
| 358 | + == [] |
| 359 | + ) |
| 360 | + assert ( |
| 361 | + VuMarkService( |
| 362 | + server_access_key=access_key, |
| 363 | + server_secret_key=secret_key, |
| 364 | + transport=transport, |
| 365 | + ).generate_vumark_instance( |
| 366 | + target_id="target", |
| 367 | + instance_id="instance", |
| 368 | + accept=VuMarkAccept.PNG, |
| 369 | + ) |
| 370 | + == b"vumark-bytes" |
| 371 | + ) |
| 372 | + |
| 373 | + |
| 374 | +@pytest.mark.asyncio |
| 375 | +async def test_falsy_async_transport_is_retained( |
| 376 | + high_quality_image: io.BytesIO, |
| 377 | +) -> None: |
| 378 | + """Falsy custom async transports are not replaced by the default.""" |
| 379 | + access_key = uuid.uuid4().hex |
| 380 | + secret_key = uuid.uuid4().hex |
| 381 | + transport = _FalsyAsyncTransport() |
| 382 | + |
| 383 | + async with AsyncVWS( |
| 384 | + server_access_key=access_key, |
| 385 | + server_secret_key=secret_key, |
| 386 | + transport=transport, |
| 387 | + ) as vws_client: |
| 388 | + assert await vws_client.list_targets() == [] |
| 389 | + |
| 390 | + async with AsyncCloudRecoService( |
| 391 | + client_access_key=access_key, |
| 392 | + client_secret_key=secret_key, |
| 393 | + transport=transport, |
| 394 | + ) as cloud_reco_client: |
| 395 | + assert await cloud_reco_client.query(image=high_quality_image) == [] |
| 396 | + |
| 397 | + async with AsyncVuMarkService( |
| 398 | + server_access_key=access_key, |
| 399 | + server_secret_key=secret_key, |
| 400 | + transport=transport, |
| 401 | + ) as vumark_client: |
| 402 | + assert ( |
| 403 | + await vumark_client.generate_vumark_instance( |
| 404 | + target_id="target", |
| 405 | + instance_id="instance", |
| 406 | + accept=VuMarkAccept.PNG, |
| 407 | + ) |
| 408 | + == b"vumark-bytes" |
| 409 | + ) |
0 commit comments