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 read bytes count #463

Closed
wants to merge 3 commits into from
Closed
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 aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def read_chunk(self, size=chunk_size):
'Content-Length required for chunked read'
chunk_size = min(size, self._length - self._read_bytes)
chunk = yield from self._content.read(chunk_size)
self._read_bytes += chunk_size
self._read_bytes += len(chunk)
if self._read_bytes == self._length:
self._at_eof = True
assert b'\r\n' == (yield from self._content.readline()), \
Expand Down
30 changes: 30 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ def readline(self):
return self.content.readline()


class StreamWithShortenRead(Stream):

def __init__(self, content):
self._first = True
super().__init__(content)

@asyncio.coroutine
def read(self, size=None):
if size is not None and self._first:
self._first = False
size = size // 2
return super().read(size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be return (yield from super().read(size)).
I've fixed it already.



class MultipartResponseWrapperTestCase(TestCase):

def setUp(self):
Expand Down Expand Up @@ -148,6 +162,22 @@ def test_read_chunk_requires_content_length(self):
with self.assertRaises(AssertionError):
yield from obj.read_chunk()

def test_read_chunk_properly_counts_read_bytes(self):
expected = b'.' * 10
size = len(expected)
obj = aiohttp.multipart.BodyPartReader(
self.boundary, {'CONTENT-LENGTH': size},
StreamWithShortenRead(expected + b'\r\n--:--'))
result = bytearray()
while True:
chunk = yield from obj.read_chunk()
if not chunk:
break
result.extend(chunk)
self.assertEqual(size, len(result))
self.assertEqual(b'.' * size, result)
self.assertTrue(obj.at_eof())

def test_read_does_reads_boundary(self):
stream = Stream(b'Hello, world!\r\n--:')
obj = aiohttp.multipart.BodyPartReader(
Expand Down