diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 76b630cfea9..f3d2248965b 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -288,6 +288,7 @@ def update_body_from_data(self, data): 'attempt to send text data instead of binary' self.body = data if not self.chunked and isinstance(data, io.BytesIO): + # Not chunking if content-length can be determined size = len(data.getbuffer()) self.headers[hdrs.CONTENT_LENGTH] = str(size) self.chunked = False diff --git a/tests/test_client_functional2.py b/tests/test_client_functional2.py index c4ac3c46547..bcf86e2b805 100644 --- a/tests/test_client_functional2.py +++ b/tests/test_client_functional2.py @@ -116,7 +116,7 @@ def go(): self.loop.run_until_complete(go()) - def test_raw_post_data(self): + def test_post_data_bytesio(self): data = b'some buffer' @asyncio.coroutine @@ -137,3 +137,25 @@ def go(): yield from resp.release() self.loop.run_until_complete(go()) + + def test_post_data_with_bytesio_file(self): + data = b'some buffer' + + @asyncio.coroutine + def handler(request): + post_data = yield from request.post() + self.assertEqual(['file'], list(post_data.keys())) + self.assertEqual(data, post_data['file'].file.read()) + return web.Response() + + @asyncio.coroutine + def go(): + app, srv, url = yield from self.create_server() + app.router.add_route('post', '/', handler) + resp = yield from self.client.post( + url+'/', + data={'file': io.BytesIO(data)}) + self.assertEqual(200, resp.status) + yield from resp.release() + + self.loop.run_until_complete(go())