Skip to content

Commit

Permalink
Make sure MutableHeaders._list is actually a list (#1917)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Oct 21, 2022
1 parent ab70211 commit 9aa0db4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit 9aa0db4

Please sign in to comment.