Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,6 @@ def update_cached_response(
}
)

# we want a 200 b/c we have content via the cache
cached_response.status = 200

# update our cache
self._cache_set(cache_url, request, cached_response)

Expand Down
17 changes: 11 additions & 6 deletions tests/test_cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ def test_update_cached_response_no_local_cache(self):
result = cc.update_cached_response(req, resp)
assert result is resp

def test_update_cached_response_with_valid_headers_separate_body(self, tmp_path):
@pytest.mark.parametrize("status", [200, 203, 300, 301, 308])
def test_update_cached_response_with_valid_headers_separate_body(
self, tmp_path, status
):
"""
If the local cache has the given URL ``update_cached_response()`` will:

Expand All @@ -244,9 +247,10 @@ def test_update_cached_response_with_valid_headers_separate_body(self, tmp_path)
This is the version for a cache that stores a separate body.
"""
cache = SeparateBodyFileCache(os.fsdecode(tmp_path))
self.update_cached_response_with_valid_headers_test(cache)
self.update_cached_response_with_valid_headers_test(cache, status)

def test_update_cached_response_with_valid_headers(self):
@pytest.mark.parametrize("status", [200, 203, 300, 301, 308])
def test_update_cached_response_with_valid_headers(self, status):
"""
If the local cache has the given URL ``update_cached_response()`` will:

Expand All @@ -256,9 +260,9 @@ def test_update_cached_response_with_valid_headers(self):
This is the version for non-separate body.
"""
cache = DictCache({})
self.update_cached_response_with_valid_headers_test(cache)
self.update_cached_response_with_valid_headers_test(cache, status)

def update_cached_response_with_valid_headers_test(self, cache):
def update_cached_response_with_valid_headers_test(self, cache, status):
"""
If the local cache has the given URL ``update_cached_response()`` will:

Expand All @@ -273,7 +277,7 @@ def update_cached_response_with_valid_headers_test(self, cache):
url = "http://localhost:123/x"
req = DummyRequest(url=url, headers={})
cached_resp = DummyResponse(
status=200,
status=status,
headers={
"ETag": etag,
"x-value:": "a",
Expand Down Expand Up @@ -307,6 +311,7 @@ def update_cached_response_with_valid_headers_test(self, cache):
result2 = cc.cached_request(req)

for r in [result, result2]:
assert r.status == status
assert r.headers["ETag"] == etag
assert r.headers["x-value"] == "b"
assert r.headers["Content-Length"] == "100"
Expand Down
38 changes: 38 additions & 0 deletions tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
Test for supporting redirect caches as needed.
"""

from unittest.mock import patch

import pytest
import requests
from urllib3 import HTTPResponse

from cachecontrol import CacheControl
from cachecontrol.adapter import CacheControlAdapter


class TestPermanentRedirects:
Expand Down Expand Up @@ -53,3 +58,36 @@ def test_bust_cache_on_redirect(self, url):
)

assert not resp.from_cache


@pytest.mark.parametrize("status", [301, 308])
def test_revalidated_redirect_keeps_status(status):
session = CacheControl(requests.Session())
url = "https://example.test/old"
headers = {
"ETag": '"redirect"',
"Location": "/new",
"Date": "Tue, 03 Nov 2026 12:00:00 GMT",
"Cache-Control": "max-age=0",
}
adapter = session.get_adapter(url)
adapter.controller.cache_response(
requests.Request("GET", url).prepare(),
HTTPResponse(status=status, headers=headers, body=b""),
body=b"",
)

def revalidate(self, request, *args, **kwargs):
assert request.headers["If-None-Match"] == '"redirect"'
return CacheControlAdapter.build_response(
self, request, HTTPResponse(status=304, headers=headers, body=b"")
)

with patch("requests.adapters.HTTPAdapter.send", revalidate):
response = session.get(url, allow_redirects=False)

assert response.from_cache
assert response.status_code == status
assert response.is_redirect
assert response.headers["Location"] == "/new"
assert response.content == b""