]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Ignore transfer-encoding if content-length presents (#1170)
authorJoe <nigelchiang@outlook.com>
Tue, 11 Aug 2020 15:43:57 +0000 (23:43 +0800)
committerGitHub <noreply@github.com>
Tue, 11 Aug 2020 15:43:57 +0000 (23:43 +0800)
* Ignore transfer-encoding if content-length presents

Co-authored-by: Tom Christie <tom@tomchristie.com>
httpx/_models.py
tests/models/test_requests.py

index a5913c43c476d46988880b49845542aec0981e56..8d294e7f237944e3578e08d32d1546bb98bb0691 100644 (file)
@@ -613,6 +613,9 @@ class Request:
 
     def prepare(self) -> None:
         for key, value in self.stream.get_headers().items():
+            # Ignore Transfer-Encoding if the Content-Length has been set explicitly.
+            if key.lower() == "transfer-encoding" and "content-length" in self.headers:
+                continue
             self.headers.setdefault(key, value)
 
         auto_headers: typing.List[typing.Tuple[bytes, bytes]] = []
index e360ea052582d1381936b2b83b547afc77058004..f2ee98edb6f7fb7f93a0bae8075e8ddbd6cb2c9f 100644 (file)
@@ -73,6 +73,23 @@ def test_transfer_encoding_header():
     assert request.headers["Transfer-Encoding"] == "chunked"
 
 
+def test_ignore_transfer_encoding_header_if_content_length_exists():
+    """
+    `Transfer-Encoding` should be ignored if `Content-Length` has been set explicitly.
+    See https://github.com/encode/httpx/issues/1168
+    """
+
+    def streaming_body(data):
+        yield data  # pragma: nocover
+
+    data = streaming_body(b"abcd")
+
+    headers = {"Content-Length": "4"}
+    request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
+    assert "Transfer-Encoding" not in request.headers
+    assert request.headers["Content-Length"] == "4"
+
+
 def test_override_host_header():
     headers = {"host": "1.2.3.4:80"}