Skip to content
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

Gracefully fail when freshness cannot be calculated #104

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Fix issue where freshness cannot be calculated to re-send request. (#104)

## 0.0.17 (6/11/2023)

- Fix `Last-Modified` validation.
Expand Down
6 changes: 4 additions & 2 deletions hishel/_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,10 @@ def construct_response_from_cache(
freshness_lifetime = get_heuristic_freshness(
response=response, clock=self._clock
)
else: # pragma: no cover
raise RuntimeError("The lifespan of freshness cannot be calculated.")
else:
# If Freshness cannot be calculated, then send the request
self._make_request_conditional(request=request, response=response)
return request

age = get_age(response, self._clock)
is_fresh = freshness_lifetime > age
Expand Down
13 changes: 13 additions & 0 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,16 @@ def test_must_understand_response_directive():
controller = Controller()

assert controller.is_cachable(request=request, response=response)

karpetrosyan marked this conversation as resolved.
Show resolved Hide resolved

def test_freshness_lifetime_invalid_information():
karpetrosyan marked this conversation as resolved.
Show resolved Hide resolved
controller = Controller()
response = Response(
status=400,
)
original_request = Request("GET", "https://example.com")
request = Request("GET", "https://example.com")
conditional_request = controller.construct_response_from_cache(
request=request, response=response, original_request=original_request
)
assert isinstance(conditional_request, Request)
karpetrosyan marked this conversation as resolved.
Show resolved Hide resolved
Loading