Skip to content
Myr

Fix Headers.update to correctly handle repeated headers (#2038)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Adrian Garcia Badaracco · committed by GitHub · parent 8dc9b6b · Browse files · 2 files+43 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+43 added−2 removed

  • tests:+39 added−0 removed
  • other:+4 added−2 removed

2 files+43 added−2 removed

modified:httpx/_models.py+4 added-2 removed
962 unmodified lines
963 return split_values964965 def update(self, headers: HeaderTypes = None) -> None: # type: ignore966 headers = Headers(headers)967 for key, value in headers.raw:968 self[key.decode(headers.encoding)] = value.decode(headers.encoding)967 for key in headers.keys():968 if key in self:969 self.pop(key)970 self._list.extend(headers._list)971972 def copy(self) -> "Headers":973 return Headers(self, encoding=self.encoding)974
973 unmodified lines
modified:tests/client/test_headers.py+39 added
8 unmodified lines
9 data = {"headers": dict(request.headers)}10 return httpx.Response(200, json=data)111213def echo_repeated_headers_multi_items(request: httpx.Request) -> httpx.Response:14 data = {"headers": list(request.headers.multi_items())}15 return httpx.Response(200, json=data)161718def echo_repeated_headers_items(request: httpx.Request) -> httpx.Response:19 data = {"headers": list(request.headers.items())}20 return httpx.Response(200, json=data)212223def test_client_header():24 """25 Set a header in the Client.26 """
92 unmodified lines
119 }120 }121122123def test_header_repeated_items():124 url = "http://example.org/echo_headers"125 client = httpx.Client(transport=httpx.MockTransport(echo_repeated_headers_items))126 response = client.get(url, headers=[("x-header", "1"), ("x-header", "2,3")])127128 assert response.status_code == 200129130 echoed_headers = response.json()["headers"]131 # as per RFC 7230, the whitespace after a comma is insignificant132 # so we split and strip here so that we can do a safe comparison133 assert ["x-header", ["1", "2", "3"]] in [134 [k, [subv.lstrip() for subv in v.split(",")]] for k, v in echoed_headers135 ]136137138def test_header_repeated_multi_items():139 url = "http://example.org/echo_headers"140 client = httpx.Client(141 transport=httpx.MockTransport(echo_repeated_headers_multi_items)142 )143 response = client.get(url, headers=[("x-header", "1"), ("x-header", "2,3")])144145 assert response.status_code == 200146147 echoed_headers = response.json()["headers"]148 assert ["x-header", "1"] in echoed_headers149 assert ["x-header", "2,3"] in echoed_headers150151152def test_remove_default_header():153 """154 Remove a default header from the Client.155 """
74 unmodified lines