-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runner.py
95 lines (65 loc) · 2.65 KB
/
test_runner.py
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
import pytest
from htag import Tag
from htagweb import Runner
import sys,json
from starlette.testclient import TestClient
from starlette.responses import PlainTextResponse
import bs4
def test_runner_ws_mode():
def do_tests(client):
response = client.get('/')
# assert that get bootstrap page
assert response.status_code == 200
assert "<!DOCTYPE html>" in response.text
id=bs4.BeautifulSoup(response.text,"html.parser").select("button")[-1].get("id")
datas={"id":int(id),"method":"__on__","args":["onclick-"+str(id)],"kargs":{},"event":{}}
with client.websocket_connect('/_/examples.simple.App') as websocket:
#following exchanges will be json <-> json
websocket.send_text( json.dumps(datas) )
dico = json.loads(websocket.receive_text())
assert "update" in dico
app=Runner("examples.simple.App")
with TestClient(app) as client:
do_tests(client)
def test_runner_http_mode():
def do_tests(client):
response = client.get('/')
assert response.status_code == 200
assert "<!DOCTYPE html>" in response.text
id=bs4.BeautifulSoup(response.text,"html.parser").select("button")[-1].get("id")
datas={"id":int(id),"method":"__on__","args":["onclick-"+str(id)],"kargs":{},"event":{}}
response = client.post('/_/examples.simple.App',content=json.dumps(datas))
dico = json.loads(response.text)
assert "update" in dico
app=Runner("examples.simple.App",http_only=True)
with TestClient(app) as client:
do_tests(client)
def test_runner_route_static():
def do_tests(client):
# assert nothing is at "/"
response = client.get('/')
assert response.status_code == 404
# but something is at "/hello"
response = client.get('/hello')
assert response.status_code == 200
assert response.text == "world"
async def serve(req): return PlainTextResponse("world")
app=Runner()
app.add_route("/hello", serve)
with TestClient(app) as client:
do_tests(client)
def test_runner_route_app():
def do_tests(client):
# assert nothing is at "/"
response = client.get('/')
assert response.status_code == 404
# but something is at "/hello"
response = client.get('/an_app')
assert response.status_code == 200
assert "<!DOCTYPE html>" in response.text
async def serve_an_app(req):
return await req.app.handle(req,"examples.simple.App")
app=Runner()
app.add_route("/an_app", serve_an_app)
with TestClient(app) as client:
do_tests(client)