Skip to content

Commit

Permalink
Fix bug when MultipartEncoder is asked to encode zero parts
Browse files Browse the repository at this point in the history
  • Loading branch information
thauk-copperleaf committed Jun 7, 2018
1 parent 6d74296 commit abe5fc5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requests_toolbelt/multipart/encoder.py
Expand Up @@ -190,7 +190,7 @@ def _load(self, amount):
part = self._current_part or self._next_part()
while amount == -1 or amount > 0:
written = 0
if not part.bytes_left_to_write():
if part and not part.bytes_left_to_write():
written += self._write(b'\r\n')
written += self._write_boundary()
part = self._next_part()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_multipart_encoder.py
Expand Up @@ -311,6 +311,12 @@ def test_accepts_custom_headers(self):
output = m.read().decode('utf-8')
assert output.index('X-My-Header: my-value\r\n') > 0

def test_no_parts(self):
fields = []
boundary = '--90967316f8404798963cce746a4f4ef9'
m = MultipartEncoder(fields=fields, boundary=boundary)
output = m.read().decode('utf-8')
assert output == '----90967316f8404798963cce746a4f4ef9--\r\n'

if __name__ == '__main__':
unittest.main()

0 comments on commit abe5fc5

Please sign in to comment.