Skip to content

Commit

Permalink
Fix request auto headers (#1205)
Browse files Browse the repository at this point in the history
* Failing test case

* Fix auto_headers in Request.prepare()

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
  • Loading branch information
tomchristie and florimondmanca authored Aug 21, 2020
1 parent f4b407a commit 19515e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 2 additions & 4 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,8 @@ def encoding(self, value: str) -> None:
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()
Expand Down Expand Up @@ -647,8 +646,7 @@ def prepare(self) -> None:
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:
Expand Down
7 changes: 6 additions & 1 deletion tests/client/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import httpcore
import pytest

from httpx import AsyncClient, Headers, __version__
from httpx import AsyncClient, Headers, Request, __version__
from httpx._content_streams import ContentStream, JSONStream


Expand Down Expand Up @@ -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

0 comments on commit 19515e8

Please sign in to comment.