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

Send raw files as is #153

Merged
merged 1 commit into from
Sep 29, 2014
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
25 changes: 23 additions & 2 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import asyncio
import collections
import http.cookies
import json
import io
import inspect
import io
import json
import mimetypes
import random
import time
import urllib.parse
Expand Down Expand Up @@ -373,6 +374,20 @@ def update_body_from_data(self, data):
if 'CONTENT-LENGTH' not in self.headers and self.chunked is None:
self.chunked = True

elif isinstance(data, io.IOBase):
assert not isinstance(data, io.StringIO), \
'attempt to send text data instead of binary'
self.body = data
self.chunked = True
if hasattr(data, 'mode'):
if data.mode == 'r':
raise ValueError('file {!r} should be open in binary mode'
''.format(data))
if 'CONTENT-TYPE' not in self.headers and hasattr(data, 'name'):
mime = mimetypes.guess_type(data.name)[0]
mime = 'application/octet-stream' if mime is None else mime
self.headers['CONTENT-TYPE'] = mime

else:
if not isinstance(data, helpers.FormData):
data = helpers.FormData(data)
Expand Down Expand Up @@ -474,6 +489,12 @@ def write_bytes(self, request, reader):
except streams.EofStream:
break

elif isinstance(self.body, io.IOBase):
chunk = self.body.read(self.chunked)
while chunk:
request.write(chunk)
chunk = self.body.read(self.chunked)

else:
if isinstance(self.body, (bytes, bytearray)):
self.body = (self.body,)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import asyncio
import inspect
import io
import time
import unittest
import unittest.mock
Expand Down Expand Up @@ -634,6 +635,24 @@ def gen():
unittest.mock.call(b'\r\n'),
unittest.mock.call(b'0\r\n\r\n')])

def test_data_file(self):
req = ClientRequest(
'POST', 'http://python.org/', data=io.BytesIO(b'*' * 2),
loop=self.loop)
self.assertTrue(req.chunked)
self.assertTrue(isinstance(req.body, io.IOBase))
self.assertEqual(req.headers['TRANSFER-ENCODING'], 'chunked')

resp = req.send(self.transport, self.protocol)
self.assertIsInstance(req._writer, asyncio.Future)
self.loop.run_until_complete(resp.wait_for_close())
self.assertIsNone(req._writer)
self.assertEqual(
self.transport.write.mock_calls[-3:],
[unittest.mock.call(b'*' * 2),
unittest.mock.call(b'\r\n'),
unittest.mock.call(b'0\r\n\r\n')])

def test_data_stream_exc(self):
fut = asyncio.Future(loop=self.loop)

Expand Down
22 changes: 13 additions & 9 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,21 +470,25 @@ def test_POST_FILES_SINGLE(self):
url = httpd.url('method', 'post')

with open(__file__) as f:
with self.assertRaises(ValueError):
self.loop.run_until_complete(
client.request('post', url, data=f, loop=self.loop))

def test_POST_FILES_SINGLE_BINARY(self):
with test_utils.run_server(self.loop, router=Functional) as httpd:
url = httpd.url('method', 'post')

with open(__file__, 'rb') as f:
r = self.loop.run_until_complete(
client.request('post', url, data=f, loop=self.loop))

content = self.loop.run_until_complete(r.json())

f.seek(0)
filename = os.path.split(f.name)[-1]

self.assertEqual(1, len(content['multipart-data']))
self.assertEqual(
filename, content['multipart-data'][0]['name'])
self.assertEqual(
filename, content['multipart-data'][0]['filename'])
self.assertEqual(
f.read(), content['multipart-data'][0]['data'])
self.assertEqual(0, len(content['multipart-data']))
self.assertEqual(content['content'], f.read().decode())
self.assertEqual(content['headers']['Content-Type'],
'text/x-python')
self.assertEqual(r.status, 200)
r.close()

Expand Down