File size: 2,077 Bytes
3b3b2bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Real HTTP check for the stateless synthetic Space, not a browser-render test."""
from pathlib import Path
import json,os,socket,subprocess,sys,time,urllib.request,urllib.error
ROOT=Path(__file__).resolve().parents[1]
def main():
    with socket.socket() as s:s.bind(('127.0.0.1',0));port=s.getsockname()[1]
    proc=subprocess.Popen([sys.executable,'-m','uvicorn','app:app','--host','127.0.0.1','--port',str(port)],
        cwd=ROOT/'space',env=dict(os.environ,PYTHONPATH=str(ROOT)),stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
    base=f'http://127.0.0.1:{port}'
    try:
        for _ in range(100):
            if proc.poll() is not None:raise RuntimeError('demo server exited')
            try:
                with urllib.request.urlopen(base+'/health',timeout=1) as r:health=json.load(r)
                break
            except OSError:time.sleep(.05)
        else:raise RuntimeError('demo startup timeout')
        with urllib.request.urlopen(base+'/',timeout=5) as r:
            html=r.read().decode();assert 'PHILLNET / RECALL' in html
        rows=[]
        for name in ('glossary','intersection','proof-budget','correction'):
            with urllib.request.urlopen(base+'/demo/'+name,timeout=10) as r:
                result=json.load(r);result.pop('evidence',None);rows.append(result)
        with urllib.request.urlopen(base+'/hero.png',timeout=5) as r:assert r.read(8)==b'\x89PNG\r\n\x1a\n'
        assert rows[0]['target_retrieved'] and rows[1]['answers']==[{'?service':'Beacon'}]
        assert rows[2]['after_bytes']<rows[2]['before_bytes'] and not rows[3]['old_certificate_valid_now']
    finally:proc.terminate();proc.wait(10)
    report={'transport':'actual localhost HTTP','health':health,'four_scenarios_executed':True,'hero_served':True,
            'private_memory_writes_exposed':False,'server_left_running':False,'browser_rendered':False,
            'docker_built':False,'results':rows}
    (ROOT/'reports/space_http.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))
if __name__=='__main__':main()