def test_middleware_called_if_routed_to_sub_app(route: str, origin: str, expected: str, expected_body: str): app = App( enable_cors=True, cors_config={ "allow_origins": ["example.com", "localhost:8001", "ietf.org"] }, ) @app.route("/") async def index(req, res): res.text = "Hello" sub = App() @sub.route("/") class SubApp: async def get(self, req, res): res.text = "OK" app.mount("/sub", sub) client = create_client(app) res = client.get(route, headers={"origin": origin}) assert res.text == expected_body if not expected: # unknown origin -> no allow-origin header assert "access-control-allow-origin" not in res.headers else: # allowed origin -> allow-origin header" assert "access-control-allow-origin" in res.headers assert res.headers.get("access-control-allow-origin") == expected
def test_reverse_named_sub_app_route(app: App): sub = App("sub") @sub.route("/foo") async def foo(req, res): pass app.mount("/sub", sub) assert app.url_for("sub:foo") == "/sub/foo"
def test_cannot_reverse_unnamed_sub_app_route(app: App): sub = App() @sub.route("/foo") async def foo(req, res): pass app.mount("/sub", sub) with pytest.raises(HTTPError): app.url_for("sub:foo")
def test_mount_extra_static_files_dirs(tmpdir_factory): static_dir = tmpdir_factory.mktemp("staticfiles") _create_asset(static_dir) app = App(static_dir=None) app.mount("assets", static(str(static_dir))) client = create_client(app) response = client.get(f"/assets/{FILE_DIR}/{FILE_NAME}") assert response.status_code == 200 assert response.text == FILE_CONTENTS
def test_access_sub_route(app: App, client): other = App() @other.route("/foo") async def foo(req, res): res.text = "OK" app.mount("/other", other) r = client.get("/other/foo") assert r.status_code == 200 assert r.text == "OK"
def test_middleware_called_if_routed_to_sub_app(app: App, client): with build_middleware() as middleware: app.add_middleware(middleware) sub = App() @sub.route("/home") async def home(req, res): res.text = "OK" app.mount("/sub", sub) r = client.get("/sub/home") assert r.status_code == 200 assert r.text == "OK"
def test_mount(app: App, client, path): other = App() requested_path = None async def view(req, res): nonlocal requested_path requested_path = req.url.path other.route("/")(view) other.route("/foo")(view) app.mount("/other", other) r = client.get(path) assert r.status_code == 200 assert requested_path is not None assert requested_path.rstrip("/") == path
from bocadillo import App, Templates, static app = App() templates = Templates(app) # Create a socket.io async server. # NOTE: use the asgi driver, as described in: # https://python-socketio.readthedocs.io/en/latest/server.html#uvicorn-daphne-and-other-asgi-servers sio = socketio.AsyncServer(async_mode="asgi") # Create an ASGI-compliant app out of the socket.io server, # and mount it under the root app. # NOTE: "socket.io" is the default for `socketio_path`. We only add it # here for the sake of being explicit. # As a result, the client can connect at `/sio/socket.io`. app.mount("/sio", socketio.ASGIApp(sio, socketio_path="socket.io")) # Server static files for the socket.io client. # See: https://github.com/socketio/socket.io-client # NOTE: alternatively, the socket.io client could be served from # a CDN if you don't have npm/Node.js available in your runtime. # If so, static files would be linked to in the HTML page, and we wouldn't # need this line. # See: https://socket.io/docs/#Javascript-Client app.mount("/socket.io", static("node_modules/socket.io-client/dist")) @app.route("/") async def index(req, res): res.html = await templates.render("index.html")
"""Application definition.""" from bocadillo import App, discover_providers, Templates, static app = App() discover_providers("chatbot.providerconf") templates = Templates(app, directory='dist') app.mount(prefix='js', app=static('dist/js')) app.mount(prefix='css', app=static('dist/css')) # Create routes here. @app.route('/') async def index(req, res): res.html = await templates.render("index.html") @app.websocket_route("/conversation") async def converse(ws, diego, save_client): async for message in ws: response = diego.get_response(message) await ws.send(str(response)) @app.route("/client-count") async def client_count(req, res, clients): res.json = {"count": len(clients)}