vendredi 13 février 2015

How can I test "uploading a file" using Tornado unit tests?

I'm want to test my web service (built on Tornado) using tornado.testing.AsyncHTTPTestCase. It says here that using POST for AsyncHttpClients should look like the following.



from tornado.testing import AsyncHTTPTestCase
from urllib import urlencode

class ApplicationTestCase(AsyncHTTPTestCase):
def get_app(self):
return app.Application()

def test_file_uploading(self):
url = '/'
filepath = 'uploading_file.zip' # Binary file
data = ??????? # Read from "filepath" and put the generated something into "data"
self.http_client.fetch(self.get_url(url),
self.stop,
method="POST",
data=urlencode(data))
response = self.wait()
self.assertEqual(response.code, 302) # Do assertion

if __name__ == '__main__':
unittest.main()


The problem is that I've no idea what to write at ???????. Are there any utility functions built in Tornado, or is it better to use alternative libraries like Requests?


P.S. ... actually, I've tried using Requests, but my test stopped working because probably I didn't do good for asynchronous tasking



def test_file_uploading(self):
url = '/'
filepath = 'uploading_file.zip' # Binary file
files = {'file':open(filepath,'rb')}
r = requests.post(self.get_url(url),files=files) # Freezes here
self.assertEqual(response.code, 302) # Do assertion

Aucun commentaire:

Enregistrer un commentaire