MCPcopy Index your code
hub / github.com/idank/explainshell / TestExplainCacheHeaders

Class TestExplainCacheHeaders

tests/test_web_views.py:339–430  ·  view source on GitHub ↗

/explain responses carry ETag + Cache-Control so Cloudflare and browsers can cache them. The ETag is `` - `` — both deploy-wide constants — so it flips on any DB rebuild or code change and otherwise stays stable.

Source from the content-addressed store, hash-verified

337
338
339class TestExplainCacheHeaders(unittest.TestCase):
340 """/explain responses carry ETag + Cache-Control so Cloudflare and
341 browsers can cache them. The ETag is ``<db_sha[:16]>-<app_ver>`` —
342 both deploy-wide constants — so it flips on any DB rebuild or code
343 change and otherwise stays stable."""
344
345 _RAW = RawManpage(
346 source_text=".TH BAR 1",
347 generated_at=datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc),
348 generator="test",
349 )
350
351 def _make_mp(self) -> ParsedManpage:
352 return ParsedManpage(
353 source="ubuntu/26.04/1/bar.1.gz",
354 name="bar",
355 synopsis="bar synopsis",
356 options=[Option(text="-a desc", short=["-a"], long=[])],
357 aliases=[("bar", 10)],
358 )
359
360 def setUp(self):
361 self.app = create_app()
362 self.app.config["DEBUG"] = False
363 self.app.config["APP_VERSION"] = "deadbeef"
364 self.app.config["DB_SHA256"] = "abcdef0123456789fedcba9876543210"
365 self.store = Store.create(":memory:")
366 self.store.add_manpage(self._make_mp(), self._RAW)
367 _use_store(self.app, self.store)
368 self.app.config["TESTING"] = True
369 self.client = self.app.test_client()
370
371 _EXPECTED_ETAG = 'W/"abcdef0123456789-deadbeef"'
372 _EXPECTED_CC = (
373 "public, max-age=604800, s-maxage=604800, stale-while-revalidate=86400"
374 )
375
376 def test_manpage_view_sets_etag_and_cache_control(self):
377 rv = self.client.get("/explain/bar")
378 self.assertEqual(rv.status_code, 200)
379 self.assertEqual(rv.headers.get("ETag"), self._EXPECTED_ETAG)
380 self.assertEqual(rv.headers.get("Cache-Control"), self._EXPECTED_CC)
381 # Explicit Vary so CF can't pull a cookie header into the cache key.
382 self.assertEqual(rv.headers.get("Vary"), "Accept-Encoding")
383
384 def test_cmd_view_sets_etag_and_cache_control(self):
385 rv = self.client.get("/explain?cmd=bar+-a")
386 self.assertEqual(rv.status_code, 200)
387 self.assertEqual(rv.headers.get("ETag"), self._EXPECTED_ETAG)
388 self.assertEqual(rv.headers.get("Cache-Control"), self._EXPECTED_CC)
389 self.assertEqual(rv.headers.get("Vary"), "Accept-Encoding")
390
391 def test_if_none_match_returns_304(self):
392 etag = self.client.get("/explain/bar").headers["ETag"]
393 rv = self.client.get("/explain/bar", headers={"If-None-Match": etag})
394 self.assertEqual(rv.status_code, 304)
395 self.assertIn("Cache-Control", rv.headers)
396

Callers

nothing calls this directly

Calls 1

RawManpageClass · 0.90

Tested by

no test coverage detected