From f9d9c67ab1e542dd36a491c2636e3ba23dcec335 Mon Sep 17 00:00:00 2001 From: Marcel Maatkamp Date: Wed, 19 Aug 2026 16:55:59 +0200 Subject: [PATCH] Invalidate cached responses after POST RFC 9111 4.4 requires invalidation of the target URI on a non-error response to any unsafe method, and names POST explicitly. invalidating_methods listed PUT, PATCH and DELETE only, so a successful POST left a cached GET for the same URL in place. The added test asserts on the GET that follows the POST rather than on the POST response itself, so it fails without the change. --- cachecontrol/adapter.py | 2 +- tests/test_adapter.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cachecontrol/adapter.py b/cachecontrol/adapter.py index 4f4c185a..3f9386c5 100644 --- a/cachecontrol/adapter.py +++ b/cachecontrol/adapter.py @@ -24,7 +24,7 @@ class CacheControlAdapter(HTTPAdapter): - invalidating_methods = {"PUT", "PATCH", "DELETE"} + invalidating_methods = {"POST", "PUT", "PATCH", "DELETE"} def __init__( self, diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 8a4f38e9..cb4e754d 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -62,6 +62,11 @@ def test_delete_invalidates_cache(self, url, sess): sess.get(url) assert not r2.from_cache + def test_post_invalidates_cache(self, url, sess): + sess.get(url) + sess.post(url, data={"foo": "bar"}) + assert not sess.get(url).from_cache + def test_close(self): cache = mock.Mock(spec=DictCache) sess = Session()