auto_headers: typing.List[typing.Tuple[bytes, bytes]] = []
has_host = "host" in self.headers
+ has_content_length = (
+ "content-length" in self.headers or "transfer-encoding" in self.headers
+ )
has_user_agent = "user-agent" in self.headers
has_accept = "accept" in self.headers
has_accept_encoding = "accept-encoding" in self.headers
if url.userinfo:
url = url.copy_with(username=None, password=None)
auto_headers.append((b"host", url.authority.encode("ascii")))
+ if not has_content_length and self.method in ("POST", "PUT", "PATCH"):
+ auto_headers.append((b"content-length", b"0"))
if not has_user_agent:
auto_headers.append((b"user-agent", USER_AGENT.encode("ascii")))
if not has_accept:
assert response.json()["Custom-header"] == "value"
+def test_build_post_request(server):
+ url = server.url.copy_with(path="/echo_headers")
+ headers = {"Custom-header": "value"}
+
+ with httpx.Client() as client:
+ request = client.build_request("POST", url)
+ request.headers.update(headers)
+ response = client.send(request)
+
+ assert response.status_code == 200
+ assert response.url == url
+
+ assert response.json()["Content-length"] == "0"
+ assert response.json()["Custom-header"] == "value"
+
+
def test_post(server):
with httpx.Client() as client:
response = client.post(server.url, data=b"Hello, world!")