]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Make sure MutableHeaders._list is actually a list (#1917)
authorAdrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>
Fri, 21 Oct 2022 12:57:01 +0000 (07:57 -0500)
committerGitHub <noreply@github.com>
Fri, 21 Oct 2022 12:57:01 +0000 (07:57 -0500)
starlette/datastructures.py
tests/test_datastructures.py

index 02d555b184197c2854c0b4e4a2dd24bec16528d6..096ad21021eb321688e35c71e845b2f2746aa90b 100644 (file)
@@ -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]]:
index d32185492fb8f93b3928505387381d32099981e3..b87b26e22f39e9be378fa0d47781fd766742b7d9 100644 (file)
@@ -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