async def test_app_session_websocket_return(session_app: Quart) -> None: test_client = session_app.test_client() async with test_client.websocket("/ws_return/") as test_websocket: with pytest.raises(WebsocketResponseError): await test_websocket.receive() session_app.session_interface.open_session.assert_called() # type: ignore session_app.session_interface.save_session.assert_called() # type: ignore
async def test_app_session_websocket(session_app: Quart) -> None: test_client = session_app.test_client() async with test_client.websocket("/ws/") as test_websocket: await test_websocket.receive() session_app.session_interface.open_session.assert_called() # type: ignore session_app.session_interface.save_session.assert_not_called( ) # type: ignore
async def test_propagation(debug: bool, testing: bool, raises: bool, http_scope: HTTPScope) -> None: app = Quart(__name__) @app.route("/") async def exception() -> ResponseReturnValue: raise SimpleError() app.debug = debug app.testing = testing test_client = app.test_client() if raises: with pytest.raises(SimpleError): await app.handle_request( Request( "GET", "http", "/", b"", Headers(), "", "1.1", http_scope, send_push_promise=no_op_push, )) else: response = await test_client.get("/") assert response.status_code == 500
async def test_app_after_request_handler_exception(basic_app: Quart) -> None: @basic_app.after_request def after(_: Response) -> None: raise Exception() test_client = basic_app.test_client() response = await test_client.get("/exception/") assert response.status_code == 500
async def test_app_before_request_exception(basic_app: Quart) -> None: @basic_app.before_request def before() -> None: raise Exception() test_client = basic_app.test_client() response = await test_client.get("/") assert response.status_code == 500
async def test_subdomain() -> None: app = Quart(__name__, static_host='quart.com', host_matching=True) app.config['SERVER_NAME'] = 'quart.com' @app.route('/', subdomain='<subdomain>') def route(subdomain: str) -> str: return subdomain test_client = app.test_client() response = await test_client.get('/', headers={'host': 'sub.quart.com'}) assert (await response.get_data(raw=False)) == 'sub'
async def test_subdomain() -> None: app = Quart(__name__, static_host="quart.com", host_matching=True) app.config["SERVER_NAME"] = "quart.com" @app.route("/", subdomain="<subdomain>") async def route(subdomain: str) -> str: return subdomain test_client = app.test_client() response = await test_client.get("/", headers={"host": "sub.quart.com"}) assert (await response.get_data(raw=False)) == "sub"
async def test_host_matching() -> None: app = Quart(__name__, static_host="quart.com", host_matching=True) @app.route("/", host="quart.com") async def route() -> str: return "" test_client = app.test_client() response = await test_client.get("/", headers={"host": "quart.com"}) assert response.status_code == 200 response = await test_client.get("/", headers={"host": "localhost"}) assert response.status_code == 404
async def test_host_matching() -> None: app = Quart(__name__, static_host='quart.com', host_matching=True) app.config['SERVER_NAME'] = 'quart.com' @app.route('/') def route() -> str: return '' test_client = app.test_client() response = await test_client.get('/', headers={'host': 'quart.com'}) assert response.status_code == 200 response = await test_client.get('/', headers={'host': 'localhost'}) assert response.status_code == 404
async def test_works_without_copy_current_request_context() -> None: app = Quart(__name__) @app.route("/") async def index() -> str: async def within_context() -> None: assert request.path == "/" await asyncio.ensure_future(within_context()) return "" test_client = app.test_client() response = await test_client.get("/") assert response.status_code == 200
async def test_fails_without_copy_current_request_context() -> None: app = Quart(__name__) @app.route('/') async def index() -> str: async def within_context() -> None: assert request.path == '/' await asyncio.ensure_future(within_context()) return '' test_client = app.test_client() response = await test_client.get('/') assert response.status_code == 500
async def test_copy_current_websocket_context() -> None: app = Quart(__name__) @app.websocket('/') async def index() -> None: @copy_current_websocket_context async def within_context() -> None: return websocket.path data = await asyncio.ensure_future(within_context()) await websocket.send(data.encode()) test_client = app.test_client() with test_client.websocket('/') as test_websocket: data = await test_websocket.receive() assert data == b'/'
async def test_copy_current_app_context() -> None: app = Quart(__name__) @app.route("/") async def index() -> str: g.foo = "bar" @copy_current_app_context async def within_context() -> None: assert g.foo == "bar" await asyncio.ensure_future(within_context()) return "" test_client = app.test_client() response = await test_client.get("/") assert response.status_code == 200
async def test_copy_current_app_context() -> None: app = Quart(__name__) @app.route('/') async def index() -> str: g.foo = 'bar' # type: ignore @copy_current_app_context async def within_context() -> None: assert g.foo == 'bar' await asyncio.ensure_future(within_context()) return '' test_client = app.test_client() response = await test_client.get('/') assert response.status_code == 200
async def test_propagation(debug: bool, testing: bool, raises: bool) -> None: app = Quart(__name__) @app.route("/") async def exception() -> ResponseReturnValue: raise SimpleException() app.debug = debug app.testing = testing test_client = app.test_client() if raises: with pytest.raises(SimpleException): await test_client.get("/") else: response = await test_client.get("/") assert response.status_code == 500
async def test_background_task() -> None: app = Quart(__name__) app.config["DATA"] = "data" complete = asyncio.Event() data = None async def background() -> None: nonlocal complete, data data = current_app.config["DATA"] complete.set() @app.route("/") async def index() -> str: app.add_background_task(background) return "" test_client = app.test_client() await test_client.get("/") await complete.wait() assert data == "data"
async def test_app_session(session_app: Quart) -> None: test_client = session_app.test_client() await test_client.get("/") session_app.session_interface.open_session.assert_called() # type: ignore session_app.session_interface.save_session.assert_called() # type: ignore
async def test_app_route_exception(basic_app: Quart) -> None: test_client = basic_app.test_client() response = await test_client.get("/exception/") assert response.status_code == 500