VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 4)
9d57416 verified
Raw History Blame Contribute Delete
1.78 kB
"""Text responses over 16 KB are gzipped when the client accepts it: JSON and the built web assets."""
import gzip
from pathlib import Path
import sys
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[3]; sys.path.insert(0, str(REPO))
from apps.console.server.http_app import Application
from apps.console.server.registry import Registry
class GzipTests(unittest.TestCase):
def test_assets_are_gzipped_like_json(self):
with tempfile.TemporaryDirectory() as tmp:
dist = Path(tmp) / 'dist'; (dist / 'assets').mkdir(parents=True)
body = {'assets/app.js': ('const x = 1;\n' * 3000).encode(), 'assets/app.css': ('a{color:red}\n' * 3000).encode(),
'assets/tiny.js': b'export {};\n', 'assets/pic.png': bytes(range(256)) * 100}
for name, data in body.items():
(dist / name).write_bytes(data)
(dist / 'index.html').write_text('<!doctype html>')
app = Application(Registry([]), dist)
try:
for name, data in body.items():
r = app.dispatch('GET', '/' + name, {'accept-encoding': 'gzip, br'})
self.assertEqual(r.status, 200)
zipped = r.headers.get('Content-Encoding') == 'gzip'
self.assertEqual(zipped, name.endswith(('.js', '.css')) and len(data) > 16_000, name)
self.assertEqual(gzip.decompress(r.body) if zipped else r.body, data)
plain = app.dispatch('GET', '/' + name) # 不声明 gzip 的照原样
self.assertNotIn('Content-Encoding', plain.headers); self.assertEqual(plain.body, data)
finally:
app.close()
if __name__ == '__main__':
unittest.main()