]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Revert "Add content-length header for empty bytestream (#866)" (#898)
authorTom Christie <tom@tomchristie.com>
Wed, 8 Apr 2020 12:15:22 +0000 (13:15 +0100)
committerGitHub <noreply@github.com>
Wed, 8 Apr 2020 12:15:22 +0000 (13:15 +0100)
This reverts commit 939f3ce7ce43d11f3a9829dabaa292b3a906b56d.

httpx/_content_streams.py
tests/client/test_headers.py
tests/client/test_redirects.py
tests/models/test_requests.py
tests/test_content_streams.py

index e6a3881f4ea74e4ff370f46546922cab0653af0f..3577cabf1159ef5cab06e1d6886924614751bcee 100644 (file)
@@ -73,6 +73,8 @@ class ByteStream(ContentStream):
         self.body = body.encode("utf-8") if isinstance(body, str) else body
 
     def get_headers(self) -> typing.Dict[str, str]:
+        if not self.body:
+            return {}
         content_length = str(len(self.body))
         return {"Content-Length": content_length}
 
index 5c4dbb48b911cbcd33a2143d5c37d88fe11701a5..22b0ba1e89a64c69a842a386fbbbcaf7eec1766f 100755 (executable)
@@ -40,7 +40,6 @@ async def test_client_header():
             "accept": "*/*",
             "accept-encoding": "gzip, deflate, br",
             "connection": "keep-alive",
-            "content-length": "0",
             "example-header": "example-value",
             "host": "example.org",
             "user-agent": f"python-httpx/{__version__}",
@@ -62,7 +61,6 @@ async def test_header_merge():
             "accept": "*/*",
             "accept-encoding": "gzip, deflate, br",
             "connection": "keep-alive",
-            "content-length": "0",
             "host": "example.org",
             "user-agent": "python-myclient/0.2.1",
             "x-auth-token": "FooBarBazToken",
@@ -84,7 +82,6 @@ async def test_header_merge_conflicting_headers():
             "accept": "*/*",
             "accept-encoding": "gzip, deflate, br",
             "connection": "keep-alive",
-            "content-length": "0",
             "host": "example.org",
             "user-agent": f"python-httpx/{__version__}",
             "x-auth-token": "BazToken",
@@ -108,7 +105,6 @@ async def test_header_update():
             "accept": "*/*",
             "accept-encoding": "gzip, deflate, br",
             "connection": "keep-alive",
-            "content-length": "0",
             "host": "example.org",
             "user-agent": f"python-httpx/{__version__}",
         }
@@ -121,7 +117,6 @@ async def test_header_update():
             "accept-encoding": "gzip, deflate, br",
             "another-header": "AThing",
             "connection": "keep-alive",
-            "content-length": "0",
             "host": "example.org",
             "user-agent": "python-myclient/0.2.1",
         }
@@ -152,7 +147,6 @@ async def test_host_with_auth_and_port_in_url():
             "accept": "*/*",
             "accept-encoding": "gzip, deflate, br",
             "connection": "keep-alive",
-            "content-length": "0",
             "host": "example.org",
             "user-agent": f"python-httpx/{__version__}",
             "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
@@ -177,7 +171,6 @@ async def test_host_with_non_default_port_in_url():
             "accept": "*/*",
             "accept-encoding": "gzip, deflate, br",
             "connection": "keep-alive",
-            "content-length": "0",
             "host": "example.org:123",
             "user-agent": f"python-httpx/{__version__}",
             "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
index 63403a61e3ea7b944fb41a887b01ac727f9deb3c..190ff0940921931fccf01d750457a10e942c108f 100644 (file)
@@ -279,7 +279,7 @@ async def test_body_redirect():
     response = await client.post(url, data=data)
     assert response.url == URL("https://example.org/redirect_body_target")
     assert response.json()["body"] == "Example request body"
-    assert response.json()["headers"]["content-length"] == "20"
+    assert "content-length" in response.json()["headers"]
 
 
 @pytest.mark.usefixtures("async_environment")
@@ -293,7 +293,7 @@ async def test_no_body_redirect():
     response = await client.post(url, data=data)
     assert response.url == URL("https://example.org/redirect_body_target")
     assert response.json()["body"] == ""
-    assert response.json()["headers"]["content-length"] == "0"
+    assert "content-length" not in response.json()["headers"]
 
 
 @pytest.mark.usefixtures("async_environment")
index 04613942ea4d68fbdf807e211797160e539bdc20..c72e0af96423c4247b25d45f71a2574c11ed9ceb 100644 (file)
@@ -10,7 +10,7 @@ def test_request_repr():
 
 def test_no_content():
     request = httpx.Request("GET", "http://example.org")
-    assert request.headers["Content-Length"] == "0"
+    assert "Content-Length" not in request.headers
 
 
 def test_content_length_header():
index 84999efc2a2f0c08ab2f9d5857f2e91b76791ba5..c5eb961ddca014a45670c999d1597a240fd6318b 100644 (file)
@@ -25,9 +25,7 @@ async def test_empty_content():
     async_content = b"".join([part async for part in stream])
 
     assert stream.can_replay()
-    assert stream.get_headers() == {
-        "Content-Length": "0",
-    }
+    assert stream.get_headers() == {}
     assert sync_content == b""
     assert async_content == b""
 
@@ -198,9 +196,7 @@ async def test_empty_request():
     async_content = b"".join([part async for part in stream])
 
     assert stream.can_replay()
-    assert stream.get_headers() == {
-        "Content-Length": "0",
-    }
+    assert stream.get_headers() == {}
     assert sync_content == b""
     assert async_content == b""