feat: cache response obj add test case

This commit is contained in:
vvanglro
2022-11-04 17:34:20 +08:00
parent 4cb4afeff0
commit 2710129c4e
3 changed files with 25 additions and 2 deletions

View File

@@ -2,7 +2,7 @@ import pendulum
import uvicorn import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import Response from starlette.responses import Response, JSONResponse
from fastapi_cache import FastAPICache from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend from fastapi_cache.backends.inmemory import InMemoryBackend
@@ -42,6 +42,7 @@ async def get_date():
async def get_datetime(request: Request, response: Response): async def get_datetime(request: Request, response: Response):
return {"now": pendulum.now()} return {"now": pendulum.now()}
@app.get("/sync-me") @app.get("/sync-me")
@cache(namespace="test") @cache(namespace="test")
def sync_me(): def sync_me():
@@ -50,6 +51,12 @@ def sync_me():
return 42 return 42
@app.get("/cache_response_obj")
@cache(namespace="test", expire=5)
async def cache_response_obj():
return JSONResponse({"a": 1})
@app.on_event("startup") @app.on_event("startup")
async def startup(): async def startup():
FastAPICache.init(InMemoryBackend()) FastAPICache.init(InMemoryBackend())

View File

@@ -6,7 +6,7 @@ import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from redis.asyncio.connection import ConnectionPool from redis.asyncio.connection import ConnectionPool
from starlette.requests import Request from starlette.requests import Request
from starlette.responses import Response from starlette.responses import Response, JSONResponse
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
@@ -73,6 +73,12 @@ async def cache_html(request: Request):
}) })
@app.get("/cache_response_obj")
@cache(namespace="test", expire=5)
async def cache_response_obj():
return JSONResponse({"a": 1})
@app.on_event("startup") @app.on_event("startup")
async def startup(): async def startup():
pool = ConnectionPool.from_url(url="redis://redis") pool = ConnectionPool.from_url(url="redis://redis")

View File

@@ -58,3 +58,13 @@ def test_sync() -> None:
with TestClient(app) as client: with TestClient(app) as client:
response = client.get("/sync-me") response = client.get("/sync-me")
assert response.json() == 42 assert response.json() == 42
def test_cache_response_obj() -> None:
with TestClient(app) as client:
cache_response = client.get("cache_response_obj")
assert cache_response.json() == {"a": 1}
get_cache_response = client.get("cache_response_obj")
assert get_cache_response.json() == {"a": 1}
assert get_cache_response.headers.get("cache-control")
assert get_cache_response.headers.get("etag")