mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-04 12:10:48 -05:00
* ✨ Add support for declaring a Response parameter to set headers and cookies * ✅ Add source for docs and tests * 📝 Add docs for setting headers, cookies and status code * 📝 Add attribution to Hug for inspiring response parameters
28 lines
607 B
Python
28 lines
607 B
Python
from fastapi import Depends, FastAPI
|
|
from starlette.responses import Response
|
|
from starlette.testclient import TestClient
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
async def response_status_setter(response: Response):
|
|
response.status_code = 201
|
|
|
|
|
|
async def parent_dep(result=Depends(response_status_setter)):
|
|
return result
|
|
|
|
|
|
@app.get("/", dependencies=[Depends(parent_dep)])
|
|
async def get_main():
|
|
return {"msg": "Hello World"}
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_dependency_set_status_code():
|
|
response = client.get("/")
|
|
assert response.status_code == 201
|
|
assert response.json() == {"msg": "Hello World"}
|