From: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Date: Fri, 21 Oct 2022 12:57:01 +0000 (-0500) Subject: Make sure MutableHeaders._list is actually a list (#1917) X-Git-Tag: 0.22.0~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9aa0db4bff2220ae8cfbc54c11fbf65737577009;p=thirdparty%2Fstarlette.git Make sure MutableHeaders._list is actually a list (#1917) --- diff --git a/starlette/datastructures.py b/starlette/datastructures.py index 02d555b1..096ad210 100644 --- a/starlette/datastructures.py +++ b/starlette/datastructures.py @@ -508,7 +508,7 @@ class Headers(typing.Mapping[str, str]): 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: @@ -522,7 +522,9 @@ class Headers(typing.Mapping[str, str]): 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]]: diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index d3218549..b87b26e2 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -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