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:
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]]:
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