Skip to content

Commit

Permalink
tornado unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 28, 2018
1 parent e0dc7f1 commit cb1fe75
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
12 changes: 6 additions & 6 deletions engineio/async_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
try:
import tornado.web
import tornado.websocket
except ImportError:
except ImportError: # pragma: no cover
pass
import six


def get_tornado_handler(engineio_server):
class Handler(tornado.websocket.WebSocketHandler):
class Handler(tornado.websocket.WebSocketHandler): # pragma: no cover
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.receive_queue = asyncio.Queue()
Expand Down Expand Up @@ -59,6 +59,9 @@ async def read(self, length=None):
payload = handler.request.body

uri_parts = urlsplit(handler.request.path)
full_uri = handler.request.path
if handler.request.query: # pragma: no cover
full_uri += '?' + handler.request.query
environ = {
'wsgi.input': AwaitablePayload(payload),
'wsgi.errors': sys.stderr,
Expand All @@ -70,7 +73,7 @@ async def read(self, length=None):
'SERVER_SOFTWARE': 'aiohttp',
'REQUEST_METHOD': handler.request.method,
'QUERY_STRING': handler.request.query or '',
'RAW_URI': handler.request.path,
'RAW_URI': full_uri,
'SERVER_PROTOCOL': 'HTTP/%s' % handler.request.version,
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '0',
Expand All @@ -89,9 +92,6 @@ async def read(self, length=None):
continue

key = 'HTTP_%s' % hdr_name.replace('-', '_')
if key in environ:
hdr_value = '%s,%s' % (environ[key], hdr_value)

environ[key] = hdr_value

environ['wsgi.url_scheme'] = environ.get('HTTP_X_FORWARDED_PROTO', 'http')
Expand Down
76 changes: 76 additions & 0 deletions tests/test_async_tornado.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
try:
import asyncio
except ImportError:
pass
import sys
import unittest

import six
try:
import tornado.web
except ImportError:
pass
if six.PY3:
from unittest import mock
else:
import mock

if sys.version_info >= (3, 5):
from engineio import async_tornado


def _run(coro):
"""Run the given coroutine."""
return asyncio.get_event_loop().run_until_complete(coro)


@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')
class TornadoTests(unittest.TestCase):
def test_get_tornado_handler(self):
mock_server = mock.MagicMock()
handler = async_tornado.get_tornado_handler(mock_server)
self.assertTrue(issubclass(handler,
tornado.websocket.WebSocketHandler))

def test_translate_request(self):
mock_handler = mock.MagicMock()
mock_handler.request.method = 'PUT'
mock_handler.request.path = '/foo/bar'
mock_handler.request.query = 'baz=1'
mock_handler.request.version = '1.1'
mock_handler.request.headers = {
'a': 'b',
'c': 'd',
'content-type': 'application/json',
'content-length': 123
}
mock_handler.request.body = b'hello world'
environ = async_tornado.translate_request(mock_handler)
expected_environ = {
'REQUEST_METHOD': 'PUT',
'PATH_INFO': '/foo/bar',
'QUERY_STRING': 'baz=1',
'CONTENT_TYPE': 'application/json',
'CONTENT_LENGTH': 123,
'HTTP_A': 'b',
'HTTP_C': 'd',
'RAW_URI': '/foo/bar?baz=1',
'SERVER_PROTOCOL': 'HTTP/1.1',
# 'wsgi.input': b'hello world',
'tornado.handler': mock_handler,
}
for k, v in expected_environ.items():
self.assertEqual(v, environ[k])
payload = _run(environ['wsgi.input'].read(1))
payload += _run(environ['wsgi.input'].read())
self.assertEqual(payload, b'hello world')

def test_make_response(self):
mock_handler = mock.MagicMock()
mock_environ = {'tornado.handler': mock_handler}
async_tornado.make_response('202 ACCEPTED', [('foo', 'bar')],
b'payload', mock_environ)
mock_handler.set_status.assert_called_once_with(202)
mock_handler.set_header.assert_called_once_with('foo', 'bar')
mock_handler.write.assert_called_once_with(b'payload')
mock_handler.finish.assert_called_once_with()
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ deps=
mock
eventlet
aiohttp
tornado
basepython =
flake8: python3.6
py27: python2.7
Expand Down

0 comments on commit cb1fe75

Please sign in to comment.