This repository was archived by the owner on Jan 13, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,28 @@ def iter_raw(self):
181181 for item in self ._items :
182182 yield item
183183
184+ def replace (self , key , value ):
185+ """
186+ Replace existing header with new value. If header doesn't exist this
187+ method work like ``__setitem__``. Replacing leads to deletion of all
188+ exsiting headers with the same name.
189+ """
190+ idx = None
191+ key = to_bytestring (key )
192+ for (i , (k , v )) in enumerate (self ._items ):
193+ if _keys_equal (k , key ):
194+ idx = i
195+ break
196+
197+ if idx is not None :
198+ del self [key ]
199+
200+ self ._items .insert (
201+ idx if idx is not None else len (self ._items ),
202+ to_bytestring_tuple (key , value )
203+ )
204+
205+
184206 def merge (self , other ):
185207 """
186208 Merge another header set or any other dict-like into this one.
Original file line number Diff line number Diff line change @@ -258,3 +258,22 @@ def test_merge_header_map_dict(self):
258258 (b'hi' , b'there' ),
259259 (b'cat' , b'dog' ),
260260 ]
261+
262+ def test_replacing (self ):
263+ h = HTTPHeaderMap ([
264+ (b'name' , b'value' ),
265+ (b'name2' , b'value2' ),
266+ (b'name2' , b'value2' ),
267+ (b'name3' , b'value3' ),
268+ ])
269+
270+ h .replace ('name2' , '42' )
271+ h .replace ('name4' , 'other_value' )
272+
273+ assert list (h .items ()) == [
274+ (b'name' , b'value' ),
275+ (b'name2' , b'42' ),
276+ (b'name3' , b'value3' ),
277+ (b'name4' , b'other_value' ),
278+ ]
279+
You can’t perform that action at this time.
0 commit comments