def app(self): async def handle_root(request): # pylint: disable=W0613, unused-argument return web.Response(text="root") async def handle_clear(request): response = web.Response(text="clear") await schedule_close_all_session_ws(request, response) return response async def handle_websocket(request): async with session_ws(request) as wsr: # pylint: disable=W0613, unused-argument async for msg in wsr: await wsr.send_str(msg.data) return wsr app = web.Application(middlewares=[ aiohttp_session.session_middleware( aiohttp_session.SimpleCookieStorage(cookie_name=COOKIE_NAME)), session_ws_middleware, ]) app.router.add_get("/", handle_root) app.router.add_get("/clear", handle_clear) app.router.add_get("/ws", handle_websocket) setup_session_ws(app, SessionWSRegistry()) return app
def make_app(): app = web.Application(middlewares=[ aiohttp_session.session_middleware( aiohttp_session.SimpleCookieStorage()), session_ws_middleware, ]) app.router.add_get("/", handle_root) app.router.add_get("/reset", handle_reset) app.router.add_get("/ws", handle_websocket) setup_session_websockets(app, SessionWSRegistry()) return app
async def test_async_id_factory(self): called_with = None async def dummy_id_factory(request): nonlocal called_with called_with = request return id(request) registry = SessionWSRegistry(id_factory=dummy_id_factory) request, session = self.make_request_session_tuple() await registry.new_id(request) assert called_with == request assert session[DEFAULT_SESSION_KEY] == id(request)
def test_unregister_not_last(self, wsr): """ KVP remains """ registry = SessionWSRegistry() wsr2 = make_mock_wsr() registry._registry[0] = set([wsr, wsr2]) registry.unregister(0, wsr) assert registry.get(0) == set([wsr2])
def app(self): async def handle_websocket(request): async with session_ws(request) as wsr: session_ws_id = await get_session_ws_id(request) async for msg in wsr: # pylint: disable=W0612, unused-variable await wsr.send_str(str(session_ws_id)) return wsr app = web.Application(middlewares=[ aiohttp_session.session_middleware( aiohttp_session.SimpleCookieStorage(cookie_name=COOKIE_NAME)) ]) app.router.add_get("/ws", handle_websocket) setup_session_ws(app, SessionWSRegistry()) return app
def registry(): return SessionWSRegistry()
async def test_ensure_id(self, initial, factory, expected): registry = SessionWSRegistry(id_factory=factory) request, session = self.make_request_session_tuple(initial) await registry.ensure_id(request) assert session[DEFAULT_SESSION_KEY] == expected