Datasets:
Download code/evaluation-code/v0.2-0c2690ae/apps/console/tests/test_gzip.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/apps/console/tests/test_gzip.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/apps/console/tests/test_gzip.py
-
curl -L -o test_gzip.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/apps/console/tests/test_gzip.py
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() | |