mirror of
https://github.com/fastapi/fastapi.git
synced 2026-09-14 13:36:21 +08:00
✨ Add support for Response parameters to set headers, cookies, and status codes (#294)
* ✨ 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
This commit is contained in:
committed by
GitHub
parent
c8eea09664
commit
5f7fe926ab
@@ -0,0 +1,27 @@
|
||||
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"}
|
||||
Reference in New Issue
Block a user