Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wraps websocket handler #1696

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from asyncio import CancelledError, Protocol, ensure_future, get_event_loop
from collections import defaultdict, deque
from functools import partial
from functools import partial, wraps
from inspect import getmodulename, isawaitable, signature, stack
from socket import socket
from ssl import Purpose, SSLContext, create_default_context
Expand Down Expand Up @@ -494,6 +494,7 @@ def response(handler):
else:
routes = []

@wraps(handler)
async def websocket_handler(request, *args, **kwargs):
request.app = self
if not getattr(handler, "__blueprintname__", False):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_url_for_strict_slashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import asyncio

from sanic.blueprints import Blueprint


def test_websocket_bp_route_wraps(app):
"""Tests that blueprint websocket route is wrapped."""
event = asyncio.Event()
bp = Blueprint("test_bp", url_prefix="/bp")

@bp.websocket("/route")
async def test_route(request, ws):
event.set()

@bp.websocket("/route2")
async def test_route2(request, ws):
event.set()

app.blueprint(bp)

uri = app.url_for("test_bp.test_route")
assert uri == "/bp/route"
request, response = app.test_client.websocket(uri)
assert response.opened is True
assert event.is_set()

event.clear()
uri2 = app.url_for("test_bp.test_route2")
assert uri2 == "/bp/route2"
request, response = app.test_client.websocket(uri2)
assert response.opened is True
assert event.is_set()