Skip to content

Commit

Permalink
Add test for BytesIO in multipart request
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Sep 10, 2015
1 parent 7319f0a commit d15c1c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion tests/test_client_functional2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

0 comments on commit d15c1c1

Please sign in to comment.