-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathtest_https.py
59 lines (44 loc) · 1.96 KB
/
test_https.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
import json
import pathlib
import ssl
import httpx
import pytest
import websockets
@pytest.mark.asyncio
@pytest.mark.parametrize('server_tls', ['asgi', 'rsgi'], indirect=True)
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_http_scope(server_tls, runtime_mode):
async with server_tls(runtime_mode) as port:
res = httpx.get(f'https://localhost:{port}/info?test=true', verify=False)
assert res.status_code == 200
data = res.json()
assert data['scheme'] == 'https'
@pytest.mark.asyncio
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_asgi_ws_scope(asgi_server, runtime_mode):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
localhost_pem = pathlib.Path.cwd() / 'tests' / 'fixtures' / 'tls' / 'cert.pem'
ssl_context.load_verify_locations(localhost_pem)
async with asgi_server(runtime_mode, tls=True) as port:
async with websockets.connect(f'wss://localhost:{port}/ws_info?test=true', ssl=ssl_context) as ws:
res = await ws.recv()
data = json.loads(res)
assert data['scheme'] == 'wss'
@pytest.mark.asyncio
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_rsgi_ws_scope(rsgi_server, runtime_mode):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
localhost_pem = pathlib.Path.cwd() / 'tests' / 'fixtures' / 'tls' / 'cert.pem'
ssl_context.load_verify_locations(localhost_pem)
async with rsgi_server(runtime_mode, tls=True) as port:
async with websockets.connect(f'wss://localhost:{port}/ws_info?test=true', ssl=ssl_context) as ws:
res = await ws.recv()
data = json.loads(res)
assert data['scheme'] == 'https'
@pytest.mark.asyncio
async def test_tls_encrypted_key(rsgi_server):
async with rsgi_server('st', tls='priv') as port:
res = httpx.get(f'https://localhost:{port}/info?test=true', verify=False)
assert res.status_code == 200
data = res.json()
assert data['scheme'] == 'https'