-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support http cache headers, add fastapi tests (#9)
* Add http headers support, hashing for etag * Add clear function to storage, add tests
- Loading branch information
Showing
8 changed files
with
255 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,123 @@ | ||
import asyncio | ||
from fastapi.testclient import TestClient | ||
from .utils import app | ||
import pytest | ||
|
||
client = TestClient(app) | ||
from ultra_cache.decorator import _default_hash_fn | ||
from . import utils | ||
|
||
|
||
client = TestClient(utils.app) | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="function") | ||
def reset_cache(): | ||
try: | ||
loop = asyncio.get_event_loop() | ||
except: # noqa: E722 | ||
loop = asyncio.new_event_loop() | ||
loop.run_until_complete(utils.storage.clear()) | ||
yield | ||
loop.close() | ||
|
||
|
||
# TODO: Reset cache between tests | ||
def test_cache_decorator(): | ||
response = client.get("/items/1") | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 1} | ||
assert response.headers.get("X-Cache") == "MISS" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
etag_1 = response.headers.get("ETag") | ||
assert etag_1 is not None | ||
|
||
# Test cache hit | ||
response = client.get("/items/1") | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 1} | ||
assert response.headers.get("X-Cache") == "HIT" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
etag_2 = response.headers.get("ETag") | ||
assert etag_2 is not None | ||
assert etag_1 == etag_2 | ||
|
||
# Test cache miss | ||
response = client.get("/items/2") | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 2} | ||
assert response.headers.get("X-Cache") == "MISS" | ||
assert response.headers.get("ETag") != etag_1 | ||
assert response.headers.get("Cache-Control", "") == "" | ||
|
||
|
||
def test_cache_with_maxage(): | ||
response = client.get("/items/1", headers={"Cache-Control": "max-age=10"}) | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 1} | ||
assert response.headers.get("X-Cache") == "MISS" | ||
assert response.headers.get("Cache-Control", "") == "max-age=10" | ||
|
||
|
||
def test_cache_with_if_none_match_hit(): | ||
response = client.get( | ||
"/items/1", | ||
) | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 1} | ||
assert response.headers.get("X-Cache") == "MISS" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
|
||
etag = response.headers.get("ETag") | ||
assert etag is not None | ||
assert etag == _default_hash_fn(response.json()) | ||
|
||
# run again with If-None-Match | ||
response = client.get("/items/1", headers={"If-None-Match": etag}) | ||
|
||
assert response.status_code == 304 | ||
assert response.headers.get("X-Cache") == "HIT" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
assert response.headers.get("ETag") == etag | ||
|
||
|
||
def test_cache_with_if_none_match_hit_star(): | ||
response = client.get( | ||
"/items/1", | ||
) | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 1} | ||
assert response.headers.get("X-Cache") == "MISS" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
|
||
etag = response.headers.get("ETag") | ||
assert etag is not None | ||
assert etag == _default_hash_fn(response.json()) | ||
|
||
# run again with If-None-Match | ||
response = client.get("/items/1", headers={"If-None-Match": "*"}) | ||
|
||
assert response.status_code == 304 | ||
assert response.headers.get("X-Cache") == "HIT" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
assert response.headers.get("ETag") == etag | ||
|
||
|
||
def test_cache_with_if_none_match_miss(): | ||
response = client.get( | ||
"/items/1", | ||
) | ||
assert response.status_code == 200 | ||
assert response.json() == {"item_id": 1} | ||
assert response.headers.get("X-Cache") == "MISS" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
|
||
etag = response.headers.get("ETag") | ||
assert etag is not None | ||
assert etag == _default_hash_fn(response.json()) | ||
|
||
# run again with If-None-Match | ||
response = client.get("/items/1", headers={"If-None-Match": "W/123"}) | ||
|
||
assert response.status_code == 200 | ||
assert response.headers.get("X-Cache") == "HIT" | ||
assert response.headers.get("Cache-Control", "") == "" | ||
assert response.headers.get("ETag") == etag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from typing import Self | ||
|
||
|
||
class CacheControl: | ||
REQUEST_ONLY_KEYS = ["max-stale", "min-fresh", "only-if-cached"] | ||
|
||
def __init__(self, parts: dict[str, str]) -> None: | ||
self.parts: dict[str, str | None] = parts | ||
|
||
def set(self, key: str, value: str) -> None: | ||
self.parts[key] = value | ||
|
||
def get(self, key: str) -> str | None: | ||
return self.parts.get(key, None) | ||
|
||
def setdefault(self, key: str, value: str) -> None: | ||
self.parts.setdefault(key, value) | ||
|
||
@classmethod | ||
def from_string(cls, cache_control: str | None) -> Self: | ||
if cache_control is None: | ||
return cls({}) | ||
return cls( | ||
{ | ||
x.split("=")[0].strip(): x.split("=")[1].strip() if "=" in x else None | ||
for x in cache_control.lower().split(",") | ||
} | ||
) | ||
|
||
@property | ||
def max_age(self) -> int | None: | ||
value = self.parts.get("max-age", None) | ||
if value is None: | ||
return None | ||
return int(value) | ||
|
||
@property | ||
def no_cache(self) -> bool: | ||
return "no-cache" in self.parts | ||
|
||
@property | ||
def no_store(self) -> bool: | ||
return "no-store" in self.parts | ||
|
||
def to_response_header(self) -> str: | ||
return ", ".join( | ||
[ | ||
f"{k}={v}" | ||
for k, v in self.parts.items() | ||
if k not in self.REQUEST_ONLY_KEYS | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters