Skip to content

Instantly share code, notes, and snippets.

@melvinkcx
Created February 15, 2021 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melvinkcx/d208972b452641b8c1bd504f237b7d3a to your computer and use it in GitHub Desktop.
Save melvinkcx/d208972b452641b8c1bd504f237b7d3a to your computer and use it in GitHub Desktop.
Configurable FastAPI TestClient To Allow Us To Set Cookies Once And For All
from starlette.testclient import TestClient
class CookieConfigurableTestClient(TestClient):
_access_token = None
def set_access_token(self, token):
self._access_token = token
def reset(self):
self._access_token = None
def request(self, *args, **kwargs):
cookies = kwargs.get("cookies")
if cookies is None and self._access_token:
kwargs["cookies"] = {"access_token": self._access_token}
return super().request(*args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment