Пример #1
0
 def test_lifespan_invalid(self):
     app = async_asgi.ASGIApp('eio')
     scope = {'type': 'lifespan'}
     receive = AsyncMock(return_value={'type': 'lifespan.foo'})
     send = AsyncMock()
     _run(app(scope, receive, send))
     send.mock.assert_not_called()
Пример #2
0
    def test_static_file_routing(self):
        root_dir = os.path.dirname(__file__)
        app = async_asgi.ASGIApp(
            'eio',
            static_files={
                '/': root_dir + '/index.html',
                '/foo': {
                    'content_type': 'text/plain',
                    'filename': root_dir + '/index.html',
                },
                '/static': root_dir,
                '/static/test/': root_dir + '/',
            },
        )

        def check_path(path, status_code, content_type, body):
            scope = {'type': 'http', 'path': path}
            receive = AsyncMock(return_value={'type': 'http.request'})
            send = AsyncMock()
            _run(app(scope, receive, send))
            send.mock.assert_any_call(
                {
                    'type': 'http.response.start',
                    'status': status_code,
                    'headers': [
                        (b'Content-Type', content_type.encode('utf-8'))
                    ],
                }
            )
            send.mock.assert_any_call(
                {'type': 'http.response.body', 'body': body.encode('utf-8')}
            )

        check_path('/', 200, 'text/html', '<html></html>\n')
        check_path('/foo', 200, 'text/plain', '<html></html>\n')
        check_path('/static/index.html', 200, 'text/html', '<html></html>\n')
        check_path('/static/foo.bar', 404, 'text/plain', 'Not Found')
        check_path(
            '/static/test/index.html', 200, 'text/html', '<html></html>\n'
        )
        check_path('/bar/foo', 404, 'text/plain', 'Not Found')
        check_path('', 404, 'text/plain', 'Not Found')

        app.static_files[''] = 'index.html'
        check_path('/static/test/', 200, 'text/html', '<html></html>\n')

        app.static_files[''] = {'filename': 'index.html'}
        check_path('/static/test/', 200, 'text/html', '<html></html>\n')

        app.static_files[''] = {
            'filename': 'index.html',
            'content_type': 'image/gif',
        }
        check_path('/static/test/', 200, 'image/gif', '<html></html>\n')

        app.static_files[''] = {'filename': 'test.gif'}
        check_path('/static/test/', 404, 'text/plain', 'Not Found')

        app.static_files = {}
        check_path('/static/test/index.html', 404, 'text/plain', 'Not Found')
Пример #3
0
 def test_lifespan_shutdown(self):
     app = async_asgi.ASGIApp('eio')
     scope = {'type': 'lifespan'}
     receive = AsyncMock(return_value={'type': 'lifespan.shutdown'})
     send = AsyncMock()
     _run(app(scope, receive, send))
     send.mock.assert_called_once_with(
         {'type': 'lifespan.shutdown.complete'})
Пример #4
0
 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'),
Пример #5
0
 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/'}
     _run(app(scope, 'receive', 'send'))
     mock_server.handle_request.mock.assert_called_once_with(
         scope, 'receive', 'send')
Пример #6
0
 def test_lifespan_startup(self):
     app = async_asgi.ASGIApp('eio')
     handler = app({'type': 'lifespan'})
     receive = AsyncMock(return_value={'type': 'lifespan.startup'})
     send = AsyncMock()
     _run(handler(receive, send))
     send.mock.assert_called_once_with(
         {'type': 'lifespan.startup.complete'})
Пример #7
0
 def test_lifespan_invalid(self):
     app = async_asgi.ASGIApp('eio')
     scope = {'type': 'lifespan'}
     receive = AsyncMock(side_effect=[{'type': 'lifespan.foo'},
                                      {'type': 'lifespan.shutdown'}])
     send = AsyncMock()
     _run(app(scope, receive, send))
     send.mock.assert_called_once_with(
         {'type': 'lifespan.shutdown.complete'}
     )
Пример #8
0
 def test_lifespan_startup(self):
     app = async_asgi.ASGIApp('eio')
     scope = {'type': 'lifespan'}
     receive = AsyncMock(side_effect=[{'type': 'lifespan.startup'},
                                      {'type': 'lifespan.shutdown'}])
     send = AsyncMock()
     _run(app(scope, receive, send))
     send.mock.assert_any_call(
         {'type': 'lifespan.startup.complete'}
     )
 def test_create_app(self):
     app = async_asgi.ASGIApp(
         'eio',
         'other_app',
         static_files='static_files',
         engineio_path='/foo',
     )
     assert app.engineio_server == 'eio'
     assert app.other_asgi_app == 'other_app'
     assert app.static_files == 'static_files'
     assert app.engineio_path == 'foo'
Пример #10
0
 def test_not_found(self):
     app = async_asgi.ASGIApp('eio')
     scope = {'type': 'http', 'path': '/foo'}
     receive = AsyncMock(return_value={'type': 'http.request'})
     send = AsyncMock()
     _run(app(scope, 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'})
Пример #11
0
 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_lifespan_shutdown_function_exception(self):
        down = False

        def shutdown():
            raise Exception

        app = async_asgi.ASGIApp('eio', on_shutdown=shutdown)
        scope = {'type': 'lifespan'}
        receive = AsyncMock(return_value={'type': 'lifespan.shutdown'})
        send = AsyncMock()
        _run(app(scope, receive, send))
        send.mock.assert_called_once_with({'type': 'lifespan.shutdown.failed'})
        assert not down
Пример #13
0
    def test_lifespan_startup_function_exception(self):
        up = False

        def startup():
            raise Exception

        app = async_asgi.ASGIApp('eio', on_startup=startup)
        scope = {'type': 'lifespan'}
        receive = AsyncMock(side_effect=[{'type': 'lifespan.startup'}])
        send = AsyncMock()
        _run(app(scope, receive, send))
        send.mock.assert_called_once_with({'type': 'lifespan.startup.failed'})
        assert not up
    def test_lifespan_startup_async_function(self):
        up = False

        async def startup():
            nonlocal up
            up = True

        app = async_asgi.ASGIApp('eio', on_startup=startup)
        scope = {'type': 'lifespan'}
        receive = AsyncMock(return_value={'type': 'lifespan.startup'})
        send = AsyncMock()
        _run(app(scope, receive, send))
        send.mock.assert_called_once_with(
            {'type': 'lifespan.startup.complete'})
        assert up
    def test_lifespan_shutdown_async_function(self):
        down = False

        async def shutdown():
            nonlocal down
            down = True

        app = async_asgi.ASGIApp('eio', on_shutdown=shutdown)
        scope = {'type': 'lifespan'}
        receive = AsyncMock(return_value={'type': 'lifespan.shutdown'})
        send = AsyncMock()
        _run(app(scope, receive, send))
        send.mock.assert_called_once_with(
            {'type': 'lifespan.shutdown.complete'})
        assert down
Пример #16
0
    def test_lifespan_startup_async_function(self):
        up = False

        async def startup():
            nonlocal up
            up = True

        app = async_asgi.ASGIApp('eio', on_startup=startup)
        scope = {'type': 'lifespan'}
        receive = AsyncMock(side_effect=[{'type': 'lifespan.startup'},
                                         {'type': 'lifespan.shutdown'}])
        send = AsyncMock()
        _run(app(scope, receive, send))
        send.mock.assert_any_call(
            {'type': 'lifespan.startup.complete'}
        )
        assert up
Пример #17
0
 def test_other_app_routing(self):
     other_app = AsyncMock()
     app = async_asgi.ASGIApp('eio', other_app)
     scope = {'type': 'http', 'path': '/foo'}
     _run(app(scope, 'receive', 'send'))
     other_app.mock.assert_called_once_with(scope, 'receive', 'send')
Пример #18
0
 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)