Skip to content

Commit

Permalink
Merge pull request #2 from crim-ca/nested-multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
fmigneault authored Sep 25, 2024
2 parents 743b152 + cc6ac5f commit 0932cee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
1 change: 0 additions & 1 deletion requests_toolbelt/multipart/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(self, content, encoding):
'No contents part without any header is invalid.'
)
else:
print("==" + content.decode() + "===")
raise ImproperBodyPartContentException(
'content does not contain CR-LF-CR-LF'
)
Expand Down
9 changes: 8 additions & 1 deletion requests_toolbelt/multipart/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,14 @@ def _iter_fields(self):
field = fields.RequestField(name=k, data=file_pointer,
filename=file_name,
headers=file_headers)
field.make_multipart(content_type=file_type)
file_type = file_type or file_headers.get("Content-Type")
file_loc = file_headers.get("Content-Location")
file_dis = (file_headers.get("Content-Disposition") or "").split(";", 1)[0].strip()
field.make_multipart(
content_type=file_type,
content_location=file_loc,
content_disposition=file_dis,
)
yield field

def _prepare_parts(self):
Expand Down
49 changes: 32 additions & 17 deletions tests/test_multipart_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,39 @@ def test_from_responsecaplarge(self):
assert len(decoder_2.parts[1].headers) == 0
assert decoder_2.parts[1].content == b'Body 2, Line 1'

def test_no_content(self):
dec = MultipartDecoder(
b'\r\n'.join([
b'--boundary',
b'Header-1: Header-Value-1',
b'Header-2: Header-Value-2',
b'\r\n'
b'--boundary',
b'Header-3: Header-Value-3',
b'Header-4: Header-Value-4',
b'\r\n'
b'some contents',
b'--boundary--',

]),
content_type='multipart/mixed; boundary="boundary"'
)
def test_no_content_empty_string_and_contents(self):
contents = b'\r\n'.join([
b'--boundary',
b'Header-1: Header-Value-1',
b'Header-2: Header-Value-2',
b'',
b'--boundary',
b'Header-3: Header-Value-3',
b'Header-4: Header-Value-4',
b'',
b'some contents',
b'--boundary',
b'Header-5: Header-Value-5',
b'Header-6: Header-Value-6',
b'',
b'',
b'--boundary--',
])
dec = MultipartDecoder(contents, content_type='multipart/mixed; boundary="boundary"')
assert dec.parts[0].headers == {b'Header-1': b'Header-Value-1', b'Header-2': b'Header-Value-2'}
assert dec.parts[0].content is None
assert dec.parts[1].headers == {b'Header-3': b'Header-Value-3', b'Header-4': b'Header-Value-4'}
assert dec.parts[1].content == b'some contents'
assert dec.parts[2].headers == {b'Header-5': b'Header-Value-5', b'Header-6': b'Header-Value-6'}
assert dec.parts[2].content == b''

def test_no_content_crlf_separator_required(self):
contents = b'\r\n'.join([
b'--boundary',
b'Header-1: Header-Value-1',
b'Header-2: Header-Value-2',
# missing CRLF here
b'--boundary',
])
with pytest.raises(ImproperBodyPartContentException):
MultipartDecoder(contents, content_type='multipart/mixed; boundary="boundary"')

0 comments on commit 0932cee

Please sign in to comment.