forked from sanic-org/sanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_http_alt_svc.py
64 lines (49 loc) · 1.35 KB
/
test_http_alt_svc.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
import sys
from pathlib import Path
import pytest
from sanic.app import Sanic
from sanic.response import empty
from tests.client import RawClient
parent_dir = Path(__file__).parent
localhost_dir = parent_dir / "certs/localhost"
@pytest.mark.skipif(sys.version_info < (3, 9), reason="Not supported in 3.7")
def test_http1_response_has_alt_svc(port):
Sanic._app_registry.clear()
app = Sanic("TestAltSvc")
app.config.TOUCHUP = True
response = b""
@app.get("/")
async def handler(*_):
return empty()
@app.after_server_start
async def do_request(*_):
nonlocal response
app.router.reset()
app.router.finalize()
client = RawClient(app.state.host, app.state.port)
await client.connect()
await client.send(
"""
GET / HTTP/1.1
host: localhost:7777
"""
)
response = await client.recv(1024)
await client.close()
@app.after_server_start
def shutdown(*_):
app.stop()
app.prepare(
version=3,
ssl={
"cert": localhost_dir / "fullchain.pem",
"key": localhost_dir / "privkey.pem",
},
port=port,
)
app.prepare(
version=1,
port=port,
)
Sanic.serve_single(app)
assert f'alt-svc: h3=":{port}"\r\n'.encode() in response