Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1525: add missing sub MultipartWriter headers #1530

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CHANGES

- Fix polls demo run application #1487

-
- Fix sub-Multipart messages missing their headers on serialization #1525

1.2.0 (2016-12-17)
------------------
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Steven Seguin
Sviatoslav Bulbakha
Taha Jahangir
Taras Voinarovskyi
Terence Honles
Thomas Grainger
Tolga Tezel
Vaibhav Sagar
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,11 @@ class BodyPartWriter(object):
"""Multipart writer for single body part."""

def __init__(self, obj, headers=None, *, chunk_size=8192):
if headers is None:
if isinstance(obj, MultipartWriter):
if headers is not None:
obj.headers.update(headers)
headers = obj.headers
elif headers is None:
headers = CIMultiDict()
elif not isinstance(headers, CIMultiDict):
headers = CIMultiDict(headers)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,18 +838,50 @@ def test_serialize_multipart(self):
multipart = aiohttp.multipart.MultipartWriter(boundary=':')
multipart.append('foo-bar-baz')
multipart.append_json({'test': 'passed'})
multipart.append_form({'test': 'passed'})
multipart.append_form([('one', 1), ('two', 2)])
sub_multipart = aiohttp.multipart.MultipartWriter(boundary='::')
sub_multipart.append('nested content')
sub_multipart.headers['X-CUSTOM'] = 'test'
multipart.append(sub_multipart)
self.assertEqual(
[b'--:\r\n',
b'Content-Type: text/plain; charset=utf-8\r\n'
b'Content-Length: 11',
b'\r\n\r\n',
b'foo-bar-baz',
b'\r\n',

b'--:\r\n',
b'Content-Type: application/json',
b'\r\n\r\n',
b'{"test": "passed"}',
b'\r\n',

b'--:\r\n',
b'Content-Type: application/x-www-form-urlencoded',
b'\r\n\r\n',
b'test=passed',
b'\r\n',

b'--:\r\n',
b'Content-Type: application/x-www-form-urlencoded',
b'\r\n\r\n',
b'one=1&two=2',
b'\r\n',

b'--:\r\n',
b'Content-Type: multipart/mixed; boundary="::"\r\nX-Custom: test',
b'\r\n\r\n',
b'--::\r\n',
b'Content-Type: text/plain; charset=utf-8\r\n'
b'Content-Length: 14',
b'\r\n\r\n',
b'nested content',
b'\r\n',
b'--::--\r\n',
b'',
b'\r\n',
b'--:--\r\n',
b''],
list(self.part._serialize_multipart(multipart))
Expand Down Expand Up @@ -982,6 +1014,13 @@ def test_filename(self):
self.part.set_content_disposition('related', filename='foo.html')
self.assertEqual('foo.html', self.part.filename)

def test_wrap_multipart(self):
writer = aiohttp.multipart.MultipartWriter(boundary=':')
part = aiohttp.multipart.BodyPartWriter(writer)
self.assertEqual(part.headers, writer.headers)
part.headers['X-Custom'] = 'test'
self.assertEqual(part.headers, writer.headers)


class MultipartWriterTestCase(unittest.TestCase):

Expand Down Expand Up @@ -1048,6 +1087,14 @@ def test_append_form(self):
self.assertEqual(part.headers[CONTENT_TYPE],
'application/x-www-form-urlencoded')

def test_append_multipart(self):
subwriter = aiohttp.multipart.MultipartWriter(boundary=':')
subwriter.append_json({'foo': 'bar'})
self.writer.append(subwriter, {CONTENT_TYPE: 'test/passed'})
self.assertEqual(1, len(self.writer))
part = self.writer.parts[0]
self.assertEqual(part.headers[CONTENT_TYPE], 'test/passed')

def test_serialize(self):
self.assertEqual([b''], list(self.writer.serialize()))

Expand Down