forked from aiortc/aioquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_h0.py
190 lines (158 loc) · 6.92 KB
/
test_h0.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from unittest import TestCase
from aioquic.h0.connection import H0_ALPN, H0Connection
from aioquic.h3.events import DataReceived, HeadersReceived
from aioquic.quic.events import StreamDataReceived
from .test_connection import client_and_server, transfer
def h0_client_and_server():
return client_and_server(
client_options={"alpn_protocols": H0_ALPN},
server_options={"alpn_protocols": H0_ALPN},
)
def h0_transfer(quic_sender, h0_receiver):
quic_receiver = h0_receiver._quic
transfer(quic_sender, quic_receiver)
# process QUIC events
http_events = []
event = quic_receiver.next_event()
while event is not None:
http_events.extend(h0_receiver.handle_event(event))
event = quic_receiver.next_event()
return http_events
class H0ConnectionTest(TestCase):
def test_connect(self):
with h0_client_and_server() as (quic_client, quic_server):
h0_client = H0Connection(quic_client)
h0_server = H0Connection(quic_server)
# send request
stream_id = quic_client.get_next_available_stream_id()
h0_client.send_headers(
stream_id=stream_id,
headers=[
(b":method", b"GET"),
(b":scheme", b"https"),
(b":authority", b"localhost"),
(b":path", b"/"),
],
)
h0_client.send_data(stream_id=stream_id, data=b"", end_stream=True)
# receive request
events = h0_transfer(quic_client, h0_server)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(
events[0].headers, [(b":method", b"GET"), (b":path", b"/")]
)
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)
# send response
h0_server.send_headers(
stream_id=stream_id,
headers=[
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
],
)
h0_server.send_data(
stream_id=stream_id,
data=b"<html><body>hello</body></html>",
end_stream=True,
)
# receive response
events = h0_transfer(quic_server, h0_client)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(events[0].headers, [])
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"<html><body>hello</body></html>")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)
def test_headers_only(self):
with h0_client_and_server() as (quic_client, quic_server):
h0_client = H0Connection(quic_client)
h0_server = H0Connection(quic_server)
# send request
stream_id = quic_client.get_next_available_stream_id()
h0_client.send_headers(
stream_id=stream_id,
headers=[
(b":method", b"HEAD"),
(b":scheme", b"https"),
(b":authority", b"localhost"),
(b":path", b"/"),
],
end_stream=True,
)
# receive request
events = h0_transfer(quic_client, h0_server)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(
events[0].headers, [(b":method", b"HEAD"), (b":path", b"/")]
)
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)
# send response
h0_server.send_headers(
stream_id=stream_id,
headers=[
(b":status", b"200"),
(b"content-type", b"text/html; charset=utf-8"),
],
end_stream=True,
)
# receive response
events = h0_transfer(quic_server, h0_client)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(events[0].headers, [])
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)
def test_fragmented_request(self):
with h0_client_and_server() as (quic_client, quic_server):
h0_server = H0Connection(quic_server)
stream_id = 0
# receive first fragment of the request
events = h0_server.handle_event(
StreamDataReceived(
data=b"GET /012", end_stream=False, stream_id=stream_id
)
)
self.assertEqual(len(events), 0)
# receive second fragment of the request
events = h0_server.handle_event(
StreamDataReceived(
data=b"34567890", end_stream=False, stream_id=stream_id
)
)
# receive final fragment of the request
events = h0_server.handle_event(
StreamDataReceived(
data=b"123456\r\n", end_stream=True, stream_id=stream_id
)
)
self.assertEqual(len(events), 2)
self.assertTrue(isinstance(events[0], HeadersReceived))
self.assertEqual(
events[0].headers,
[(b":method", b"GET"), (b":path", b"/01234567890123456")],
)
self.assertEqual(events[0].stream_id, stream_id)
self.assertEqual(events[0].stream_ended, False)
self.assertTrue(isinstance(events[1], DataReceived))
self.assertEqual(events[1].data, b"")
self.assertEqual(events[1].stream_id, stream_id)
self.assertEqual(events[1].stream_ended, True)