Testing file upload handlers

15 views
Skip to first unread message

Станислав Соколко

unread,
Nov 15, 2016, 12:39:11 AM11/15/16
to python-pulsar
Hello! I have a pulsar handler which uploads files and want to test it. I can successfully test simple handlers with the help of pulsar.apps.wsgi.test_wsgi_environ and pulsar.apps.wsgi.WsgiRequest. The tricky thing in testing file upload handlers is that I want to genetate a test body of the request to give it to test_wsgi_environ method as body param. The body needs to be a file-like stream containing data encoded using multipart/form-data notation. I'm now considering using some 3rd-party library like https://atlee.ca/software/poster/ 
Is there any recommended way to form the body? Or maybe some method in pulsar? I searched for similar code snippets in pulsar tests with no result

lsbardel

unread,
Nov 15, 2016, 10:58:23 AM11/15/16
to python-pulsar
Hi,


The body needs to be a file-like stream containing data encoded using multipart/form-data notation.

You can use the encode_multipart_formdata function from pulsar utils:


from io import BytesIO
from pulsar.utils.httpurl import encode_multipart_formdata

files
= {'testfile': b'svdhgscvhsgdvchsgcvhsavchvchsgdvcd'}
data
, content_type = encode_multipart_formdata(files)
file_data
= BytesIO(data)
 

file_data is what you are after I think, content_type is the value for the Content-Type header.

Станислав Соколко

unread,
Nov 18, 2016, 12:50:25 AM11/18/16
to python-pulsar
Thank you!

вторник, 15 ноября 2016 г., 20:58:23 UTC+5 пользователь lsbardel написал:
Reply all
Reply to author
Forward
0 new messages