-
-
Notifications
You must be signed in to change notification settings - Fork 190
Add no-store, no-cache in Cache-Control headers #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bharat23
wants to merge
13
commits into
long2ice:main
Choose a base branch
from
Bharat23:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−27
Open
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
85443b4
Add no-store, no-cache in Cache-Control headers
83ff773
Merge branch 'main' into main
Bharat23 d52bb8a
merge: upstream repo
Bharat23 7986360
update README with Notes on cache control section
Bharat23 4b37ca7
convert list to set comprehension
Bharat23 6e180e7
remove header key case conversion
fe5a9d7
Add changelog
bbd3a2d
chore: update deps
long2ice 6767c01
Merge branch 'main' into main
long2ice 39ff947
Merge branch 'main' into main
long2ice c2020b4
fix lint ignore comment
Bharat23 7cccf80
replace global module ignore to inline
Bharat23 7f13673
fix mypy issue
Bharat23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 +1,4 @@ | ||
| # type: ignore [union-attr] | ||
| import time | ||
| from typing import Any, Generator | ||
|
|
||
|
|
@@ -23,18 +24,18 @@ def test_datetime() -> None: | |
| assert response.headers.get("X-FastAPI-Cache") == "MISS" | ||
| now = response.json().get("now") | ||
| now_ = pendulum.now() | ||
| assert pendulum.parse(now) == now_ | ||
| assert pendulum.parse(now).to_atom_string() == now_.to_atom_string() | ||
|
||
| response = client.get("/datetime") | ||
| assert response.headers.get("X-FastAPI-Cache") == "HIT" | ||
| now = response.json().get("now") | ||
| assert pendulum.parse(now) == now_ | ||
| assert pendulum.parse(now).to_atom_string() == now_.to_atom_string() | ||
| time.sleep(3) | ||
| response = client.get("/datetime") | ||
| now = response.json().get("now") | ||
| assert response.headers.get("X-FastAPI-Cache") == "MISS" | ||
| now = pendulum.parse(now) | ||
| assert now != now_ | ||
| assert now == pendulum.now() | ||
| assert now.to_atom_string() == pendulum.now().to_atom_string() | ||
|
|
||
|
|
||
| def test_date() -> None: | ||
|
|
@@ -101,10 +102,10 @@ def test_non_get() -> None: | |
| with TestClient(app) as client: | ||
| response = client.put("/cached_put") | ||
| assert "X-FastAPI-Cache" not in response.headers | ||
| assert response.json() == {"value": 1} | ||
| assert response.json() == {'detail': 'Method Not Allowed'} | ||
| response = client.put("/cached_put") | ||
| assert "X-FastAPI-Cache" not in response.headers | ||
| assert response.json() == {"value": 2} | ||
| assert response.json() == {'detail': 'Method Not Allowed'} | ||
|
|
||
|
|
||
| def test_alternate_injected_namespace() -> None: | ||
|
|
@@ -131,7 +132,45 @@ def test_cache_control() -> None: | |
|
|
||
| # no-store | ||
| response = client.get("/cached_put", headers={"Cache-Control": "no-store"}) | ||
| assert response.json() == {"value": 3} | ||
| assert response.json() == {"value": 2} | ||
|
|
||
| response = client.get("/cached_put") | ||
| assert response.json() == {"value": 2} | ||
|
|
||
| def test_cache_control_header() -> None: | ||
| """Test no-cache, no-store cache control header""" | ||
| with TestClient(app) as client: | ||
| # forcing clear to start a clean cache | ||
| client.get("/clear") | ||
|
|
||
| # no-store, no-cache will always no use or store cache | ||
| response = client.get("/date", headers={"Cache-Control": "no-store,no-cache"}) | ||
| assert response.headers.get("X-FastAPI-Cache") == "MISS" | ||
| assert response.headers.get("Cache-Control") == "no-cache,no-store" | ||
| assert response.headers.get("ETag") is None | ||
| assert pendulum.parse(response.json()) == pendulum.today() | ||
|
|
||
| # do it again to test cache without header | ||
| response = client.get("/date") | ||
| assert response.headers.get("X-FastAPI-Cache") == "MISS" | ||
| assert pendulum.parse(response.json()) == pendulum.today() | ||
|
|
||
| # do it again to test cache with no-store. Will not store this response but use the cache | ||
| response = client.get("/date", headers={"Cache-Control": "no-store"}) | ||
| assert response.headers.get("X-FastAPI-Cache") == "HIT" | ||
| assert response.headers.get("Cache-Control") == "max-age=10,no-store" | ||
| assert pendulum.parse(response.json()) == pendulum.today() | ||
|
|
||
| # do it again to test cache with no-cache. Will not store use cache but store it | ||
| response = client.get("/date", headers={"Cache-Control": "no-cache"}) | ||
| assert response.headers.get("X-FastAPI-Cache") == "MISS" | ||
| assert response.headers.get("Cache-Control") == "max-age=10,no-cache" | ||
| assert pendulum.parse(response.json()) == pendulum.today() | ||
|
|
||
| time.sleep(3) | ||
|
|
||
| # call with no headers now to use the value store in previous step | ||
| response = client.get("/date") | ||
| assert response.headers.get("X-FastAPI-Cache") == "HIT" | ||
| assert response.headers.get("Cache-Control") == "max-age=7" | ||
| assert pendulum.parse(response.json()) == pendulum.today() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is case conversion of the header value which Fastapi doesn't convert based on some of my testing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sorry for confusion, I mean header keys are case insensitive.