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

Class TestETag

tests/test_etag.py:16–105  ·  view source on GitHub ↗

Test our equal priority caching with ETags Equal Priority Caching is a term I've defined to describe when ETags are cached orthgonally from Time Based Caching.

Source from the content-addressed store, hash-verified

14
15
16class TestETag:
17 """Test our equal priority caching with ETags
18
19 Equal Priority Caching is a term I've defined to describe when
20 ETags are cached orthgonally from Time Based Caching.
21 """
22
23 @pytest.fixture()
24 def sess(self, url):
25 self.etag_url = urljoin(url, "/etag")
26 self.update_etag_url = urljoin(url, "/update_etag")
27 self.cache = DictCache()
28 sess = CacheControl(
29 requests.Session(), cache=self.cache, serializer=NullSerializer()
30 )
31 yield sess
32
33 # closing session object
34 sess.close()
35
36 def test_etags_get_example(self, sess, server):
37 """RFC 2616 14.26
38
39 The If-None-Match request-header field is used with a method to make
40 it conditional. A client that has one or more entities previously
41 obtained from the resource can verify that none of those entities
42 is current by including a list of their associated entity tags in
43 the If-None-Match header field. The purpose of this feature is to
44 allow efficient updates of cached information with a minimum amount
45 of transaction overhead
46
47 If any of the entity tags match the entity tag of the entity that
48 would have been returned in the response to a similar GET request
49 (without the If-None-Match header) on that resource, [...] then
50 the server MUST NOT perform the requested method, [...]. Instead, if
51 the request method was GET or HEAD, the server SHOULD respond with
52 a 304 (Not Modified) response, including the cache-related header
53 fields (particularly ETag) of one of the entities that matched.
54
55 (Paraphrased) A server may provide an ETag header on a response. On
56 subsequent queries, the client may reference the value of this Etag
57 header in an If-None-Match header; on receiving such a header, the
58 server can check whether the entity at that URL has changed from the
59 clients last version, and if not, it can return a 304 to indicate
60 the client can use it's current representation.
61 """
62 r = sess.get(self.etag_url)
63
64 # make sure we cached it
65 assert self.cache.get(self.etag_url) == r.raw
66
67 # make the same request
68 resp = sess.get(self.etag_url)
69 assert resp.raw == r.raw
70 assert resp.from_cache
71
72 # tell the server to change the etags of the response
73 sess.get(self.update_etag_url)

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…