]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Remove routing decorators in test_trusted_hosts.py (#1494)
authorAmin Alaee <mohammadamin.alaee@gmail.com>
Thu, 10 Feb 2022 09:31:49 +0000 (10:31 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Feb 2022 09:31:49 +0000 (10:31 +0100)
tests/middleware/test_trusted_host.py

index de9c79e66a82ed8c5e536aef96454db441df1fd8..72452cc2cf668bff3d6fe0677bb4559ba5171e76 100644 (file)
@@ -1,19 +1,23 @@
 from starlette.applications import Starlette
+from starlette.middleware import Middleware
 from starlette.middleware.trustedhost import TrustedHostMiddleware
 from starlette.responses import PlainTextResponse
+from starlette.routing import Route
 
 
 def test_trusted_host_middleware(test_client_factory):
-    app = Starlette()
-
-    app.add_middleware(
-        TrustedHostMiddleware, allowed_hosts=["testserver", "*.testserver"]
-    )
-
-    @app.route("/")
     def homepage(request):
         return PlainTextResponse("OK", status_code=200)
 
+    app = Starlette(
+        routes=[Route("/", endpoint=homepage)],
+        middleware=[
+            Middleware(
+                TrustedHostMiddleware, allowed_hosts=["testserver", "*.testserver"]
+            )
+        ],
+    )
+
     client = test_client_factory(app)
     response = client.get("/")
     assert response.status_code == 200
@@ -34,14 +38,16 @@ def test_default_allowed_hosts():
 
 
 def test_www_redirect(test_client_factory):
-    app = Starlette()
-
-    app.add_middleware(TrustedHostMiddleware, allowed_hosts=["www.example.com"])
-
-    @app.route("/")
     def homepage(request):
         return PlainTextResponse("OK", status_code=200)
 
+    app = Starlette(
+        routes=[Route("/", endpoint=homepage)],
+        middleware=[
+            Middleware(TrustedHostMiddleware, allowed_hosts=["www.example.com"])
+        ],
+    )
+
     client = test_client_factory(app, base_url="https://example.com")
     response = client.get("/")
     assert response.status_code == 200