def test_start_runserver_no_loop_argument(tmpworkdir, loop): mktree( tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') def app(): a = web.Application() a.router.add_get('/', hello) return a """ }) asyncio.set_event_loop(loop) aux_app, observer, aux_port, _ = runserver(app_path='app.py') assert len(observer._handlers) == 1 event_handlers = list(observer._handlers.values())[0] code_event_handler = next(eh for eh in event_handlers if isinstance(eh, PyCodeEventHandler)) async def check_callback(session): async with session.get('http://localhost:8000/') as r: assert r.status == 200 assert r.headers['content-type'].startswith('text/html') text = await r.text() assert '<h1>hello world</h1>' in text assert '<script src="http://localhost:8001/livereload.js"></script>' in text try: loop.run_until_complete(check_server_running(loop, check_callback)) finally: code_event_handler._process.terminate()
def test_start_runserver_app_instance(tmpworkdir, event_loop): mktree( tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') app = web.Application() app.router.add_get('/', hello) """ }) args = runserver(app_path="app.py", host="foobar.com", main_port=0, aux_port=8001) aux_app = args["app"] aux_port = args["port"] assert isinstance(aux_app, aiohttp.web.Application) assert aux_port == 8001 assert len(aux_app.on_startup) == 1 assert len(aux_app.on_shutdown) == 1 assert len(aux_app.cleanup_ctx) == 1
def test_start_runserver_app_instance(tmpworkdir, loop, caplog): mktree( tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') app = web.Application() app.router.add_get('/', hello) """ }) asyncio.set_event_loop(loop) aux_app, observer, aux_port, _ = runserver(app_path='app.py') assert len(observer._handlers) == 1 event_handlers = list(observer._handlers.values())[0] code_event_handler = next(eh for eh in event_handlers if isinstance(eh, PyCodeEventHandler)) try: loop.run_until_complete(check_server_running(loop, live_reload=True)) finally: code_event_handler._process.terminate()
def test_start_runserver(tmpworkdir, caplog): mktree( tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') async def has_error(request): raise ValueError() def create_app(loop): app = web.Application() app.router.add_get('/', hello) app.router.add_get('/error', has_error) return app""", 'static_dir/foo.js': 'var bar=1;', }) loop = asyncio.new_event_loop() aux_app, observer, aux_port, _ = runserver(app_path='app.py', loop=loop, static_path='static_dir') assert isinstance(aux_app, aiohttp.web.Application) assert aux_port == 8001 assert len(observer._handlers) == 2 event_handlers = next(eh for eh in observer._handlers.values() if len(eh) == 2) code_event_handler = next(eh for eh in event_handlers if isinstance(eh, PyCodeEventHandler)) async def check_callback(session): async with session.get('http://localhost:8000/') as r: assert r.status == 200 assert r.headers['content-type'].startswith('text/html') text = await r.text() assert '<h1>hello world</h1>' in text assert '<script src="http://localhost:8001/livereload.js"></script>' in text async with session.get('http://localhost:8000/error') as r: assert r.status == 500 assert 'raise ValueError()' in (await r.text()) try: loop.run_until_complete(check_server_running(loop, check_callback)) finally: code_event_handler._process.terminate() assert ( 'adev.server.dft INFO: pre-check enabled, checking app factory\n' 'adev.server.dft INFO: Starting dev server at http://localhost:8000 ●\n' 'adev.server.dft INFO: Starting aux server at http://localhost:8001 ◆\n' 'adev.server.dft INFO: serving static files from ./static_dir/ at http://localhost:8001/static/\n' ) == caplog
def test_start_runserver(tmpworkdir, smart_caplog): mktree( tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') async def has_error(request): raise ValueError() def create_app(): app = web.Application() app.router.add_get('/', hello) app.router.add_get('/error', has_error) return app""", 'static_dir/foo.js': 'var bar=1;', }) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) args = runserver(app_path='app.py', static_path='static_dir') aux_app = args["app"] aux_port = args["port"] assert isinstance(aux_app, aiohttp.web.Application) assert aux_port == 8001 for startup in aux_app.on_startup: loop.run_until_complete(startup(aux_app)) async def check_callback(session): async with session.get('http://localhost:8000/') as r: assert r.status == 200 assert r.headers['content-type'].startswith('text/html') text = await r.text() assert '<h1>hello world</h1>' in text assert '<script src="http://localhost:8001/livereload.js"></script>' in text async with session.get('http://localhost:8000/error') as r: assert r.status == 500 assert 'raise ValueError()' in (await r.text()) try: loop.run_until_complete(check_server_running(check_callback)) finally: for shutdown in aux_app.on_shutdown: loop.run_until_complete(shutdown(aux_app)) assert ( 'adev.server.dft INFO: Starting aux server at http://localhost:8001 ◆\n' 'adev.server.dft INFO: serving static files from ./static_dir/ at http://localhost:8001/static/\n' 'adev.server.dft INFO: Starting dev server at http://localhost:8000 ●\n' ) in smart_caplog loop.run_until_complete( asyncio.sleep(.25)) # TODO(aiohttp 4): Remove this hack
def test_start_runserver_app_instance(tmpworkdir, loop): mktree(tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') app = web.Application() app.router.add_get('/', hello) """ }) aux_app, aux_port, _, _ = runserver(app_path='app.py', host='foobar.com') assert isinstance(aux_app, aiohttp.web.Application) assert aux_port == 8001 assert len(aux_app.on_startup) == 2 assert len(aux_app.on_shutdown) == 2
def test_start_runserver_no_loop_argument(tmpworkdir, loop): mktree( tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') def app(): a = web.Application() a.router.add_get('/', hello) return a """ }) asyncio.set_event_loop(loop) aux_app, aux_port, _ = runserver(app_path='app.py') assert isinstance(aux_app, aiohttp.web.Application) assert aux_port == 8001 assert len(aux_app.on_startup) == 1 assert len(aux_app.on_shutdown) == 1