Repository navigation
Expand file tree
/
Copy pathtest_local_serve.py
More file actions
105 lines (87 loc) · 3.77 KB
/
Copy pathtest_local_serve.py
File metadata and controls
105 lines (87 loc) · 3.77 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
"""
Self-check for local_serve.py's gzip transfer encoding. No framework, just asserts:
python3 test_local_serve.py
The dashboard is read over an SSH tunnel, where the uncompressed page (19.6 MB
at 45k records) is the entire reason a refresh takes seconds. SimpleHTTPRequest-
Handler never compresses, so local_serve adds it — served from a cache keyed on
the file's mtime+size so a 0.37 s compression isn't repeated per viewer.
Checks: a gzip-capable client gets a correctly-encoded, smaller response whose
decompressed bytes are byte-identical to the file; a client that does NOT accept
gzip still gets the plain page; and the cache re-compresses after a rebuild
instead of serving the previous page forever.
"""
import gzip
import http.client
import os
import sys
import threading
import http.server
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, BASE_DIR)
import local_serve as ls
PORT = 8899
failures = 0
def check(name, cond, detail=''):
global failures
if cond:
print(f' ok {name}{detail}')
else:
print(f' FAIL {name}{detail}')
failures += 1
def get(accept_encoding):
"""GET / and return (status, headers, raw body bytes)."""
conn = http.client.HTTPConnection('127.0.0.1', PORT, timeout=60)
headers = {'Accept-Encoding': accept_encoding} if accept_encoding else {}
conn.request('GET', '/', headers=headers)
r = conn.getresponse()
body = r.read()
conn.close()
return r.status, dict(r.getheaders()), body
def main():
if not os.path.exists(ls.LOCAL_HTML):
print('index_local.html missing — building it first')
ls.rebuild()
disk = open(ls.LOCAL_HTML, 'rb').read()
httpd = http.server.ThreadingHTTPServer((ls.BIND_ADDR, PORT), ls.LocalHandler)
threading.Thread(target=httpd.serve_forever, daemon=True).start()
os.chdir(BASE_DIR) # LocalHandler serves the cwd
try:
status, hdrs, body = get('gzip, deflate')
check('gzip request → 200', status == 200)
check('Content-Encoding: gzip', hdrs.get('Content-Encoding') == 'gzip')
check('Content-Length matches body',
int(hdrs.get('Content-Length', -1)) == len(body))
# http.client does NOT auto-decompress, so body is the raw gzip stream.
check('decompresses to exactly the file on disk',
gzip.decompress(body) == disk)
ratio = len(disk) / max(len(body), 1)
check('smaller than the raw page', ratio > 2,
f' ({len(disk):,} → {len(body):,} bytes, {ratio:.1f}x)')
status, hdrs, plain = get(None)
check('non-gzip client → 200', status == 200)
check('non-gzip client gets no Content-Encoding',
'Content-Encoding' not in hdrs)
check('non-gzip client gets the whole page', plain == disk)
# Cache invalidation: a rebuild must not keep serving the old bytes.
# Touching the file the way generate_dashboard_html does (write + replace)
# changes mtime and size, which is what the cache is keyed on.
before = get('gzip')[2]
marker = b'<!--cache-invalidation-probe-->'
tmp = ls.LOCAL_HTML + '.probe'
with open(tmp, 'wb') as f:
f.write(disk + marker)
os.replace(tmp, ls.LOCAL_HTML)
after = get('gzip')[2]
try:
check('rebuild invalidates the gzip cache',
after != before and gzip.decompress(after).endswith(marker))
finally:
with open(ls.LOCAL_HTML, 'wb') as f: # restore the real page
f.write(disk)
finally:
httpd.shutdown()
print('\nall checks passed' if failures == 0 else f'\n{failures} CHECK(S) FAILED')
return 1 if failures else 0
if __name__ == '__main__':
sys.exit(main())