Skip to content
Myr

Fix multipart edge cases with `data={}` and/or `files={}` (#861)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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

authored by Dima Boger · committed by GitHub · parent 43ec09c · Browse files · 2 files+14 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: florimondmanca <florimond.manca@gmail.com>

Diff stats

2 files+14 added−2 removed

  • tests:+12 added−0 removed
  • other:+2 added−2 removed

2 files+14 added−2 removed

modified:httpx/_content_streams.py+2 added-2 removed
321 unmodified lines
322 """323 Handles encoding the given `data`, `files`, and `json`, returning324 a `ContentStream` implementation.325 """326 if data is None:326 if not data:327 if json is not None:328 return JSONStream(json=json)329 elif files:330 return MultipartStream(data={}, files=files, boundary=boundary)331 else:332 return ByteStream(body=b"")333 elif isinstance(data, dict):334 if files is not None:334 if files:335 return MultipartStream(data=data, files=files, boundary=boundary)336 else:337 return URLEncodedStream(data=data)338 elif isinstance(data, (str, bytes)):
9 unmodified lines
modified:tests/test_content_streams.py+12 added
187 unmodified lines
188 ]189 )190191192@pytest.mark.asyncio193async def test_empty_request():194 stream = encode(data={}, files={})195 sync_content = b"".join([part for part in stream])196 async_content = b"".join([part async for part in stream])197198 assert stream.can_replay()199 assert stream.get_headers() == {}200 assert sync_content == b""201 assert async_content == b""202203204def test_invalid_argument():205 with pytest.raises(TypeError):206 encode(123)