diff --git a/starlette/datastructures.py b/starlette/datastructures.py index 247157ca..2520707d 100644 --- a/starlette/datastructures.py +++ b/starlette/datastructures.py @@ -155,6 +155,9 @@ class Headers(typing.Mapping[str, str]): if item_key == get_header_key ] + def mutablecopy(self): + return MutableHeaders(self._list[:]) + def __getitem__(self, key: str): get_header_key = key.lower().encode("latin-1") for header_key, header_value in self._list: diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index 0a5a8441..8f4f70bb 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -51,6 +51,13 @@ def test_mutable_headers(): assert dict(h) == {"b": "4"} +def test_headers_mutablecopy(): + h = Headers([(b"a", b"123"), (b"a", b"456"), (b"b", b"789")]) + c = h.mutablecopy() + assert c.items() == [("a", "123"), ("a", "456"), ("b", "789")] + c["a"] = "abc" + assert c.items() == [("b", "789"), ("a", "abc")] + def test_queryparams(): q = QueryParams([("a", "123"), ("a", "456"), ("b", "789")]) assert "a" in q