def test_frame_from_json(): uuid = uuid4().hex frame_as_dict = {'name': 'test-frame', 'uuid': uuid, 'kind': Kind.EVENT} frame_as_json = json.dumps(frame_as_dict) f = Frame.from_json(frame_as_json) assert f.name == 'test-frame' assert f.uuid == uuid assert f.kind == Kind.EVENT
async def echo(websocket, path): async for message in websocket: frame = Frame.from_json(message) if frame.kind == Kind.COMMAND and frame.name == 'login': print(f'login-ok', frame.to_dict()) await websocket.send(frame.reply('login-ok').to_json()) else: print(frame.to_dict()) await websocket.send(message)
async def send(self, data): if not self._send_ok: raise ConnectionAbortedError() frame = Frame.from_json(data) if frame.name == 'login': if self._login_ok: self.frame = frame.reply('login-ok').to_json() else: self.frame = frame.reply('login-failed').to_json() return self.frame = data
async def echo(websocket, path): # pragma: no cover async for message in websocket: frame = Frame.from_json(message) if frame.kind == Kind.COMMAND and frame.name == 'login' and frame.data.get( 'token') == 'fail-token': print(frame.to_dict()) print(f'login-fail') await websocket.send(frame.reply('login-fail').to_json()) elif frame.kind == Kind.COMMAND and frame.name == 'login': print(frame.to_dict()) print(f'login-ok') await websocket.send(frame.reply('login-ok').to_json()) else: print(frame.to_dict()) await websocket.send(message)
async def recv(self): data, remote_addr = await self.connection.recv() frame = Frame.from_json(data.decode('utf-8')) return frame