Skip to content

Commit

Permalink
async socket unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Feb 8, 2017
1 parent 039eb59 commit 7f6619f
Show file tree
Hide file tree
Showing 11 changed files with 648 additions and 193 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ docs/_build
venv*
.eggs
.ropeproject
tags
.idea
.vscode
htmlcov
*.swp
2 changes: 1 addition & 1 deletion engineio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .server import Server
if sys.version_info >= (3, 5): # pragma: no cover
from .asyncio_server import AsyncServer
else:
else: # pragma: no cover
AsyncServer = None

__version__ = '1.1.2'
Expand Down
2 changes: 1 addition & 1 deletion engineio/async_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def make_response(status, headers, payload):
headers=headers)


class WebSocket(object):
class WebSocket(object): # pragma: no cover
"""
This wrapper class provides a aiohttp WebSocket interface that is
somewhat compatible with eventlet's implementation.
Expand Down
4 changes: 2 additions & 2 deletions engineio/asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ async def disconnect(self, sid=None):
is not given, then all clients are closed.
"""
if sid is not None:
self._get_socket(sid).close()
await self._get_socket(sid).close()
del self.sockets[sid]
else:
for client in six.itervalues(self.sockets):
client.close()
await client.close()
self.sockets = {}

async def handle_request(self, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions engineio/asyncio_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ async def _websocket_handler(self, ws):
# start separate writer thread
async def writer():
while True:
packets = None
try:
packets = await self.poll()
except IOError:
Expand Down
56 changes: 56 additions & 0 deletions tests/test_async_aiohttp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys
import unittest

import six
if six.PY3:
from unittest import mock
else:
import mock

if sys.version_info >= (3, 5):
from aiohttp import web
from engineio import async_aiohttp


@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')
class AiohttpTests(unittest.TestCase):
@mock.patch('aiohttp.web_urldispatcher.UrlDispatcher.add_route')
def test_create_route(self, add_route):
app = web.Application()
mock_server = mock.MagicMock()
async_aiohttp.create_route(app, mock_server, '/foo')
self.assertEqual(add_route.call_count, 2)
add_route.assert_any_call('GET', '/foo', mock_server.handle_request)
add_route.assert_any_call('POST', '/foo', mock_server.handle_request)

def test_translate_request(self):
request = mock.MagicMock()
request._message.method = 'PUT'
request._message.path = '/foo/bar?baz=1'
request._message.version = (1, 1)
request._message.headers = {'a': 'b', 'c-c': 'd', 'c_c': 'e',
'content-type': 'application/json',
'content-length': 123}
request._payload = b'hello world'
environ = async_aiohttp.translate_request(request)
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_C': 'd,e',
'RAW_URI': '/foo/bar?baz=1',
'SERVER_PROTOCOL': 'HTTP/1.1',
'wsgi.input': b'hello world',
'aiohttp.request': request,
}
for k, v in expected_environ.items():
self.assertEqual(v, environ[k])
self.assertTrue(environ['HTTP_C_C'] == 'd,e' or environ['HTTP_C_C'] == 'e,d')
@mock.patch('engineio.async_aiohttp.aiohttp.web.Response')
def test_make_response(self, Response):
async_aiohttp.make_response('202 ACCEPTED', 'headers', 'payload')
Response.assert_called_once_with(body='payload', status=202,
headers='headers')
Loading

0 comments on commit 7f6619f

Please sign in to comment.