38 lines
995 B
Python
38 lines
995 B
Python
"""纯 ASGI 中间件不能吞掉 WebSocket 生命周期。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
from web.backend.main import SecurityHeadersMiddleware
|
|
from web.backend.session import SessionRenewalMiddleware
|
|
|
|
|
|
async def _websocket_app(scope, receive, send):
|
|
assert scope["type"] == "websocket"
|
|
await send({"type": "websocket.accept"})
|
|
await send({"type": "websocket.close", "code": 1000})
|
|
|
|
|
|
def test_websocket_bypasses_http_middleware():
|
|
messages: list[dict] = []
|
|
|
|
async def send(message):
|
|
messages.append(message)
|
|
|
|
async def receive():
|
|
return {"type": "websocket.connect"}
|
|
|
|
scope = {
|
|
"type": "websocket",
|
|
"path": "/api/douyu/ws/test",
|
|
"headers": [],
|
|
}
|
|
wrapped = SessionRenewalMiddleware(SecurityHeadersMiddleware(_websocket_app))
|
|
asyncio.run(wrapped(scope, receive, send))
|
|
|
|
assert messages == [
|
|
{"type": "websocket.accept"},
|
|
{"type": "websocket.close", "code": 1000},
|
|
]
|