|
7 | 7 | import asyncio |
8 | 8 | import datetime |
9 | 9 | import email.utils |
| 10 | +import typing |
10 | 11 | from collections.abc import Callable |
11 | 12 | from http import HTTPStatus |
12 | 13 |
|
13 | 14 | import httpx2 |
14 | 15 | import pytest |
15 | 16 |
|
16 | 17 | from httpware import AsyncClient, NotFoundError, ServiceUnavailableError, TransportError |
| 18 | +from httpware.client import _is_streaming_body |
17 | 19 | from httpware.errors import NetworkError, RetryBudgetExhaustedError |
18 | 20 | from httpware.errors import TimeoutError as HttpwareTimeoutError |
19 | 21 | from httpware.middleware.resilience.budget import RetryBudget |
@@ -441,3 +443,80 @@ async def test_explicit_budget_shared_across_retry_instances() -> None: |
441 | 443 | for _ in range(10): |
442 | 444 | assert shared.try_withdraw() is True |
443 | 445 | assert shared.try_withdraw() is False |
| 446 | + |
| 447 | + |
| 448 | +async def test_client_post_with_async_iterable_content_marks_extensions() -> None: |
| 449 | + """Posting with an async-iterable body sets the httpware.streaming_body marker on request.extensions.""" |
| 450 | + seen_extensions: list[dict[str, object]] = [] |
| 451 | + |
| 452 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 453 | + seen_extensions.append(dict(request.extensions)) |
| 454 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 455 | + |
| 456 | + async def streamed_body() -> typing.AsyncIterator[bytes]: |
| 457 | + yield b"chunk1" |
| 458 | + yield b"chunk2" |
| 459 | + |
| 460 | + transport = httpx2.MockTransport(handler) |
| 461 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 462 | + await client.post("https://example.test/upload", content=streamed_body()) |
| 463 | + |
| 464 | + assert len(seen_extensions) == 1 |
| 465 | + assert seen_extensions[0].get("httpware.streaming_body") is True |
| 466 | + |
| 467 | + |
| 468 | +async def test_client_post_with_bytes_content_does_not_mark_extensions() -> None: |
| 469 | + seen_extensions: list[dict[str, object]] = [] |
| 470 | + |
| 471 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 472 | + seen_extensions.append(dict(request.extensions)) |
| 473 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 474 | + |
| 475 | + transport = httpx2.MockTransport(handler) |
| 476 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 477 | + await client.post("https://example.test/upload", content=b"hi") |
| 478 | + |
| 479 | + assert len(seen_extensions) == 1 |
| 480 | + assert "httpware.streaming_body" not in seen_extensions[0] |
| 481 | + |
| 482 | + |
| 483 | +async def test_client_post_with_dict_data_does_not_mark_extensions() -> None: |
| 484 | + seen_extensions: list[dict[str, object]] = [] |
| 485 | + |
| 486 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 487 | + seen_extensions.append(dict(request.extensions)) |
| 488 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 489 | + |
| 490 | + transport = httpx2.MockTransport(handler) |
| 491 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 492 | + await client.post("https://example.test/upload", data={"k": "v"}) |
| 493 | + |
| 494 | + assert len(seen_extensions) == 1 |
| 495 | + assert "httpware.streaming_body" not in seen_extensions[0] |
| 496 | + |
| 497 | + |
| 498 | +async def test_client_post_with_async_iterable_data_marks_extensions() -> None: |
| 499 | + seen_extensions: list[dict[str, object]] = [] |
| 500 | + |
| 501 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 502 | + seen_extensions.append(dict(request.extensions)) |
| 503 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 504 | + |
| 505 | + async def streamed_data() -> typing.AsyncIterator[bytes]: |
| 506 | + yield b"x" |
| 507 | + |
| 508 | + transport = httpx2.MockTransport(handler) |
| 509 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 510 | + await client.post("https://example.test/upload", data=streamed_data()) |
| 511 | + |
| 512 | + assert len(seen_extensions) == 1 |
| 513 | + assert seen_extensions[0].get("httpware.streaming_body") is True |
| 514 | + |
| 515 | + |
| 516 | +def test_is_streaming_body_true_for_async_iterable_files() -> None: |
| 517 | + """_is_streaming_body returns True for an async-iterable, covering the files= path.""" |
| 518 | + |
| 519 | + async def streamed_files() -> typing.AsyncIterator[bytes]: |
| 520 | + yield b"x" # pragma: no cover |
| 521 | + |
| 522 | + assert _is_streaming_body(streamed_files()) is True |
0 commit comments