-
-
Save francbartoli/0e6ae1640d29bcb6d64deea1ba9709d7 to your computer and use it in GitHub Desktop.
[FastAPI Single File Demo] Example fastapi single file testable example #fastapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from fastapi import FastAPI | |
from starlette.responses import RedirectResponse | |
from starlette.testclient import TestClient | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) | |
app = FastAPI() | |
@app.get("/app") | |
def read_main(): | |
return {"message": "Hello World from main app"} | |
subapi = FastAPI(openapi_prefix="/subapi") | |
@subapi.get("/sub") | |
async def read_sub(): | |
return {"message": "Hello World from sub API"} | |
@subapi.get("/redirect") | |
async def redirect(): | |
url = app.url_path_for("redirected") | |
response = RedirectResponse(url=url) | |
return response | |
@subapi.get("/redirected") | |
async def redirected(): | |
logger.debug("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