Spaces:
Sleeping
Sleeping
File size: 4,687 Bytes
57ea0fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | """HTML and executable assets must come from one immutable UI release."""
import hashlib
import re
from fastapi.testclient import TestClient
from service.api import create_app
from test_service import FakeEngine
def make_static(tmp_path):
static = tmp_path / 'static'
static.mkdir()
(static / 'index.html').write_text('<!doctype html><html><head><link rel="stylesheet" href="/static/styles.css?v=old"></head>'
'<body><script src="/static/app.js" defer></script></body></html>')
(static / 'styles.css').write_text('body{color:green}')
(static / 'app.js').write_text('window.uiRevision="first";')
return static
def asset_url(html, filename):
return re.search(r'(/assets/[0-9a-f]{64}/' + re.escape(filename) + r')', html).group(1)
def test_html_is_not_cached_and_references_fingerprinted_assets(tmp_path):
static = make_static(tmp_path)
app = create_app(data_root=tmp_path / 'uploads', static_root=static, engine=FakeEngine())
with TestClient(app) as client:
page = client.get('/')
assert page.status_code == 200
assert page.headers['cache-control'] == 'no-store, max-age=0'
assert page.headers['pragma'] == 'no-cache' and page.headers['expires'] == '0'
assert page.headers['x-ui-release'] in page.text
assert 'src="/static/app.js"' not in page.text
assert 'href="/static/styles.css' not in page.text
for filename in ('app.js', 'styles.css'):
response = client.get(asset_url(page.text, filename))
original = (static / filename).read_bytes()
assert response.content == original
assert response.headers['cache-control'] == 'public, max-age=31536000, immutable'
assert hashlib.sha256(original).hexdigest() in response.headers['etag']
head = client.head('/')
assert head.status_code == 200 and head.content == b''
assert head.headers['content-length'] == page.headers['content-length']
def test_release_snapshot_cannot_mix_old_html_with_new_javascript(tmp_path):
static = make_static(tmp_path)
first = create_app(data_root=tmp_path / 'first', static_root=static, engine=FakeEngine())
with TestClient(first) as old_client:
old_page = old_client.get('/')
old_url = asset_url(old_page.text, 'app.js')
old_content = old_client.get(old_url).content
(static / 'app.js').write_text('window.uiRevision="second";window.newPanel=true;')
(static / 'index.html').write_text((static / 'index.html').read_text().replace('<body>', '<body><div id="new-panel"></div>'))
assert old_client.get('/').text == old_page.text
assert old_client.get(old_url).content == old_content
second = create_app(data_root=tmp_path / 'second', static_root=static, engine=FakeEngine())
with TestClient(second) as new_client:
new_page = new_client.get('/')
new_url = asset_url(new_page.text, 'app.js')
assert new_url != old_url
assert new_page.headers['x-ui-release'] != old_page.headers['x-ui-release']
assert 'id="new-panel"' in new_page.text
assert new_client.get(new_url).content == (static / 'app.js').read_bytes()
mismatch = new_client.get(old_url)
assert mismatch.status_code == 404 and 'no-store' in mismatch.headers['cache-control']
assert 'immutable' not in mismatch.headers['cache-control']
def test_legacy_assets_revalidate_and_only_exact_version_can_be_immutable(tmp_path):
static = make_static(tmp_path)
app = create_app(data_root=tmp_path / 'uploads', static_root=static, engine=FakeEngine())
with TestClient(app) as client:
legacy = client.get('/static/app.js')
assert legacy.headers['cache-control'] == 'no-cache, must-revalidate'
assert 'etag' in legacy.headers and 'last-modified' in legacy.headers
assert client.get('/static/app.js', headers={'If-None-Match': legacy.headers['etag']}).status_code == 304
versioned_url = asset_url(client.get('/').text, 'app.js')
versioned = client.get(versioned_url)
not_modified = client.get(versioned_url, headers={'If-None-Match': versioned.headers['etag']})
assert not_modified.status_code == 304 and not_modified.content == b''
head = client.head(versioned_url)
assert head.status_code == 200 and head.content == b''
assert head.headers['content-length'] == str(len(versioned.content))
assert client.get('/assets/' + '0' * 64 + '/app.js').status_code == 404
assert client.get('/api/status').headers['cache-control'] == 'no-store'
|