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

Make sure MutableHeaders._list is actually a list #1917

Merged
merged 6 commits into from Oct 21, 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
6 changes: 4 additions & 2 deletions starlette/datastructures.py
Expand Up @@ -508,7 +508,7 @@ def __init__(
self,
headers: typing.Optional[typing.Mapping[str, str]] = None,
raw: typing.Optional[typing.List[typing.Tuple[bytes, bytes]]] = None,
scope: typing.Optional[typing.Mapping[str, typing.Any]] = None,
scope: typing.Optional[typing.MutableMapping[str, typing.Any]] = None,
) -> None:
self._list: typing.List[typing.Tuple[bytes, bytes]] = []
if headers is not None:
Expand All @@ -522,7 +522,9 @@ def __init__(
assert scope is None, 'Cannot set both "raw" and "scope".'
self._list = raw
elif scope is not None:
self._list = scope["headers"]
# scope["headers"] isn't necessarily a list
# it might be a tuple or other iterable
self._list = scope["headers"] = list(scope["headers"])

@property
def raw(self) -> typing.List[typing.Tuple[bytes, bytes]]:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_datastructures.py
Expand Up @@ -214,6 +214,16 @@ def test_headers_mutablecopy():
assert c.items() == [("a", "abc"), ("b", "789")]


def test_mutable_headers_from_scope():
# "headers" in scope must not necessarily be a list
h = MutableHeaders(scope={"headers": ((b"a", b"1"),)})
assert dict(h) == {"a": "1"}
h.update({"b": "2"})
assert dict(h) == {"a": "1", "b": "2"}
assert list(h.items()) == [("a", "1"), ("b", "2")]
assert list(h.raw) == [(b"a", b"1"), (b"b", b"2")]


def test_url_blank_params():
q = QueryParams("a=123&abc&def&b=456")
assert "a" in q
Expand Down