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

Setup uploaded filename if field value is binary and transfer encoding is not specified #349

Merged
merged 1 commit into from
Apr 29, 2015
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
3 changes: 3 additions & 0 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def add_field(self, name, value, *, content_type=None, filename=None,

if isinstance(value, io.IOBase):
self._is_multipart = True
elif isinstance(value, (bytes, bytearray, memoryview)):
if filename is None and content_transfer_encoding is None:
filename = name

type_options = multidict.MultiDict({'name': name})
if filename is not None and not isinstance(filename, str):
Expand Down
48 changes: 48 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,51 @@ def go():
self.assertEqual('keep-alive', resp.headers['CONNECTION'])

self.loop.run_until_complete(go())

def test_upload_file(self):

here = os.path.dirname(__file__)
fname = os.path.join(here, 'software_development_in_picture.jpg')
with open(fname, 'rb') as f:
data = f.read()

@asyncio.coroutine
def handler(request):
form = yield from request.post()
raw_data = form['file'].file.read()
self.assertEqual(data, raw_data)
return web.Response(body=b'OK')

@asyncio.coroutine
def go():
_, _, url = yield from self.create_server('POST', '/', handler)
resp = yield from request('POST', url,
data={'file': data},
loop=self.loop)
self.assertEqual(200, resp.status)

self.loop.run_until_complete(go())

def test_upload_file_object(self):

here = os.path.dirname(__file__)
fname = os.path.join(here, 'software_development_in_picture.jpg')
with open(fname, 'rb') as f:
data = f.read()

@asyncio.coroutine
def handler(request):
form = yield from request.post()
raw_data = form['file'].file.read()
self.assertEqual(data, raw_data)
return web.Response(body=b'OK')

@asyncio.coroutine
def go():
_, _, url = yield from self.create_server('POST', '/', handler)
resp = yield from request('POST', url,
files={'file': open(fname, 'rb')},
loop=self.loop)
self.assertEqual(200, resp.status)

self.loop.run_until_complete(go())