]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix multipart edge cases with `data={}` and/or `files={}` (#861)
authorDima Boger <kotvberloge@gmail.com>
Mon, 16 Mar 2020 21:34:50 +0000 (00:34 +0300)
committerGitHub <noreply@github.com>
Mon, 16 Mar 2020 21:34:50 +0000 (22:34 +0100)
* Add reproducible test example for empty multipart

* Possible fix for empty combination of files/data

* Return bytestream with empty data/files

* Remove content-length in test

Co-authored-by: florimondmanca <florimond.manca@gmail.com>
httpx/_content_streams.py
tests/test_content_streams.py

index 5f3237e1bbad8313f8d29266041be964d846d707..afffed34324eb069c07c52207569529d6d728259 100644 (file)
@@ -323,7 +323,7 @@ def encode(
     Handles encoding the given `data`, `files`, and `json`, returning
     a `ContentStream` implementation.
     """
-    if data is None:
+    if not data:
         if json is not None:
             return JSONStream(json=json)
         elif files:
@@ -331,7 +331,7 @@ def encode(
         else:
             return ByteStream(body=b"")
     elif isinstance(data, dict):
-        if files is not None:
+        if files:
             return MultipartStream(data=data, files=files, boundary=boundary)
         else:
             return URLEncodedStream(data=data)
index 6aa91f40d4bd476d278af716eb3ae810cc5ff16c..c5eb961ddca014a45670c999d1597a240fd6318b 100644 (file)
@@ -189,6 +189,18 @@ async def test_multipart_data_and_files_content():
     )
 
 
+@pytest.mark.asyncio
+async def test_empty_request():
+    stream = encode(data={}, files={})
+    sync_content = b"".join([part for part in stream])
+    async_content = b"".join([part async for part in stream])
+
+    assert stream.can_replay()
+    assert stream.get_headers() == {}
+    assert sync_content == b""
+    assert async_content == b""
+
+
 def test_invalid_argument():
     with pytest.raises(TypeError):
         encode(123)