forked from python-hyper/wsproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_handshake.py
102 lines (85 loc) · 3.66 KB
/
test_handshake.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
96
97
98
99
100
101
102
import pytest
from wsproto.connection import CLIENT, ConnectionState, SERVER
from wsproto.events import AcceptConnection, Ping, Request
from wsproto.handshake import H11Handshake
from wsproto.utilities import LocalProtocolError, RemoteProtocolError
def test_successful_handshake() -> None:
client = H11Handshake(CLIENT)
server = H11Handshake(SERVER)
server.receive_data(client.send(Request(host="localhost", target="/")))
assert isinstance(next(server.events()), Request)
client.receive_data(server.send(AcceptConnection()))
assert isinstance(next(client.events()), AcceptConnection)
assert client.state is ConnectionState.OPEN
assert server.state is ConnectionState.OPEN
assert repr(client) == "H11Handshake(client=True, state=ConnectionState.OPEN)"
assert repr(server) == "H11Handshake(client=False, state=ConnectionState.OPEN)"
def test_host_encoding() -> None:
client = H11Handshake(CLIENT)
server = H11Handshake(SERVER)
data = client.send(Request(host="芝士汉堡", target="/"))
assert b"Host: xn--7ks3rz39bh7u" in data
server.receive_data(data)
request = next(server.events())
assert isinstance(request, Request)
assert request.host == "芝士汉堡"
@pytest.mark.parametrize("http", [b"HTTP/1.0", b"HTTP/1.1"])
def test_rejected_handshake(http: bytes) -> None:
server = H11Handshake(SERVER)
with pytest.raises(RemoteProtocolError):
server.receive_data(
b"GET / " + http + b"\r\n"
b"Upgrade: websocket\r\n"
b"Connection: Upgrade\r\n"
b"Sec-WebSocket-Key: VQr8cvwwZ1fEk62PDq8J3A==\r\n"
b"Sec-WebSocket-Version: 13\r\n"
b"\r\n"
)
def test_initiate_upgrade_as_client() -> None:
client = H11Handshake(CLIENT)
with pytest.raises(LocalProtocolError):
client.initiate_upgrade_connection([], "/")
def test_send_invalid_event() -> None:
client = H11Handshake(CLIENT)
with pytest.raises(LocalProtocolError):
client.send(Ping())
def test_h11_multiple_headers_handshake() -> None:
server = H11Handshake(SERVER)
data = (
b"GET wss://api.website.xyz/ws HTTP/1.1\r\n"
b"Host: api.website.xyz\r\n"
b"Connection: Upgrade\r\n"
b"Pragma: no-cache\r\n"
b"Cache-Control: no-cache\r\n"
b"User-Agent: Mozilla/5.0 (X11; Linux x86_64) "
b"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36\r\n"
b"Upgrade: websocket\r\n"
b"Origin: https://website.xyz\r\n"
b"Sec-WebSocket-Version: 13\r\n"
b"Accept-Encoding: gzip, deflate, br\r\n"
b"Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7\r\n"
b"Sec-WebSocket-Key: tOzeAzi9xK7ADxxEdTzmaA==\r\n"
b"Sec-WebSocket-Extensions: this-extension; isnt-seen, even-tho, it-should-be\r\n"
b"Sec-WebSocket-Protocol: there-protocols\r\n"
b"Sec-WebSocket-Protocol: arent-seen\r\n"
b"Sec-WebSocket-Extensions: this-extension; were-gonna-see, and-another-extension; were-also; gonna-see=100; percent\r\n" # noqa: E501
b"Sec-WebSocket-Protocol: only-these-protocols, are-seen, from-the-request-object\r\n"
b"\r\n"
)
server.receive_data(data)
request = next(server.events())
assert isinstance(request, Request)
assert request.subprotocols == [
"there-protocols",
"arent-seen",
"only-these-protocols",
"are-seen",
"from-the-request-object",
]
assert request.extensions == [
"this-extension; isnt-seen",
"even-tho",
"it-should-be",
"this-extension; were-gonna-see",
"and-another-extension; were-also; gonna-see=100; percent",
]