capybara-simulated 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b82726ef99cc26d3aaba51a010103e2712615464a13d5513400877cbbbe686e
4
- data.tar.gz: ce5d823da6b55a169d76db983a18919893172fb7b88e1c96b71d2f9b6ee85bc7
3
+ metadata.gz: b61d25a747a9298497ccf9c66f47f62f5e76c7ee79410aa7f1823a57c536e301
4
+ data.tar.gz: 296588737e8879ff5a64fa95034a625d40d80d9971405f962c9cbbb6a8cb30c5
5
5
  SHA512:
6
- metadata.gz: cbdaecdbc0b555e43842c7569f8b3b144efd1c0fe1e2bd0f5f48c577056cd1d356154c4d15a8910b36b5a88f9d069f1dfc7bf98961fbf2431b81e6b64350b3c0
7
- data.tar.gz: 8ea39123d1b9c5d13b5e8289e06c21d369a6d886a0e87ba9da07fa6071f71db10aa2a208dfde6ec713a34e7df4b76d3cbe0d5c15fd3cc05eb3ee9503499072d6
6
+ metadata.gz: 3adaf35e860f19084cf71c91092617151919f8ae71926f757afefdd3636d2f399a51c08ad703ed7c6210aeb9ab7f00a1c3f30cbf558f9ed2928b0d1a068a77d9
7
+ data.tar.gz: e89bca3cb36f3d48adb87ab880d31c00a8d0787c59f0b40c1cbaf1ebadf36a3eef3ebfc17bd1b3641011cf397e1fc16b13593d78cd83910a00ec7c834a686ae8
@@ -66,7 +66,16 @@ module Capybara
66
66
  end
67
67
 
68
68
  # RFC 9111 §3: status codes cacheable by default.
69
- CACHEABLE_STATUSES = [200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501].freeze
69
+ # Statuses we store. 301/308 (permanent redirects) are cacheable by default; 302/307
70
+ # (temporary) are cacheable too but only ever served fresh with EXPLICIT freshness — they
71
+ # are excluded from HEURISTIC_STATUSES below, and a 302/307 with neither explicit freshness
72
+ # nor a validator is dropped by `store`. Storing them lets a cached redirect be followed
73
+ # from the cache (request-cache only-if-cached/force-cache).
74
+ CACHEABLE_STATUSES = [200, 203, 204, 206, 300, 301, 302, 307, 308, 404, 405, 410, 414, 501].freeze
75
+ # Statuses eligible for HEURISTIC freshness (RFC 9111 §4.2.2 — "cacheable by default").
76
+ # 302/307 are NOT here: a temporary redirect is served fresh only with explicit freshness,
77
+ # never a Last-Modified-derived heuristic (which would risk serving it stale).
78
+ HEURISTIC_STATUSES = [200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501].freeze
70
79
 
71
80
  # `Vary` fields we can safely ignore because Simulated never sends
72
81
  # the corresponding request header — the cached "no value" variant
@@ -103,7 +112,7 @@ module Capybara
103
112
  # `private` is a per-user response a shared cache MUST NOT store
104
113
  # (§5.2.2.7); this process-wide cache is shared across sessions.
105
114
  return if cc[:private]
106
- max_age = freshness_seconds(cc, h)
115
+ max_age = freshness_seconds(cc, h, heuristic: HEURISTIC_STATUSES.include?(status))
107
116
  # Nothing useful to cache without a freshness signal or a
108
117
  # validator to revalidate against.
109
118
  return if max_age.nil? && h['etag'].nil? && h['last-modified'].nil?
@@ -132,7 +141,7 @@ module Capybara
132
141
  h = ensure_lowercase(new_headers)
133
142
  cc = parse_cache_control(h['cache-control'])
134
143
  entry.stored_at = Time.now
135
- entry.max_age = freshness_seconds(cc, h) || entry.max_age
144
+ entry.max_age = freshness_seconds(cc, h, heuristic: HEURISTIC_STATUSES.include?(entry.status)) || entry.max_age
136
145
  # RFC 9111 §4.3.4: a 304 UPDATES stored header fields with the ones
137
146
  # it carries; it does not delete them. A bare 304 (no Cache-Control —
138
147
  # the common ETag / Last-Modified revalidation) must therefore PRESERVE
@@ -145,8 +154,9 @@ module Capybara
145
154
 
146
155
  private
147
156
 
148
- def freshness_seconds(cc, headers)
149
- cc[:max_age] || expires_to_max_age(headers['expires'], headers['date']) || heuristic_freshness(headers)
157
+ def freshness_seconds(cc, headers, heuristic: true)
158
+ explicit = cc[:max_age] || expires_to_max_age(headers['expires'], headers['date'])
159
+ explicit || (heuristic ? heuristic_freshness(headers) : nil)
150
160
  end
151
161
 
152
162
  # RFC 9111 §4.2.2: a response with no explicit freshness (no max-age, no