MCPcopy Create free account
hub / github.com/psf/cachecontrol / TestSessionActions

Class TestSessionActions

tests/test_adapter.py:41–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39
40
41class TestSessionActions:
42 def test_get_caches(self, url, sess):
43 r2 = sess.get(url)
44 assert r2.from_cache is True
45
46 def test_get_with_no_cache_does_not_cache(self, url, sess):
47 r2 = sess.get(url, headers={"Cache-Control": "no-cache"})
48 assert not r2.from_cache
49
50 def test_put_invalidates_cache(self, url, sess):
51 r2 = sess.put(url, data={"foo": "bar"})
52 sess.get(url)
53 assert not r2.from_cache
54
55 def test_patch_invalidates_cache(self, url, sess):
56 r2 = sess.patch(url, data={"foo": "bar"})
57 sess.get(url)
58 assert not r2.from_cache
59
60 def test_delete_invalidates_cache(self, url, sess):
61 r2 = sess.delete(url)
62 sess.get(url)
63 assert not r2.from_cache
64
65 def test_close(self):
66 cache = mock.Mock(spec=DictCache)
67 sess = Session()
68 sess.mount("http://", CacheControlAdapter(cache))
69
70 sess.close()
71 assert cache.close.called
72
73 def test_do_not_leak_response(self, url, sess):
74 resp = sess.get(url + "stream", stream=True)
75 resp.raise_for_status()
76 r1_weak = weakref.ref(resp.raw)
77
78 # This is a mis-use of requests, becase we should either consume
79 # the body, or call .close().
80 # But requests without cachecontrol handle this anyway, because
81 # urllib3.response.HTTPResponse has a __del__ finalizer on it that closes it
82 # once there are no more references to it.
83 # We should not break this.
84
85 resp = None
86 if platform.python_implementation() == "PyPy":
87 # NOTE: Need to explicitly tell PyPy to collect at this point.
88 # See: https://github.com/psf/cachecontrol/issues/351
89 # See: https://doc.pypy.org/en/latest/cpython_differences.html#differences-related-to-garbage-collection-strategies
90 gc.collect()
91
92 # Below this point, it should be closed because there are no more references
93 # to the session response.
94
95 r1 = r1_weak()
96 assert r1 is None or r1.closed

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…