Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow headers to be sent as iterables #1782

Merged
merged 2 commits into from Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/protocols/test_http.py
Expand Up @@ -966,3 +966,17 @@ async def test_return_close_header(protocol_cls, close_header: bytes):
assert b"content-type: text/plain" in protocol.transport.buffer
assert b"content-length: 12" in protocol.transport.buffer
assert close_header in protocol.transport.buffer


@pytest.mark.anyio
@pytest.mark.parametrize("protocol_cls", HTTP_PROTOCOLS)
async def test_iterator_headers(protocol_cls):
async def app(scope, receive, send):
headers = iter([(b"x-test-header", b"test value")])
await send({"type": "http.response.start", "status": 200, "headers": headers})
Comment on lines +973 to +976
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified those lines to be more mindful about what we really want to support.

await send({"type": "http.response.body", "body": b""})

protocol = get_connected_protocol(app, protocol_cls)
protocol.data_received(SIMPLE_GET_REQUEST)
await protocol.loop.run_one()
assert b"x-test-header: test value" in protocol.transport.buffer
5 changes: 1 addition & 4 deletions uvicorn/protocols/http/h11_impl.py
Expand Up @@ -468,10 +468,7 @@ async def send(self, message: "ASGISendEvent") -> None:
self.waiting_for_100_continue = False

status_code = message["status"]
message_headers = cast(
List[Tuple[bytes, bytes]], message.get("headers", [])
)
headers = self.default_headers + message_headers
headers = self.default_headers + list(message.get("headers", []))
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've modified this to match httptools.


if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
headers = headers + [CLOSE_HEADER]
Expand Down