Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
[FastAPI Single File Demo] Example fastapi single file testable example #fastapi
import logging
from fastapi import FastAPI
from starlette.responses import RedirectResponse
from starlette.testclient import TestClient
logger = logging.getLogger(__name__)
app = FastAPI()
def read_main():
return {"message": "Hello World from main app"}
subapi = FastAPI(openapi_prefix="/subapi")
async def read_sub():
return {"message": "Hello World from sub API"}
async def redirect():
url = app.url_path_for("redirected")
response = RedirectResponse(url=url)
return response
async def redirected():
return {"message": "you've been redirected"}
app.mount("/subapi", subapi)
client = TestClient(app)
def test_redirect_subapi():
url = app.url_path_for("redirect")
response = client.get(url)
assert response.json() == {"message": "you've been redirected"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.