async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if scope["type"] in ("http", "websocket"): connection = HTTPConnection(scope) if "user" in connection.cookies: user_auth = connection.cookies["user"] headers = MutableHeaders(scope=scope) headers.setdefault("Authorization", user_auth) await self.app(scope, receive, send)
def test_mutable_headers(): h = MutableHeaders() assert dict(h) == {} h["a"] = "1" assert dict(h) == {"a": "1"} h["a"] = "2" assert dict(h) == {"a": "2"} h.setdefault("a", "3") assert dict(h) == {"a": "2"} h.setdefault("b", "4") assert dict(h) == {"a": "2", "b": "4"} del h["a"] assert dict(h) == {"b": "4"}
def ws_connect( self, path: str, subprotocols: Optional[list[str]] = None, queries: Optional[Params] = None, headers: Optional[Headers] = None, cookies: Optional[Mapping] = None, timeout: Optional[float] = None, ) -> AsyncWebsocketClient: """ Create asynchronous WebSocket Connection. """ if isinstance(headers, Mapping): ci_headers = MutableHeaders(headers=headers) else: ci_headers = MutableHeaders( raw=[ (k.encode("latin-1"), v.encode("latin-1")) for k, v in headers or [] ] ) ci_headers.setdefault("connection", "upgrade") ci_headers.setdefault("sec-websocket-key", "testserver==") ci_headers.setdefault("sec-websocket-version", "13") if subprotocols is not None: ci_headers.setdefault("sec-websocket-protocol", ", ".join(subprotocols)) return AsyncWebsocketClient(self, path, ci_headers, queries, cookies, timeout)