pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
792 B
34 lines
792 B
from fastapi import Depends, FastAPI, Response
|
|
from fastapi.testclient import TestClient
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def set_cookie(*, response: Response):
|
|
response.set_cookie("cookie-name", "cookie-value")
|
|
return {}
|
|
|
|
|
|
def set_indirect_cookie(*, dep: str = Depends(set_cookie)):
|
|
return dep
|
|
|
|
|
|
@app.get("/directCookie")
|
|
def get_direct_cookie(dep: str = Depends(set_cookie)):
|
|
return {"dep": dep}
|
|
|
|
|
|
@app.get("/indirectCookie")
|
|
def get_indirect_cookie(dep: str = Depends(set_indirect_cookie)):
|
|
return {"dep": dep}
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_cookie_is_set_once():
|
|
direct_response = client.get("/directCookie")
|
|
indirect_response = client.get("/indirectCookie")
|
|
assert (
|
|
direct_response.headers["set-cookie"] == indirect_response.headers["set-cookie"]
|
|
)
|
|
|