for idx in reversed(pop_indexes):
del self._list[idx]
+ def __ior__(self, other: typing.Mapping) -> "MutableHeaders":
+ if not isinstance(other, typing.Mapping):
+ raise TypeError(f"Expected a mapping but got {other.__class__.__name__}")
+ self.update(other)
+ return self
+
+ def __or__(self, other: typing.Mapping) -> "MutableHeaders":
+ if not isinstance(other, typing.Mapping):
+ raise TypeError(f"Expected a mapping but got {other.__class__.__name__}")
+ new = self.mutablecopy()
+ new.update(other)
+ return new
+
@property
def raw(self) -> typing.List[typing.Tuple[bytes, bytes]]:
return self._list
self._list.append((set_key, set_value))
return value
- def update(self, other: dict) -> None:
+ def update(self, other: typing.Mapping) -> None:
for key, val in other.items():
self[key] = val
assert h.raw == [(b"b", b"4")]
+def test_mutable_headers_merge():
+ h = MutableHeaders()
+ h = h | MutableHeaders({"a": "1"})
+ assert isinstance(h, MutableHeaders)
+ assert dict(h) == {"a": "1"}
+ assert h.items() == [("a", "1")]
+ assert h.raw == [(b"a", b"1")]
+
+
+def test_mutable_headers_merge_dict():
+ h = MutableHeaders()
+ h = h | {"a": "1"}
+ assert isinstance(h, MutableHeaders)
+ assert dict(h) == {"a": "1"}
+ assert h.items() == [("a", "1")]
+ assert h.raw == [(b"a", b"1")]
+
+
+def test_mutable_headers_update():
+ h = MutableHeaders()
+ h |= MutableHeaders({"a": "1"})
+ assert isinstance(h, MutableHeaders)
+ assert dict(h) == {"a": "1"}
+ assert h.items() == [("a", "1")]
+ assert h.raw == [(b"a", b"1")]
+
+
+def test_mutable_headers_update_dict():
+ h = MutableHeaders()
+ h |= {"a": "1"}
+ assert isinstance(h, MutableHeaders)
+ assert dict(h) == {"a": "1"}
+ assert h.items() == [("a", "1")]
+ assert h.raw == [(b"a", b"1")]
+
+
+def test_mutable_headers_merge_not_mapping():
+ h = MutableHeaders()
+ with pytest.raises(TypeError):
+ h |= {"not_mapping"} # type: ignore
+ with pytest.raises(TypeError):
+ h | {"not_mapping"} # type: ignore
+
+
def test_headers_mutablecopy():
h = Headers(raw=[(b"a", b"123"), (b"a", b"456"), (b"b", b"789")])
c = h.mutablecopy()