Skip to content

Instantly share code, notes, and snippets.

@oddskool
Created February 3, 2012 14:35
Show Gist options
  • Save oddskool/1730451 to your computer and use it in GitHub Desktop.
Save oddskool/1730451 to your computer and use it in GitHub Desktop.
Sample test case for a Tornado WS that asynchronously calls a third-party WS
from tornado import ioloop , gen
from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from tornado.web import asynchronous, RequestHandler, Application
from tornado.testing import AsyncHTTPTestCase
import sys
from tornado.ioloop import IOLoop
class MainHandler(RequestHandler):
url = u'http://www.google.com'
@asynchronous
@gen.engine
def get(self):
http_client = AsyncHTTPClient()
request = HTTPRequest(self.url)
response = yield gen.Task(http_client.fetch, request)
response.rethrow()
self.write(repr(response))
self.finish()
application = Application([(r"/", MainHandler),], debug=True)
class Test(AsyncHTTPTestCase):
def get_app(self):
return application
def test_task_handler(self):
response = self.fetch('/')
print >>sys.stderr, response
def get_new_ioloop(self):
return IOLoop.instance()
if __name__ == "__main__":
application.listen(8888)
ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment