]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add content-length header for empty bytestream (#866)
authorDima Boger <kotvberloge@gmail.com>
Sat, 28 Mar 2020 15:05:06 +0000 (18:05 +0300)
committerGitHub <noreply@github.com>
Sat, 28 Mar 2020 15:05:06 +0000 (16:05 +0100)
Also, fixed related tests

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 afffed34324eb069c07c52207569529d6d728259..dd6c51f6af7d3fa172d0512268a4d95e6f4e1bc4 100644 (file)
@@ -71,8 +71,6 @@ 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 22b0ba1e89a64c69a842a386fbbbcaf7eec1766f..5c4dbb48b911cbcd33a2143d5c37d88fe11701a5 100755 (executable)
@@ -40,6 +40,7 @@ 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__}",
@@ -61,6 +62,7 @@ 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",
@@ -82,6 +84,7 @@ 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",
@@ -105,6 +108,7 @@ 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__}",
         }
@@ -117,6 +121,7 @@ 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",
         }
@@ -147,6 +152,7 @@ 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=",
@@ -171,6 +177,7 @@ 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 190ff0940921931fccf01d750457a10e942c108f..63403a61e3ea7b944fb41a887b01ac727f9deb3c 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 "content-length" in response.json()["headers"]
+    assert response.json()["headers"]["content-length"] == "20"
 
 
 @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 "content-length" not in response.json()["headers"]
+    assert response.json()["headers"]["content-length"] == "0"
 
 
 @pytest.mark.usefixtures("async_environment")
index c72e0af96423c4247b25d45f71a2574c11ed9ceb..04613942ea4d68fbdf807e211797160e539bdc20 100644 (file)
@@ -10,7 +10,7 @@ def test_request_repr():
 
 def test_no_content():
     request = httpx.Request("GET", "http://example.org")
-    assert "Content-Length" not in request.headers
+    assert request.headers["Content-Length"] == "0"
 
 
 def test_content_length_header():
index c5eb961ddca014a45670c999d1597a240fd6318b..84999efc2a2f0c08ab2f9d5857f2e91b76791ba5 100644 (file)
@@ -25,7 +25,9 @@ async def test_empty_content():
     async_content = b"".join([part async for part in stream])
 
     assert stream.can_replay()
-    assert stream.get_headers() == {}
+    assert stream.get_headers() == {
+        "Content-Length": "0",
+    }
     assert sync_content == b""
     assert async_content == b""
 
@@ -196,7 +198,9 @@ async def test_empty_request():
     async_content = b"".join([part async for part in stream])
 
     assert stream.can_replay()
-    assert stream.get_headers() == {}
+    assert stream.get_headers() == {
+        "Content-Length": "0",
+    }
     assert sync_content == b""
     assert async_content == b""