unit test of tornado http server with multipart form-data request

788 views
Skip to first unread message

pivt

unread,
Jan 10, 2012, 5:42:36 AM1/10/12
to python-...@googlegroups.com
I set up a tornado http server which receive some multipart form-data request, and store some of the arguments into mysql.

I have read the source code of httpserver_test.py, and found:
...
class HTTPServerTest(AsyncHTTPTestCase, LogTrapTestCase):
    def get_app(self):
        return Application([("/echo", EchoHandler),
                            ("/typecheck", TypeCheckHandler),
                            ])

    def test_query_string_encoding(self):
        response = self.fetch("/echo?foo=%C3%A9")
        data = json_decode(response.body)
        self.assertEqual(data, {u"foo": [u"\u00e9"]})
...

It seems the GET request can be easy test, but I want to test the POST request with multipart form-data parameters, how can I do that?

I have search that MultipartPostHandler module could post multipart form data, but I don't know how to integrate it into tornado.

kzahel

unread,
Jan 10, 2012, 11:29:14 PM1/10/12
to Tornado Web Server
create a tornado.httpclient.HTTPRequest object with method POST and
body created by using a function like
http://code.activestate.com/recipes/146306-http-client-to-post-using-multipartform-data/

Peter Bengtsson

unread,
Jan 13, 2012, 1:10:40 PM1/13/12
to python-...@googlegroups.com
I suggest you use my tornado-utils package and use the TestClient
http://www.peterbe.com/plog/goodies-from-tornado-utils-testclient

It's heavily inspired by Django and has saved me many moons of writing tests.
Reply all
Reply to author
Forward
0 new messages