-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose websocket_class (#3152)
feat: expose websocket_class to the other layers
- Loading branch information
1 parent
93aabd9
commit 78d1a1a
Showing
9 changed files
with
206 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
from litestar import Litestar, WebSocket, websocket_listener | ||
from litestar.types.asgi_types import WebSocketMode | ||
|
||
|
||
class CustomWebSocket(WebSocket): | ||
async def receive_data(self, mode: WebSocketMode) -> str | bytes: | ||
"""Return fixed response for every websocket message.""" | ||
await super().receive_data(mode=mode) | ||
return "Fixed response" | ||
|
||
|
||
@websocket_listener("/") | ||
async def handler(data: str) -> str: | ||
return data | ||
|
||
|
||
app = Litestar([handler], websocket_class=CustomWebSocket) |
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
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
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from typing import Type, Union | ||
|
||
import pytest | ||
|
||
from litestar import Controller, Litestar, Router, WebSocket | ||
from litestar.handlers.websocket_handlers.listener import WebsocketListener, websocket_listener | ||
|
||
RouterWebSocket: Type[WebSocket] = type("RouterWebSocket", (WebSocket,), {}) | ||
ControllerWebSocket: Type[WebSocket] = type("ControllerWebSocket", (WebSocket,), {}) | ||
AppWebSocket: Type[WebSocket] = type("AppWebSocket", (WebSocket,), {}) | ||
HandlerWebSocket: Type[WebSocket] = type("HandlerWebSocket", (WebSocket,), {}) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"handler_websocket_class, controller_websocket_class, router_websocket_class, app_websocket_class, has_default_app_class, expected", | ||
( | ||
(HandlerWebSocket, ControllerWebSocket, RouterWebSocket, AppWebSocket, True, HandlerWebSocket), | ||
(None, ControllerWebSocket, RouterWebSocket, AppWebSocket, True, ControllerWebSocket), | ||
(None, None, RouterWebSocket, AppWebSocket, True, RouterWebSocket), | ||
(None, None, None, AppWebSocket, True, AppWebSocket), | ||
(None, None, None, None, True, WebSocket), | ||
(None, None, None, None, False, WebSocket), | ||
), | ||
ids=( | ||
"Custom class for all layers", | ||
"Custom class for all above handler layer", | ||
"Custom class for all above controller layer", | ||
"Custom class for all above router layer", | ||
"No custom class for layers", | ||
"No default class in app", | ||
), | ||
) | ||
def test_websocket_class_resolution_of_layers( | ||
handler_websocket_class: Union[Type[WebSocket], None], | ||
controller_websocket_class: Union[Type[WebSocket], None], | ||
router_websocket_class: Union[Type[WebSocket], None], | ||
app_websocket_class: Union[Type[WebSocket], None], | ||
has_default_app_class: bool, | ||
expected: Type[WebSocket], | ||
) -> None: | ||
class MyController(Controller): | ||
@websocket_listener("/") | ||
def handler(self, data: str) -> None: | ||
return | ||
|
||
if controller_websocket_class: | ||
MyController.websocket_class = ControllerWebSocket | ||
|
||
router = Router(path="/", route_handlers=[MyController]) | ||
|
||
if router_websocket_class: | ||
router.websocket_class = router_websocket_class | ||
|
||
app = Litestar(route_handlers=[router]) | ||
|
||
if app_websocket_class or not has_default_app_class: | ||
app.websocket_class = app_websocket_class # type: ignore | ||
|
||
route_handler = app.routes[0].route_handler # type: ignore | ||
|
||
if handler_websocket_class: | ||
route_handler.websocket_class = handler_websocket_class # type: ignore | ||
|
||
websocket_class = route_handler.resolve_websocket_class() # type: ignore | ||
assert websocket_class is expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"handler_websocket_class, router_websocket_class, app_websocket_class, has_default_app_class, expected", | ||
( | ||
(HandlerWebSocket, RouterWebSocket, AppWebSocket, True, HandlerWebSocket), | ||
(None, RouterWebSocket, AppWebSocket, True, RouterWebSocket), | ||
(None, None, AppWebSocket, True, AppWebSocket), | ||
(None, None, None, True, WebSocket), | ||
(None, None, None, False, WebSocket), | ||
), | ||
ids=( | ||
"Custom class for all layers", | ||
"Custom class for all above handler layer", | ||
"Custom class for all above router layer", | ||
"No custom class for layers", | ||
"No default class in app", | ||
), | ||
) | ||
def test_listener_websocket_class_resolution_of_layers( | ||
handler_websocket_class: Union[Type[WebSocket], None], | ||
router_websocket_class: Union[Type[WebSocket], None], | ||
app_websocket_class: Union[Type[WebSocket], None], | ||
has_default_app_class: bool, | ||
expected: Type[WebSocket], | ||
) -> None: | ||
class Handler(WebsocketListener): | ||
path = "/" | ||
websocket_class = handler_websocket_class | ||
|
||
def on_receive(self, data: str) -> str: # pyright: ignore | ||
return data | ||
|
||
router = Router(path="/", route_handlers=[Handler]) | ||
|
||
if router_websocket_class: | ||
router.websocket_class = router_websocket_class | ||
|
||
app = Litestar(route_handlers=[router]) | ||
|
||
if app_websocket_class or not has_default_app_class: | ||
app.websocket_class = app_websocket_class # type: ignore | ||
|
||
route_handler = app.routes[0].route_handler # type: ignore | ||
|
||
if handler_websocket_class: | ||
route_handler.websocket_class = handler_websocket_class # type: ignore | ||
|
||
websocket_class = route_handler.resolve_websocket_class() # type: ignore | ||
assert websocket_class is expected |