def test_lifespan_invalid(self): app = async_asgi.ASGIApp('eio') handler = app({'type': 'lifespan'}) receive = AsyncMock(return_value={'type': 'lifespan.foo'}) send = AsyncMock() _run(handler(receive, send)) send.mock.assert_not_called()
def test_lifespan_shutdown(self): app = async_asgi.ASGIApp('eio') handler = app({'type': 'lifespan'}) receive = AsyncMock(return_value={'type': 'lifespan.shutdown'}) send = AsyncMock() _run(handler(receive, send)) send.mock.assert_called_once_with( {'type': 'lifespan.shutdown.complete'})
def test_engineio_routing(self): mock_server = mock.MagicMock() mock_server.handle_request = AsyncMock() app = async_asgi.ASGIApp(mock_server) scope = {'type': 'http', 'path': '/engine.io/'} handler = app(scope) _run(handler('receive', 'send')) mock_server.handle_request.mock.assert_called_once_with( scope, 'receive', 'send')
def test_create_app(self): app = async_asgi.ASGIApp('eio', 'other_app', static_files='static_files', engineio_path='/foo') self.assertEqual(app.engineio_server, 'eio') self.assertEqual(app.other_asgi_app, 'other_app') self.assertEqual(app.static_files, 'static_files') self.assertEqual(app.engineio_path, 'foo'),
def test_not_found(self): app = async_asgi.ASGIApp('eio') handler = app({'type': 'http', 'path': '/foo'}) receive = AsyncMock(return_value={'type': 'http.request'}) send = AsyncMock() _run(handler(receive, send)) send.mock.assert_any_call({ 'type': 'http.response.start', 'status': 404, 'headers': [(b'Content-Type', b'text/plain')] }) send.mock.assert_any_call({ 'type': 'http.response.body', 'body': b'not found' })
def test_static_file_routing(self): root_dir = os.path.dirname(__file__) app = async_asgi.ASGIApp('eio', static_files={ '/foo': { 'content_type': 'text/html', 'filename': root_dir + '/index.html' } }) handler = app({'type': 'http', 'path': '/foo'}) receive = AsyncMock(return_value={'type': 'http.request'}) send = AsyncMock() _run(handler(receive, send)) send.mock.assert_called_with({ 'type': 'http.response.body', 'body': b'<html></html>\n' })
def test_other_app_routing(self): other_app = mock.MagicMock() app = async_asgi.ASGIApp('eio', other_app) scope = {'type': 'http', 'path': '/foo'} app(scope) other_app.assert_called_once_with(scope)