Skip to content

Commit

Permalink
Add support for middleware injection on routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Sep 21, 2022
1 parent ca15294 commit cdfd1bc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sanic_testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sanic_testing.manager import TestManager

__version__ = "22.9.0b1"
__version__ = "22.9.0b2"
__all__ = ("TestManager",)
5 changes: 5 additions & 0 deletions sanic_testing/reusable.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(

def __enter__(self):
self.run()
return self

def __exit__(self, *_):
self.stop()
Expand Down Expand Up @@ -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:")
):
Expand Down
5 changes: 5 additions & 0 deletions sanic_testing/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand Down

0 comments on commit cdfd1bc

Please sign in to comment.