Skip to content

Commit

Permalink
[3.5] Make sure the closing boundary is written when calling 'write' …
Browse files Browse the repository at this point in the history
…on an empty MultipartWriter (#3520)

(cherry picked from commit 0ad5d90)

Co-authored-by: Arthur Darcet <[email protected]>
  • Loading branch information
arthurdarcet authored and asvetlov committed May 11, 2019
1 parent 75e5bab commit f6bb07f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
9 changes: 3 additions & 6 deletions aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,9 @@ def __iter__(self) -> Iterator[_Part]:
def __len__(self) -> int:
return len(self._parts)

def __bool__(self) -> bool:
return True

_valid_tchar_regex = re.compile(br"\A[!#$%&'*+\-.^_`|~\w]+\Z")
_invalid_qdtext_char_regex = re.compile(br"[\x00-\x08\x0A-\x1F\x7F]")

Expand Down Expand Up @@ -836,9 +839,6 @@ def append_form(
@property
def size(self) -> Optional[int]:
"""Size of the payload."""
if not self._parts:
return 0

total = 0
for part, encoding, te_encoding in self._parts:
if encoding or te_encoding or part.size is None:
Expand All @@ -856,9 +856,6 @@ def size(self) -> Optional[int]:
async def write(self, writer: Any,
close_boundary: bool=True) -> None:
"""Write body."""
if not self._parts:
return

for part, encoding, te_encoding in self._parts:
await writer.write(b'--' + self._boundary + b'\r\n')
await writer.write(part._binary_headers)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ async def test_reading_skips_prelude(self) -> None:


async def test_writer(writer) -> None:
assert writer.size == 0
assert writer.size == 7
assert writer.boundary == ':'


Expand Down Expand Up @@ -848,6 +848,11 @@ async def test_writer_write_no_close_boundary(buf, stream) -> None:
b'\r\n') == bytes(buf))


async def test_writer_write_no_parts(buf, stream, writer) -> None:
await writer.write(stream)
assert b'--:--\r\n' == bytes(buf)


async def test_writer_serialize_with_content_encoding_gzip(buf, stream,
writer):
writer.append('Time to Relax!', {CONTENT_ENCODING: 'gzip'})
Expand Down
24 changes: 22 additions & 2 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,27 @@ async def handler(request):
app.router.add_post('/', handler)
client = await aiohttp_client(app)

resp = await client.post('/', data=writer, headers=writer.headers)
resp = await client.post('/', data=writer)
assert 200 == resp.status
await resp.release()


async def test_multipart_empty(aiohttp_client) -> None:
with multipart.MultipartWriter() as writer:
pass

async def handler(request):
reader = await request.multipart()
assert isinstance(reader, multipart.MultipartReader)
async for part in reader:
assert False, 'Unexpected part found in reader: {!r}'.format(part)
return web.Response()

app = web.Application()
app.router.add_post('/', handler)
client = await aiohttp_client(app)

resp = await client.post('/', data=writer)
assert 200 == resp.status
await resp.release()

Expand Down Expand Up @@ -264,7 +284,7 @@ async def handler(request):
app.router.add_post('/', handler)
client = await aiohttp_client(app)

resp = await client.post('/', data=writer, headers=writer.headers)
resp = await client.post('/', data=writer)
assert 200 == resp.status
await resp.release()

Expand Down

0 comments on commit f6bb07f

Please sign in to comment.