From: Tom Christie Date: Fri, 21 Aug 2020 11:03:15 +0000 (+0100) Subject: Fix request auto headers (#1205) X-Git-Tag: 0.14.2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19515e8a8b7df6e5064047f02df307798c184ef5;p=thirdparty%2Fhttpx.git Fix request auto headers (#1205) * Failing test case * Fix auto_headers in Request.prepare() Co-authored-by: Florimond Manca --- diff --git a/httpx/_models.py b/httpx/_models.py index 1e139bce..3ce57899 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -411,9 +411,8 @@ class Headers(typing.MutableMapping[str, str]): def raw(self) -> typing.List[typing.Tuple[bytes, bytes]]: """ Returns a list of the raw header items, as byte pairs. - May be mutated in-place. """ - return self._list + return list(self._list) def keys(self) -> typing.KeysView[str]: return {key.decode(self.encoding): None for key in self._dict.keys()}.keys() @@ -647,8 +646,7 @@ class Request: if not has_connection: auto_headers.append((b"connection", b"keep-alive")) - for item in reversed(auto_headers): - self.headers.raw.insert(0, item) + self.headers = Headers(auto_headers + self.headers.raw) @property def content(self) -> bytes: diff --git a/tests/client/test_headers.py b/tests/client/test_headers.py index 81b369b6..b85107ba 100755 --- a/tests/client/test_headers.py +++ b/tests/client/test_headers.py @@ -5,7 +5,7 @@ import typing import httpcore import pytest -from httpx import AsyncClient, Headers, __version__ +from httpx import AsyncClient, Headers, Request, __version__ from httpx._content_streams import ContentStream, JSONStream @@ -181,3 +181,8 @@ async def test_host_with_non_default_port_in_url(): "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=", } } + + +def test_request_auto_headers(): + request = Request("GET", "https://www.example.org/") + assert "host" in request.headers