Fix utf8 headers

This commit is contained in:
Alexander Goryushkin 2025-05-12 10:08:55 +05:00
parent 6c7af96773
commit 9e9b027ee2
3 changed files with 25 additions and 2 deletions

View File

@ -207,7 +207,7 @@ class BaseClient:
self._auth = self._build_auth(auth)
self._params = QueryParams(params)
self.headers = Headers(headers)
self.headers = Headers(headers, encoding=default_encoding)
self._cookies = Cookies(cookies)
self._timeout = Timeout(timeout)
self.follow_redirects = follow_redirects

View File

@ -272,7 +272,7 @@ class Headers(typing.MutableMapping[str, str]):
return split_values
def update(self, headers: HeaderTypes | None = None) -> None: # type: ignore
headers = Headers(headers)
headers = Headers(headers, encoding=self.encoding)
for key in headers.keys():
if key in self:
self.pop(key)

View File

@ -291,3 +291,26 @@ def test_is_not_https_redirect_if_not_default_ports():
headers = client._redirect_headers(request, url, "GET")
assert "Authorization" not in headers
def test_utf8_header():
"""
Set a header in the Client.
"""
url = "http://example.org/echo_headers"
headers = {"Example-Header": "заголовок"}
client = httpx.Client(transport=httpx.MockTransport(echo_headers), headers=headers)
response = client.get(url)
assert response.status_code == 200
assert response.json() == {
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br, zstd",
"connection": "keep-alive",
"example-header": "заголовок",
"host": "example.org",
"user-agent": f"python-httpx/{httpx.__version__}",
}
}