-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathtest_ws.py
71 lines (58 loc) · 2.5 KB
/
test_ws.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
import json
import os
import pytest
import websockets
@pytest.mark.asyncio
@pytest.mark.parametrize('server', ['asgi', 'rsgi'], indirect=True)
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_messages(server, runtime_mode):
async with server(runtime_mode) as port:
async with websockets.connect(f'ws://localhost:{port}/ws_echo') as ws:
await ws.send('foo')
res_text = await ws.recv()
await ws.send(b'foo')
res_bytes = await ws.recv()
assert res_text == 'foo'
assert res_bytes == b'foo'
@pytest.mark.asyncio
@pytest.mark.parametrize('server', ['asgi', 'rsgi'], indirect=True)
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_reject(server, runtime_mode):
async with server(runtime_mode) as port:
with pytest.raises(websockets.exceptions.InvalidStatus) as exc:
async with websockets.connect(f'ws://localhost:{port}/ws_reject'):
pass
assert exc.value.response.status_code == 403
@pytest.mark.asyncio
@pytest.mark.skipif(bool(os.getenv('PGO_RUN')), reason='PGO build')
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_asgi_scope(asgi_server, runtime_mode):
async with asgi_server(runtime_mode) as port:
async with websockets.connect(f'ws://localhost:{port}/ws_info?test=true') as ws:
res = await ws.recv()
data = json.loads(res)
assert data['asgi'] == {'version': '3.0', 'spec_version': '2.3'}
assert data['type'] == 'websocket'
assert data['http_version'] == '1.1'
assert data['scheme'] == 'ws'
assert data['path'] == '/ws_info'
assert data['query_string'] == 'test=true'
assert data['headers']['host'] == f'localhost:{port}'
assert not data['subprotocols']
@pytest.mark.asyncio
@pytest.mark.skipif(bool(os.getenv('PGO_RUN')), reason='PGO build')
@pytest.mark.parametrize('runtime_mode', ['mt', 'st'])
async def test_rsgi_scope(rsgi_server, runtime_mode):
async with rsgi_server(runtime_mode) as port:
async with websockets.connect(f'ws://localhost:{port}/ws_info?test=true') as ws:
res = await ws.recv()
data = json.loads(res)
assert data['proto'] == 'ws'
assert data['http_version'] == '1.1'
assert data['rsgi_version'] == '1.4'
assert data['scheme'] == 'http'
assert data['method'] == 'GET'
assert data['path'] == '/ws_info'
assert data['query_string'] == 'test=true'
assert data['headers']['host'] == f'localhost:{port}'
assert not data['authority']