]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Remove routing decorators in `test_testclient.py` (#1502)
authorAmin Alaee <mohammadamin.alaee@gmail.com>
Thu, 10 Feb 2022 11:44:36 +0000 (12:44 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Feb 2022 11:44:36 +0000 (11:44 +0000)
* Remove routing decorators in test_testclient.py

* Remove routing decorators in test_testclient.py

* fix lint

tests/test_testclient.py

index eb47a59c2b14269f352323482cf086d98442f1a2..22f0b3880824950922986d0d0e5827af74070d13 100644 (file)
@@ -10,6 +10,7 @@ import trio.lowlevel
 from starlette.applications import Starlette
 from starlette.middleware import Middleware
 from starlette.responses import JSONResponse
+from starlette.routing import Route
 from starlette.websockets import WebSocket, WebSocketDisconnect
 
 if sys.version_info >= (3, 7):  # pragma: no cover
@@ -19,14 +20,18 @@ else:  # pragma: no cover
     asyncio_current_task = asyncio.Task.current_task
     from contextlib2 import asynccontextmanager
 
-mock_service = Starlette()
 
-
-@mock_service.route("/")
 def mock_service_endpoint(request):
     return JSONResponse({"mock": "example"})
 
 
+mock_service = Starlette(
+    routes=[
+        Route("/", endpoint=mock_service_endpoint),
+    ]
+)
+
+
 def current_task():
     # anyio's TaskInfo comparisons are invalid after their associated native
     # task object is GC'd https://github.com/agronholm/anyio/issues/324
@@ -42,14 +47,13 @@ def current_task():
     raise RuntimeError(f"unsupported asynclib={asynclib_name}")  # pragma: no cover
 
 
-startup_error_app = Starlette()
-
-
-@startup_error_app.on_event("startup")
 def startup():
     raise RuntimeError()
 
 
+startup_error_app = Starlette(on_startup=[startup])
+
+
 def test_use_testclient_in_endpoint(test_client_factory):
     """
     We should be able to use the test client within applications.
@@ -58,14 +62,13 @@ def test_use_testclient_in_endpoint(test_client_factory):
     during tests or in development.
     """
 
-    app = Starlette()
-
-    @app.route("/")
     def homepage(request):
         client = test_client_factory(mock_service)
         response = client.get("/")
         return JSONResponse(response.json())
 
+    app = Starlette(routes=[Route("/", endpoint=homepage)])
+
     client = test_client_factory(app)
     response = client.get("/")
     assert response.json() == {"mock": "example"}
@@ -103,12 +106,14 @@ def test_use_testclient_as_contextmanager(test_client_factory, anyio_backend_nam
         shutdown_task = current_task()
         shutdown_loop = get_identity()
 
-    app = Starlette(lifespan=lifespan_context)
-
-    @app.route("/loop_id")
     async def loop_id(request):
         return JSONResponse(get_identity())
 
+    app = Starlette(
+        lifespan=lifespan_context,
+        routes=[Route("/loop_id", endpoint=loop_id)],
+    )
+
     client = test_client_factory(app)
 
     with client: