Search code examples
pythonunit-testingtornado

How do I write tests for torando handler wrapped with stream_body decorator?


I have an async handler, wrapped with "stream_body" decorator, which is used for receiving big files and reading them incrementally similar to this example. However, I don't understand, how can I test this functionality in unittest, as this requires an async client.


Solution

  • You can test a handler that uses stream_request_body without any special features in the client. For example, this test in Tornado itself tests a stream_request_body handler with an ordinary request (which happens to be sent with an AsyncHTTPClient because that's how AsyncHTTPTestCase does things, but it could in principle be done with a synchronous client in another thread).

    If you want to test the streaming properties of the handler, then you may need a more flexible client. For example, a test that uses body_producer in AsyncHTTPClient can control the sending of chunks without uploading everything as fast as possible.