Skip to content

Commit

Permalink
Fix multipart edge cases with data={} and/or files={} (#861)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
b0g3r and florimondmanca committed Mar 16, 2020
1 parent 43ec09c commit 430285f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions httpx/_content_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ 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:
return MultipartStream(data={}, files=files, boundary=boundary)
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)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_content_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 430285f

Please sign in to comment.