Debian Codegraph @

testing/web-platform/tests/tools/third_party/h2/test/test_priority.py

raw
1 # -*- coding: utf-8 -*-
2 """
3 test_priority
4 ~~~~~~~~~~~~~
5
6 Test the priority logic of Hyper-h2.
7 """
8 import pytest
9
10 import h2.config
11 import h2.connection
12 import h2.errors
13 import h2.events
14 import h2.exceptions
15 import h2.stream
16
17
18 class TestPriority(object):
19 """
20 Basic priority tests.
21 """
22 example_request_headers = [
23 (':authority', 'example.com'),
24 (':path', '/'),
25 (':scheme', 'https'),
26 (':method', 'GET'),
27 ]
28 example_response_headers = [
29 (':status', '200'),
30 ('server', 'pytest-h2'),
31 ]
32 server_config = h2.config.H2Configuration(client_side=False)
33
34 def test_receiving_priority_emits_priority_update(self, frame_factory):
35 """
36 Receiving a priority frame emits a PriorityUpdated event.
37 """
38 c = h2.connection.H2Connection(config=self.server_config)
39 c.initiate_connection()
40 c.receive_data(frame_factory.preamble())
41 c.clear_outbound_data_buffer()
42
43 f = frame_factory.build_priority_frame(
44 stream_id=1,
45 weight=255,
46 )
47 events = c.receive_data(f.serialize())
48
49 assert len(events) == 1
50 assert not c.data_to_send()
51
52 event = events[0]
53 assert isinstance(event, h2.events.PriorityUpdated)
54 assert event.stream_id == 1
55 assert event.depends_on == 0
56 assert event.weight == 256
57 assert event.exclusive is False
58
59 def test_headers_with_priority_info(self, frame_factory):
60 """
61 Receiving a HEADERS frame with priority information on it emits a
62 PriorityUpdated event.
63 """
64 c = h2.connection.H2Connection(config=self.server_config)
65 c.initiate_connection()
66 c.receive_data(frame_factory.preamble())
67 c.clear_outbound_data_buffer()
68
69 f = frame_factory.build_headers_frame(
70 headers=self.example_request_headers,
71 stream_id=3,
72 flags=['PRIORITY'],
73 stream_weight=15,
74 depends_on=1,
75 exclusive=True,
76 )
77 events = c.receive_data(f.serialize())
78
79 assert len(events) == 2
80 assert not c.data_to_send()
81
82 event = events[1]
83 assert isinstance(event, h2.events.PriorityUpdated)
84 assert event.stream_id == 3
85 assert event.depends_on == 1
86 assert event.weight == 16
87 assert event.exclusive is True
88
89 def test_streams_may_not_depend_on_themselves(self, frame_factory):
90 """
91 A stream adjusted to depend on itself causes a Protocol Error.
92 """
93 c = h2.connection.H2Connection(config=self.server_config)
94 c.initiate_connection()
95 c.receive_data(frame_factory.preamble())
96 c.clear_outbound_data_buffer()
97
98 f = frame_factory.build_headers_frame(
99 headers=self.example_request_headers,
100 stream_id=3,
101 flags=['PRIORITY'],
102 stream_weight=15,
103 depends_on=1,
104 exclusive=True,
105 )
106 c.receive_data(f.serialize())
107 c.clear_outbound_data_buffer()
108
109 f = frame_factory.build_priority_frame(
110 stream_id=3,
111 depends_on=3,
112 weight=15
113 )
114 with pytest.raises(h2.exceptions.ProtocolError):
115 c.receive_data(f.serialize())
116
117 expected_frame = frame_factory.build_goaway_frame(
118 last_stream_id=3,
119 error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR,
120 )
121 assert c.data_to_send() == expected_frame.serialize()
122
123 @pytest.mark.parametrize(
124 'depends_on,weight,exclusive',
125 [
126 (0, 256, False),
127 (3, 128, False),
128 (3, 128, True),
129 ]
130 )
131 def test_can_prioritize_stream(self, depends_on, weight, exclusive,
132 frame_factory):
133 """
134 hyper-h2 can emit priority frames.
135 """
136 c = h2.connection.H2Connection()
137 c.initiate_connection()
138
139 c.send_headers(headers=self.example_request_headers, stream_id=1)
140 c.send_headers(headers=self.example_request_headers, stream_id=3)
141 c.clear_outbound_data_buffer()
142
143 c.prioritize(
144 stream_id=1,
145 depends_on=depends_on,
146 weight=weight,
147 exclusive=exclusive
148 )
149
150 f = frame_factory.build_priority_frame(
151 stream_id=1,
152 weight=weight - 1,
153 depends_on=depends_on,
154 exclusive=exclusive,
155 )
156 assert c.data_to_send() == f.serialize()
157
158 @pytest.mark.parametrize(
159 'depends_on,weight,exclusive',
160 [
161 (0, 256, False),
162 (1, 128, False),
163 (1, 128, True),
164 ]
165 )
166 def test_emit_headers_with_priority_info(self, depends_on, weight,
167 exclusive, frame_factory):
168 """
169 It is possible to send a headers frame with priority information on
170 it.
171 """
172 c = h2.connection.H2Connection()
173 c.initiate_connection()
174 c.clear_outbound_data_buffer()
175
176 c.send_headers(
177 headers=self.example_request_headers,
178 stream_id=3,
179 priority_weight=weight,
180 priority_depends_on=depends_on,
181 priority_exclusive=exclusive,
182 )
183
184 f = frame_factory.build_headers_frame(
185 headers=self.example_request_headers,
186 stream_id=3,
187 flags=['PRIORITY'],
188 stream_weight=weight - 1,
189 depends_on=depends_on,
190 exclusive=exclusive,
191 )
192 assert c.data_to_send() == f.serialize()
193
194 def test_may_not_prioritize_stream_to_depend_on_self(self, frame_factory):
195 """
196 A stream adjusted to depend on itself causes a Protocol Error.
197 """
198 c = h2.connection.H2Connection()
199 c.initiate_connection()
200 c.receive_data(frame_factory.preamble())
201 c.send_headers(
202 headers=self.example_request_headers,
203 stream_id=3,
204 priority_weight=255,
205 priority_depends_on=0,
206 priority_exclusive=False,
207 )
208 c.clear_outbound_data_buffer()
209
210 with pytest.raises(h2.exceptions.ProtocolError):
211 c.prioritize(
212 stream_id=3,
213 depends_on=3,
214 )
215
216 assert not c.data_to_send()
217
218 def test_may_not_initially_set_stream_depend_on_self(self, frame_factory):
219 """
220 A stream that starts by depending on itself causes a Protocol Error.
221 """
222 c = h2.connection.H2Connection()
223 c.initiate_connection()
224 c.receive_data(frame_factory.preamble())
225 c.clear_outbound_data_buffer()
226
227 with pytest.raises(h2.exceptions.ProtocolError):
228 c.send_headers(
229 headers=self.example_request_headers,
230 stream_id=3,
231 priority_depends_on=3,
232 )
233
234 assert not c.data_to_send()
235
236 @pytest.mark.parametrize('weight', [0, -15, 257])
237 def test_prioritize_requires_valid_weight(self, weight):
238 """
239 A call to prioritize with an invalid weight causes a ProtocolError.
240 """
241 c = h2.connection.H2Connection()
242 c.initiate_connection()
243 c.clear_outbound_data_buffer()
244
245 with pytest.raises(h2.exceptions.ProtocolError):
246 c.prioritize(stream_id=1, weight=weight)
247
248 assert not c.data_to_send()
249
250 @pytest.mark.parametrize('weight', [0, -15, 257])
251 def test_send_headers_requires_valid_weight(self, weight):
252 """
253 A call to send_headers with an invalid weight causes a ProtocolError.
254 """
255 c = h2.connection.H2Connection()
256 c.initiate_connection()
257 c.clear_outbound_data_buffer()
258
259 with pytest.raises(h2.exceptions.ProtocolError):
260 c.send_headers(
261 stream_id=1,
262 headers=self.example_request_headers,
263 priority_weight=weight
264 )
265
266 assert not c.data_to_send()
267
268 def test_prioritize_defaults(self, frame_factory):
269 """
270 When prioritize() is called with no explicit arguments, it emits a
271 weight of 16, depending on stream zero non-exclusively.
272 """
273 c = h2.connection.H2Connection()
274 c.initiate_connection()
275 c.clear_outbound_data_buffer()
276
277 c.prioritize(stream_id=1)
278
279 f = frame_factory.build_priority_frame(
280 stream_id=1,
281 weight=15,
282 depends_on=0,
283 exclusive=False,
284 )
285 assert c.data_to_send() == f.serialize()
286
287 @pytest.mark.parametrize(
288 'priority_kwargs',
289 [
290 {'priority_weight': 16},
291 {'priority_depends_on': 0},
292 {'priority_exclusive': False},
293 ]
294 )
295 def test_send_headers_defaults(self, priority_kwargs, frame_factory):
296 """
297 When send_headers() is called with only one explicit argument, it emits
298 default values for everything else.
299 """
300 c = h2.connection.H2Connection()
301 c.initiate_connection()
302 c.clear_outbound_data_buffer()
303
304 c.send_headers(
305 stream_id=1,
306 headers=self.example_request_headers,
307 **priority_kwargs
308 )
309
310 f = frame_factory.build_headers_frame(
311 headers=self.example_request_headers,
312 stream_id=1,
313 flags=['PRIORITY'],
314 stream_weight=15,
315 depends_on=0,
316 exclusive=False,
317 )
318 assert c.data_to_send() == f.serialize()
319
320 def test_servers_cannot_prioritize(self, frame_factory):
321 """
322 Server stacks are not allowed to call ``prioritize()``.
323 """
324 c = h2.connection.H2Connection(config=self.server_config)
325 c.initiate_connection()
326 c.receive_data(frame_factory.preamble())
327 c.clear_outbound_data_buffer()
328
329 f = frame_factory.build_headers_frame(
330 stream_id=1,
331 headers=self.example_request_headers,
332 )
333 c.receive_data(f.serialize())
334
335 with pytest.raises(h2.exceptions.RFC1122Error):
336 c.prioritize(stream_id=1)
337
338 def test_servers_cannot_prioritize_with_headers(self, frame_factory):
339 """
340 Server stacks are not allowed to prioritize on headers either.
341 """
342 c = h2.connection.H2Connection(config=self.server_config)
343 c.initiate_connection()
344 c.receive_data(frame_factory.preamble())
345 c.clear_outbound_data_buffer()
346
347 f = frame_factory.build_headers_frame(
348 stream_id=1,
349 headers=self.example_request_headers,
350 )
351 c.receive_data(f.serialize())
352
353 with pytest.raises(h2.exceptions.RFC1122Error):
354 c.send_headers(
355 stream_id=1,
356 headers=self.example_response_headers,
357 priority_weight=16,
358 )