Skip to content

Commit b6ff77f

Browse files
authored
bug: 304 response to request with if-none-metch header field must contain cache-control, expires, etag header fields (#44)
Fixes #39
1 parent 0235bda commit b6ff77f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/fastapi_redis_cache/cache.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,24 @@ async def inner_wrapper(*args, **kwargs):
4646
key = redis_cache.get_cache_key(func, *args, **kwargs)
4747
ttl, in_cache = redis_cache.check_cache(key)
4848
if in_cache:
49+
redis_cache.set_response_headers(response, True, deserialize_json(in_cache), ttl)
4950
if redis_cache.requested_resource_not_modified(request, in_cache):
5051
response.status_code = int(HTTPStatus.NOT_MODIFIED)
51-
return response
52-
cached_data = deserialize_json(in_cache)
53-
redis_cache.set_response_headers(response, cache_hit=True, response_data=cached_data, ttl=ttl)
54-
if create_response_directly:
55-
return Response(content=in_cache, media_type="application/json", headers=response.headers)
56-
return cached_data
52+
return (
53+
Response(
54+
content=None,
55+
status_code=response.status_code,
56+
media_type="application/json",
57+
headers=response.headers,
58+
)
59+
if create_response_directly
60+
else response
61+
)
62+
return (
63+
Response(content=in_cache, media_type="application/json", headers=response.headers)
64+
if create_response_directly
65+
else deserialize_json(in_cache)
66+
)
5767
response_data = await get_api_response_async(func, *args, **kwargs)
5868
ttl = calculate_ttl(expire)
5969
cached = redis_cache.add_to_cache(key, response_data, ttl)

tests/test_cache.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,18 @@ def test_if_none_match():
113113
invalid_etag = "W/-5480454928453453778"
114114
response = client.get("/cache_never_expire", headers={"if-none-match": f"{etag}, {invalid_etag}"})
115115
assert response.status_code == 304
116+
assert not response.content
117+
assert "x-fastapi-cache" in response.headers and response.headers["x-fastapi-cache"] == "Hit"
118+
assert "cache-control" in response.headers
119+
assert "expires" in response.headers
120+
assert "etag" in response.headers
116121
response = client.get("/cache_never_expire", headers={"if-none-match": "*"})
117122
assert response.status_code == 304
123+
assert not response.content
124+
assert "x-fastapi-cache" in response.headers and response.headers["x-fastapi-cache"] == "Hit"
125+
assert "cache-control" in response.headers
126+
assert "expires" in response.headers
127+
assert "etag" in response.headers
118128
response = client.get("/cache_never_expire", headers={"if-none-match": invalid_etag})
119129
assert response.status_code == 200
120130
assert response.json() == {"success": True, "message": "this data can be cached indefinitely"}

0 commit comments

Comments
 (0)