Run an API without opening a socket
An ASGI application can receive real HTTP-shaped requests without binding a network port. HTTPX's ASGITransport calls the application in memory. This makes examples deterministic and removes a running server, an internet connection and a port choice from the test setup.
FastAPI parses path parameters before calling the endpoint. An invalid integer therefore produces a validation response instead of reaching your business logic. Use an async endpoint for these examples so they also run in a browser runtime that cannot start background threads.
import asyncio, httpx
from fastapi import FastAPI
app = FastAPI()
@app.get("/double/{value}")
async def double(value: int):
return {"value": value * 2}
async def demo():
transport = httpx.ASGITransport(app=app)
async with httpx.AsyncClient(
transport=transport,
base_url="http://test") as client:
print((await client.get(
"/double/3")).json())
asyncio.run(demo())Your task
Implement build_app() returning a FastAPI app with GET /square/{value}, integer value, returning {"square": value*value}. Invalid integers must produce HTTP 422.
The explanation opens here once every check passes.