]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix request auto headers (#1205)
authorTom Christie <tom@tomchristie.com>
Fri, 21 Aug 2020 11:03:15 +0000 (12:03 +0100)
committerGitHub <noreply@github.com>
Fri, 21 Aug 2020 11:03:15 +0000 (12:03 +0100)
* Failing test case

* Fix auto_headers in Request.prepare()

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
httpx/_models.py
tests/client/test_headers.py

index 1e139bce1f3dddb00d66c1d71c33874df2da41c2..3ce57899c3a7bce253a5ddec94e26eca5a649a37 100644 (file)
@@ -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:
index 81b369b6d610fb0565b7b1e0f42417e68bd8a752..b85107bace86ab299a1a890bb30719e593a04c36 100755 (executable)
@@ -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