-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Remove special casing of controllers during registration (#…
…3527) * Convert controllers to routers on registration
- Loading branch information
1 parent
b0c8f02
commit 057f813
Showing
3 changed files
with
74 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,40 @@ | ||
from typing import TYPE_CHECKING, Dict | ||
from unittest.mock import MagicMock, call | ||
|
||
import pytest | ||
|
||
from litestar import Controller, Request, Router, get | ||
from litestar.status_codes import HTTP_200_OK | ||
from litestar.testing import create_test_client | ||
|
||
state: Dict[str, str] = {} | ||
|
||
if TYPE_CHECKING: | ||
from litestar.types import AfterResponseHookHandler | ||
@pytest.mark.parametrize("sync", [True, False]) | ||
@pytest.mark.parametrize("layer", ["app", "router", "controller", "handler"]) | ||
def test_after_response_resolution(layer: str, sync: bool) -> None: | ||
mock = MagicMock() | ||
|
||
if sync: | ||
|
||
def create_sync_test_handler(msg: str) -> "AfterResponseHookHandler": | ||
def handler(_: Request) -> None: | ||
state["msg"] = msg | ||
def handler(_: Request) -> None: # pyright: ignore | ||
mock(layer) | ||
|
||
return handler | ||
else: | ||
|
||
async def handler(_: Request) -> None: # type: ignore[misc] | ||
mock(layer) | ||
|
||
def create_async_test_handler(msg: str) -> "AfterResponseHookHandler": | ||
async def handler(_: Request) -> None: | ||
state["msg"] = msg | ||
class MyController(Controller): | ||
path = "/controller" | ||
after_response = handler if layer == "controller" else None | ||
|
||
return handler | ||
@get("/", after_response=handler if layer == "handler" else None) | ||
def my_handler(self) -> None: | ||
return None | ||
|
||
router = Router( | ||
path="/router", route_handlers=[MyController], after_response=handler if layer == "router" else None | ||
) | ||
|
||
@pytest.mark.parametrize("layer", ["app", "router", "controller", "handler"]) | ||
def test_after_response_resolution(layer: str) -> None: | ||
for handler in (create_sync_test_handler(layer), create_async_test_handler(layer)): | ||
state.pop("msg", None) | ||
|
||
class MyController(Controller): | ||
path = "/controller" | ||
after_response = handler if layer == "controller" else None | ||
|
||
@get("/", after_response=handler if layer == "handler" else None) | ||
def my_handler(self) -> None: | ||
return None | ||
|
||
router = Router( | ||
path="/router", route_handlers=[MyController], after_response=handler if layer == "router" else None | ||
) | ||
|
||
with create_test_client(route_handlers=[router], after_response=handler if layer == "app" else None) as client: | ||
response = client.get("/router/controller/") | ||
assert response.status_code == HTTP_200_OK | ||
assert state["msg"] == layer | ||
with create_test_client(route_handlers=[router], after_response=handler if layer == "app" else None) as client: | ||
response = client.get("/router/controller/") | ||
assert response.status_code == HTTP_200_OK | ||
assert all(c == call(layer) for c in mock.call_args_list) |