|
20 | 20 | from starlette.responses import JSONResponse, RedirectResponse, Response |
21 | 21 | from starlette.routing import Route |
22 | 22 | from starlette.testclient import ASGIInstance, TestClient |
23 | | -from starlette.types import ASGIApp, Receive, Scope, Send |
| 23 | +from starlette.types import ASGIApp, Message, Receive, Scope, Send |
24 | 24 | from starlette.websockets import WebSocket, WebSocketDisconnect |
25 | 25 | from tests.types import TestClientFactory |
26 | 26 |
|
@@ -490,3 +490,93 @@ def test_timeout_deprecation() -> None: |
490 | 490 | ): |
491 | 491 | client = TestClient(mock_service) |
492 | 492 | client.get("/", timeout=1) |
| 493 | + |
| 494 | + |
| 495 | +@pytest.mark.parametrize( |
| 496 | + "messages, error", |
| 497 | + [ |
| 498 | + ([], "TestClient did not receive any response"), |
| 499 | + ([{"type": "http.response.trailers"}], "without declaring trailers"), |
| 500 | + ( |
| 501 | + [{"type": "http.response.start", "status": 200, "trailers": True}, {"type": "http.response.trailers"}], |
| 502 | + "before body completed", |
| 503 | + ), |
| 504 | + ( |
| 505 | + [ |
| 506 | + {"type": "http.response.start", "status": 200, "trailers": True}, |
| 507 | + {"type": "http.response.body"}, |
| 508 | + {"type": "http.response.body"}, |
| 509 | + ], |
| 510 | + "after body completed", |
| 511 | + ), |
| 512 | + ( |
| 513 | + [ |
| 514 | + {"type": "http.response.start", "status": 200, "trailers": True}, |
| 515 | + {"type": "http.response.body"}, |
| 516 | + {"type": "http.response.trailers"}, |
| 517 | + {"type": "http.response.trailers"}, |
| 518 | + ], |
| 519 | + "after response completed", |
| 520 | + ), |
| 521 | + ], |
| 522 | +) |
| 523 | +def test_invalid_trailer_sequence(test_client_factory: TestClientFactory, messages: list[Message], error: str) -> None: |
| 524 | + async def app(scope: Scope, receive: Receive, send: Send) -> None: |
| 525 | + for message in messages: |
| 526 | + await send(message) |
| 527 | + |
| 528 | + with pytest.raises(AssertionError, match=error): |
| 529 | + test_client_factory(app).get("/") |
| 530 | + |
| 531 | + |
| 532 | +@pytest.mark.parametrize("empty", [True, False]) |
| 533 | +def test_capture_trailers(test_client_factory: TestClientFactory, empty: bool) -> None: |
| 534 | + async def app(scope: Scope, receive: Receive, send: Send) -> None: |
| 535 | + assert "http.response.trailers" in scope["extensions"] |
| 536 | + await send({"type": "http.response.start", "status": 200, "trailers": True}) |
| 537 | + await send({"type": "http.response.body", "body": b"hello"}) |
| 538 | + await anyio.lowlevel.checkpoint() |
| 539 | + headers = [] if empty else [(b"x-item", b"one")] |
| 540 | + await send({"type": "http.response.trailers", "headers": headers, "more_trailers": True}) |
| 541 | + headers = [] if empty else [(b"x-item", b"two")] |
| 542 | + await send({"type": "http.response.trailers", "headers": headers}) |
| 543 | + |
| 544 | + response = test_client_factory(app).get("/", headers={"te": "trailers"}) |
| 545 | + assert response.content == b"hello" |
| 546 | + assert response.extensions["http.response.trailers"] == ( |
| 547 | + [] if empty else [(b"x-item", b"one"), (b"x-item", b"two")] |
| 548 | + ) |
| 549 | + assert "x-item" not in response.headers |
| 550 | + |
| 551 | + |
| 552 | +@pytest.mark.parametrize("partial", [True, False]) |
| 553 | +def test_incomplete_trailers(test_client_factory: TestClientFactory, partial: bool) -> None: |
| 554 | + async def app(scope: Scope, receive: Receive, send: Send) -> None: |
| 555 | + await send({"type": "http.response.start", "status": 200, "trailers": True}) |
| 556 | + await send({"type": "http.response.body", "body": b"hello"}) |
| 557 | + if partial: |
| 558 | + await send({"type": "http.response.trailers", "headers": [(b"x-item", b"partial")], "more_trailers": True}) |
| 559 | + |
| 560 | + response = test_client_factory(app).get("/") |
| 561 | + assert response.content == b"hello" |
| 562 | + assert response.extensions["http.response.trailers"] == ([(b"x-item", b"partial")] if partial else []) |
| 563 | + |
| 564 | + |
| 565 | +@pytest.mark.parametrize("raise_server_exceptions", [True, False]) |
| 566 | +def test_trailer_error_respects_raise_server_exceptions( |
| 567 | + test_client_factory: TestClientFactory, raise_server_exceptions: bool |
| 568 | +) -> None: |
| 569 | + async def app(scope: Scope, receive: Receive, send: Send) -> None: |
| 570 | + await send({"type": "http.response.start", "status": 200, "trailers": True}) |
| 571 | + await send({"type": "http.response.body", "body": b"hello"}) |
| 572 | + await send({"type": "http.response.trailers", "headers": [(b"x-item", b"partial")], "more_trailers": True}) |
| 573 | + raise ValueError("trailer production failed") |
| 574 | + |
| 575 | + client = test_client_factory(app, raise_server_exceptions=raise_server_exceptions) |
| 576 | + if raise_server_exceptions: |
| 577 | + with pytest.raises(ValueError, match="trailer production failed"): |
| 578 | + client.get("/") |
| 579 | + else: |
| 580 | + response = client.get("/") |
| 581 | + assert response.content == b"hello" |
| 582 | + assert response.extensions["http.response.trailers"] == [(b"x-item", b"partial")] |
0 commit comments