askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_html_cache_headers.py · 2.3 KB · 58 lines
| 1 | """Object-served HTML is no-store, so "did you deploy?" has one answer. |
| 2 | |
| 3 | These pages are rendered per request and their inline script IS the |
| 4 | application, yet they shipped with no cache headers at all -- leaving the |
| 5 | browser's heuristic cache in charge of whether a deploy arrived. Safari's |
| 6 | heuristics bit for real: a /talk fix was deployed and verified live with |
| 7 | curl while an iPad kept reproducing the old bug, because the iPad was |
| 8 | running the old page and there was no way to know. |
| 9 | |
| 10 | Deliberately HTML-only: the generative widgets (/style, /list, /form) are |
| 11 | separate objects with their own content types, and turning off caching |
| 12 | for every asset on every view is a heavier performance decision than a |
| 13 | stale-page bug justifies. |
| 14 | """ |
| 15 | |
| 16 | import object_server |
| 17 | |
| 18 | |
| 19 | def normalize(payload): |
| 20 | return object_server._normalize_object_response(payload) |
| 21 | |
| 22 | |
| 23 | def header(headers, name): |
| 24 | return [v for k, v in headers if k.lower() == name] |
| 25 | |
| 26 | |
| 27 | def test_an_html_page_object_is_no_store(): |
| 28 | _, headers, _ = normalize({"content_type": "text/html; charset=utf-8", |
| 29 | "body": "<h1>page</h1>"}) |
| 30 | assert header(headers, "cache-control") == ["no-store"] |
| 31 | |
| 32 | |
| 33 | def test_a_bare_string_page_is_no_store(): |
| 34 | _, headers, _ = normalize("bare html") |
| 35 | assert header(headers, "cache-control") == ["no-store"] |
| 36 | |
| 37 | |
| 38 | def test_json_and_other_content_types_are_untouched(): |
| 39 | for payload in ({"content_type": "application/json", "body": "{}"}, |
| 40 | {"content_type": "text/css", "body": "body{}"}, |
| 41 | {"content_type": "application/javascript", "body": ";"}, |
| 42 | {"ok": True}): |
| 43 | _, headers, _ = normalize(payload) |
| 44 | assert header(headers, "cache-control") == [], payload |
| 45 | |
| 46 | |
| 47 | def test_the_tuple_form_keeps_full_header_control(): |
| 48 | """An object that writes its own headers is respected -- including |
| 49 | choosing to cache.""" |
| 50 | _, headers, _ = normalize((200, [("content-type", "text/html"), |
| 51 | ("cache-control", "max-age=60")], b"x")) |
| 52 | assert header(headers, "cache-control") == ["max-age=60"] |
| 53 | |
| 54 | |
| 55 | def test_content_type_parameters_do_not_dodge_the_rule(): |
| 56 | _, headers, _ = normalize({"content_type": "TEXT/HTML; charset=utf-8", |
| 57 | "body": "x"}) |
| 58 | assert header(headers, "cache-control") == ["no-store"] |