Files
fastapi/tests/test_repeated_cookie_headers.py
obataku 7ce756f9dd 🐛 Fix duplicated headers set by indirect dependencies (#1386)
* Added test for repeating cookies in response headers

* update `response` headers, status code to match `sub_response` in `solve_dependencies` only if necessary; fix formatting of scottsmith2gmail's test

* restore code coverage, remove dead code from `solve_dependencies`

Co-authored-by: Scott Smith <scott.smith.2@gmail.com>
2020-06-13 14:44:51 +02:00

35 lines
792 B
Python

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"]
)