From cdfd1bc29c9aa2020fadd6a6e1afe041168cf4ad Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Wed, 21 Sep 2022 10:07:41 +0300 Subject: [PATCH] Add support for middleware injection on routes --- sanic_testing/__init__.py | 2 +- sanic_testing/reusable.py | 5 +++++ sanic_testing/testing.py | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sanic_testing/__init__.py b/sanic_testing/__init__.py index e7572b7..fbbb1ac 100644 --- a/sanic_testing/__init__.py +++ b/sanic_testing/__init__.py @@ -1,4 +1,4 @@ from sanic_testing.manager import TestManager -__version__ = "22.9.0b1" +__version__ = "22.9.0b2" __all__ = ("TestManager",) diff --git a/sanic_testing/reusable.py b/sanic_testing/reusable.py index 160bff0..e088e5c 100644 --- a/sanic_testing/reusable.py +++ b/sanic_testing/reusable.py @@ -62,6 +62,7 @@ def __init__( def __enter__(self): self.run() + return self def __exit__(self, *_): self.stop() @@ -114,6 +115,10 @@ def _sanic_endpoint_test( _collect_request ) + for route in self.app.router.routes: + if _collect_request not in route.extra.request_middleware: + route.extra.request_middleware.appendleft(_collect_request) + if uri.startswith( ("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:") ): diff --git a/sanic_testing/testing.py b/sanic_testing/testing.py index 168757b..26c829d 100644 --- a/sanic_testing/testing.py +++ b/sanic_testing/testing.py @@ -372,6 +372,11 @@ async def request( # type: ignore await self.sanic_app._startup() # type: ignore await self.sanic_app._server_event("init", "before") await self.sanic_app._server_event("init", "after") + for route in self.sanic_app.router.routes: + if self._collect_request not in route.extra.request_middleware: + route.extra.request_middleware.appendleft( + self._collect_request + ) if not url.startswith( ("http:", "https:", "ftp:", "ftps://", "//", "ws:", "wss:")