def test_application_as_handler(evloop, mock_socket): app = App(name="testapp") @app.route("/") class Echo(BaseHandler): @asyncio.coroutine def handle(self, ws, **kwargs): from_client = yield from ws.recv() yield from ws.send(from_client) handler = app.as_handler(loop=evloop) @asyncio.coroutine def run_handler(): yield from handler(mock_socket, "/") assert mock_socket.recv.called assert mock_socket.send.called mock_socket.send.assert_called_with("hello") evloop.run_until_complete(run_handler())
def test_application_as_handler_unknown_route(evloop, mock_socket): app = App(name="testapp") @app.route("/") class Echo(BaseHandler): @asyncio.coroutine def handle(self, ws, **kwargs): from_client = yield from ws.recv() yield from ws.send(from_client) handler = app.as_handler(loop=evloop) @asyncio.coroutine def run_handler(): yield from handler(mock_socket, "/echo") assert mock_socket.close.called error = "could not resolve the path /echo tried "\ "{}".format(list(app.router.routes.keys())) mock_socket.close.assert_called_with(code=1011, reason=error) evloop.run_until_complete(run_handler())
def test_run_middleware(): run = App().run_middlewares message = '{"foo": "bar"}' @asyncio.coroutine def testit(message): message = yield from run(message) assert message == {"foo": "bar"} message = yield from run({"foo": "bar"}, on="send") assert message == '{"foo": "bar"}' loop = asyncio.get_event_loop() loop.run_until_complete(testit(message))
import asyncio from yawf import App, BaseHandler app = App(name="test") @app.route("/echo") class Echo(BaseHandler): @asyncio.coroutine def handle(self, *args, **kwargs): app.logger.debug("recieved a websocket connection") while self.websocket.open: app.logger.debug("websocket.open {}".format(self.websocket.open)) app.logger.debug("waiting for value from client") value = yield from self.websocket.recv() if value is None: break app.logger.debug("recieved `{}` from client".format(value)) yield from self.websocket.send(value) app.logger.debug("sent value back to client") app.logger.debug("closing the websocket now.") app.run("localhost", 8765, debug=True)
def test_run_app_debug(evloop, mock_loop): app = App(name="testapp") app.run("localhost", 8765, debug=True, loop=mock_loop) assert app.logger.level == 10 # debug assert app.debug is True
def test_run_app(evloop, mock_loop): app = App(name="testapp") app.run("localhost", 8765, loop=mock_loop) assert mock_loop.run_until_complete.called assert mock_loop.run_forever.called assert mock_loop.close.called
import asyncio from yawf import App, BaseHandler, schemas app = App(name="test") # Our chatroom requires two keys in our json dict # handle -- an id for the user. # message -- the a user would like to send. # this schema will be used to validate both sent and recieved messages class ChatroomSchema(schemas.MessageSchema): handle = schemas.StringField(validators=[ schemas.MinLengthValidator(length=4), schemas.MaxLengthValidator(length=15) ]) message = schemas.StringField() @app.route("/chatroom") class Chatroom(BaseHandler): recv_schema = ChatroomSchema send_schema = ChatroomSchema @asyncio.coroutine def handle(self, ws, *args, **kwargs): app.logger.debug("recieved a websocket connection") while ws.open: value = yield from self.recv_json(ws) if value is None: self.remove_websocket(ws)
def get_app(): from yawf import App return App()
import asyncio from yawf import App from ${name}.schemas import Hello app = App(name="${name}") @app.route("/") @asyncio.coroutine def hello_handler(ws, **kwargs): message = Hello(message="hello, world!") while ws.open: yield from ws.send(Hello.dumps(message)) yield from asyncio.sleep(1)