26
26
27
27
28
28
def decode_frame (frame_data ):
29
- f , length = Frame .parse_frame_header (frame_data [:8 ])
30
- f .parse_body (frame_data [8 : 8 + length ])
31
- assert 8 + length == len (frame_data )
29
+ f , length = Frame .parse_frame_header (frame_data [:9 ])
30
+ f .parse_body (memoryview ( frame_data [9 : 9 + length ]) )
31
+ assert 9 + length == len (frame_data )
32
32
return f
33
33
34
34
@@ -52,8 +52,8 @@ def test_base_frame_cant_parse_body(self):
52
52
53
53
54
54
class TestDataFrame (object ):
55
- payload = b'\x00 \x08 \x00 \x01 \x00 \x00 \x00 \x01 testdata'
56
- payload_with_padding = b'\x00 \x13 \x00 \x09 \x00 \x00 \x00 \x01 \x0A testdata' + b'\0 ' * 10
55
+ payload = b'\x00 \x00 \ x08\x00 \x01 \x00 \x00 \x00 \x01 testdata'
56
+ payload_with_padding = b'\x00 \x00 \ x13\x00 \x09 \x00 \x00 \x00 \x01 \x0A testdata' + b'\0 ' * 10
57
57
58
58
def test_data_frame_has_correct_flags (self ):
59
59
f = DataFrame (1 )
@@ -80,17 +80,15 @@ def test_data_frame_with_padding_serializes_properly(self):
80
80
assert s == self .payload_with_padding
81
81
82
82
def test_data_frame_parses_properly (self ):
83
- f , length = Frame .parse_frame_header (self .payload [:8 ])
84
- f .parse_body (memoryview (self .payload [8 :8 + length ]))
83
+ f = decode_frame (self .payload )
85
84
86
85
assert isinstance (f , DataFrame )
87
86
assert f .flags == set (['END_STREAM' ])
88
87
assert f .pad_length == 0
89
88
assert f .data == b'testdata'
90
89
91
90
def test_data_frame_with_padding_parses_properly (self ):
92
- f , length = Frame .parse_frame_header (self .payload_with_padding [:8 ])
93
- f .parse_body (memoryview (self .payload_with_padding [8 :8 + length ]))
91
+ f = decode_frame (self .payload_with_padding )
94
92
95
93
assert isinstance (f , DataFrame )
96
94
assert f .flags == set (['END_STREAM' , 'PADDED' ])
@@ -117,7 +115,7 @@ def test_data_frame_comes_on_a_stream(self):
117
115
118
116
119
117
class TestPriorityFrame (object ):
120
- payload = b'\x00 \x05 \x02 \x00 \x00 \x00 \x00 \x01 \x80 \x00 \x00 \x04 \x40 '
118
+ payload = b'\x00 \x00 \ x05\x02 \x00 \x00 \x00 \x00 \x01 \x80 \x00 \x00 \x04 \x40 '
121
119
122
120
def test_priority_frame_has_no_flags (self ):
123
121
f = PriorityFrame (1 )
@@ -134,8 +132,7 @@ def test_priority_frame_with_all_data_serializes_properly(self):
134
132
assert f .serialize () == self .payload
135
133
136
134
def test_priority_frame_with_all_data_parses_properly (self ):
137
- f , length = Frame .parse_frame_header (self .payload [:8 ])
138
- f .parse_body (memoryview (self .payload [8 :8 + length ]))
135
+ f = decode_frame (self .payload )
139
136
140
137
assert isinstance (f , PriorityFrame )
141
138
assert f .flags == set ()
@@ -160,12 +157,11 @@ def test_rst_stream_frame_serializes_properly(self):
160
157
f .error_code = 420
161
158
162
159
s = f .serialize ()
163
- assert s == b'\x00 \x04 \x03 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x01 \xa4 '
160
+ assert s == b'\x00 \x00 \ x04\x03 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x01 \xa4 '
164
161
165
162
def test_rst_stream_frame_parses_properly (self ):
166
- s = b'\x00 \x04 \x03 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x01 \xa4 '
167
- f , length = Frame .parse_frame_header (s [:8 ])
168
- f .parse_body (memoryview (s [8 :8 + length ]))
163
+ s = b'\x00 \x00 \x04 \x03 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x01 \xa4 '
164
+ f = decode_frame (s )
169
165
170
166
assert isinstance (f , RstStreamFrame )
171
167
assert f .flags == set ()
@@ -183,13 +179,13 @@ def test_rst_stream_frame_must_have_body_length_four(self):
183
179
184
180
class TestSettingsFrame (object ):
185
181
serialized = (
186
- b'\x00 \x24 \x04 \x01 \x00 \x00 \x00 \x00 ' + # Frame header
187
- b'\x00 \x01 \x00 \x00 \x10 \x00 ' + # HEADER_TABLE_SIZE
188
- b'\x00 \x02 \x00 \x00 \x00 \x00 ' + # ENABLE_PUSH
189
- b'\x00 \x03 \x00 \x00 \x00 \x64 ' + # MAX_CONCURRENT_STREAMS
190
- b'\x00 \x04 \x00 \x00 \xFF \xFF ' + # INITIAL_WINDOW_SIZE
191
- b'\x00 \x05 \x00 \x00 \x40 \x00 ' + # SETTINGS_MAX_FRAME_SIZE
192
- b'\x00 \x06 \x00 \x00 \xFF \xFF ' # SETTINGS_MAX_HEADER_LIST_SIZE
182
+ b'\x00 \x00 \ x24\x04 \x01 \x00 \x00 \x00 \x00 ' + # Frame header
183
+ b'\x00 \x01 \x00 \x00 \x10 \x00 ' + # HEADER_TABLE_SIZE
184
+ b'\x00 \x02 \x00 \x00 \x00 \x00 ' + # ENABLE_PUSH
185
+ b'\x00 \x03 \x00 \x00 \x00 \x64 ' + # MAX_CONCURRENT_STREAMS
186
+ b'\x00 \x04 \x00 \x00 \xFF \xFF ' + # INITIAL_WINDOW_SIZE
187
+ b'\x00 \x05 \x00 \x00 \x40 \x00 ' + # SETTINGS_MAX_FRAME_SIZE
188
+ b'\x00 \x06 \x00 \x00 \xFF \xFF ' # SETTINGS_MAX_HEADER_LIST_SIZE
193
189
)
194
190
195
191
settings = {
@@ -215,8 +211,7 @@ def test_settings_frame_serializes_properly(self):
215
211
assert s == self .serialized
216
212
217
213
def test_settings_frame_parses_properly (self ):
218
- f , length = Frame .parse_frame_header (self .serialized [:8 ])
219
- f .parse_body (memoryview (self .serialized [8 :8 + length ]))
214
+ f = decode_frame (self .serialized )
220
215
221
216
assert isinstance (f , SettingsFrame )
222
217
assert f .flags == set (['ACK' ])
@@ -242,19 +237,18 @@ def test_push_promise_frame_serializes_properly(self):
242
237
243
238
s = f .serialize ()
244
239
assert s == (
245
- b'\x00 \x0F \x05 \x04 \x00 \x00 \x00 \x01 ' +
240
+ b'\x00 \x00 \ x0F\x05 \x04 \x00 \x00 \x00 \x01 ' +
246
241
b'\x00 \x00 \x00 \x04 ' +
247
242
b'hello world'
248
243
)
249
244
250
245
def test_push_promise_frame_parses_properly (self ):
251
246
s = (
252
- b'\x00 \x0F \x05 \x04 \x00 \x00 \x00 \x01 ' +
247
+ b'\x00 \x00 \ x0F\x05 \x04 \x00 \x00 \x00 \x01 ' +
253
248
b'\x00 \x00 \x00 \x04 ' +
254
249
b'hello world'
255
250
)
256
- f , length = Frame .parse_frame_header (s [:8 ])
257
- f .parse_body (memoryview (s [8 :8 + length ]))
251
+ f = decode_frame (s )
258
252
259
253
assert isinstance (f , PushPromiseFrame )
260
254
assert f .flags == set (['END_HEADERS' ])
@@ -276,7 +270,7 @@ def test_ping_frame_serializes_properly(self):
276
270
277
271
s = f .serialize ()
278
272
assert s == (
279
- b'\x00 \x08 \x06 \x01 \x00 \x00 \x00 \x00 \x01 \x02 \x00 \x00 \x00 \x00 \x00 \x00 '
273
+ b'\x00 \x00 \ x08\x06 \x01 \x00 \x00 \x00 \x00 \x01 \x02 \x00 \x00 \x00 \x00 \x00 \x00 '
280
274
)
281
275
282
276
def test_no_more_than_8_octets (self ):
@@ -287,9 +281,8 @@ def test_no_more_than_8_octets(self):
287
281
f .serialize ()
288
282
289
283
def test_ping_frame_parses_properly (self ):
290
- s = b'\x00 \x08 \x06 \x01 \x00 \x00 \x00 \x00 \x01 \x02 \x00 \x00 \x00 \x00 \x00 \x00 '
291
- f , length = Frame .parse_frame_header (s [:8 ])
292
- f .parse_body (memoryview (s [8 :8 + length ]))
284
+ s = b'\x00 \x00 \x08 \x06 \x01 \x00 \x00 \x00 \x00 \x01 \x02 \x00 \x00 \x00 \x00 \x00 \x00 '
285
+ f = decode_frame (s )
293
286
294
287
assert isinstance (f , PingFrame )
295
288
assert f .flags == set (['ACK' ])
@@ -321,21 +314,20 @@ def test_goaway_serializes_properly(self):
321
314
322
315
s = f .serialize ()
323
316
assert s == (
324
- b'\x00 \x0D \x07 \x00 \x00 \x00 \x00 \x00 ' + # Frame header
325
- b'\x00 \x00 \x00 \x40 ' + # Last Stream ID
326
- b'\x00 \x00 \x00 \x20 ' + # Error Code
327
- b'hello' # Additional data
317
+ b'\x00 \x00 \ x0D\x07 \x00 \x00 \x00 \x00 \x00 ' + # Frame header
318
+ b'\x00 \x00 \x00 \x40 ' + # Last Stream ID
319
+ b'\x00 \x00 \x00 \x20 ' + # Error Code
320
+ b'hello' # Additional data
328
321
)
329
322
330
323
def test_goaway_frame_parses_properly (self ):
331
324
s = (
332
- b'\x00 \x0D \x07 \x00 \x00 \x00 \x00 \x00 ' + # Frame header
333
- b'\x00 \x00 \x00 \x40 ' + # Last Stream ID
334
- b'\x00 \x00 \x00 \x20 ' + # Error Code
335
- b'hello' # Additional data
325
+ b'\x00 \x00 \ x0D\x07 \x00 \x00 \x00 \x00 \x00 ' + # Frame header
326
+ b'\x00 \x00 \x00 \x40 ' + # Last Stream ID
327
+ b'\x00 \x00 \x00 \x20 ' + # Error Code
328
+ b'hello' # Additional data
336
329
)
337
- f , length = Frame .parse_frame_header (s [:8 ])
338
- f .parse_body (memoryview (s [8 :8 + length ]))
330
+ f = decode_frame (s )
339
331
340
332
assert isinstance (f , GoAwayFrame )
341
333
assert f .flags == set ()
@@ -359,12 +351,11 @@ def test_window_update_serializes_properly(self):
359
351
f .window_increment = 512
360
352
361
353
s = f .serialize ()
362
- assert s == b'\x00 \x04 \x08 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x02 \x00 '
354
+ assert s == b'\x00 \x00 \ x04\x08 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x02 \x00 '
363
355
364
356
def test_windowupdate_frame_parses_properly (self ):
365
- s = b'\x00 \x04 \x08 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x02 \x00 '
366
- f , length = Frame .parse_frame_header (s [:8 ])
367
- f .parse_body (memoryview (s [8 :8 + length ]))
357
+ s = b'\x00 \x00 \x04 \x08 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x02 \x00 '
358
+ f = decode_frame (s )
368
359
369
360
assert isinstance (f , WindowUpdateFrame )
370
361
assert f .flags == set ()
@@ -386,17 +377,16 @@ def test_headers_frame_serializes_properly(self):
386
377
387
378
s = f .serialize ()
388
379
assert s == (
389
- b'\x00 \x0B \x01 \x05 \x00 \x00 \x00 \x01 ' +
380
+ b'\x00 \x00 \ x0B\x01 \x05 \x00 \x00 \x00 \x01 ' +
390
381
b'hello world'
391
382
)
392
383
393
384
def test_headers_frame_parses_properly (self ):
394
385
s = (
395
- b'\x00 \x0B \x01 \x05 \x00 \x00 \x00 \x01 ' +
386
+ b'\x00 \x00 \ x0B\x01 \x05 \x00 \x00 \x00 \x01 ' +
396
387
b'hello world'
397
388
)
398
- f , length = Frame .parse_frame_header (s [:8 ])
399
- f .parse_body (memoryview (s [8 :8 + length ]))
389
+ f = decode_frame (s )
400
390
401
391
assert isinstance (f , HeadersFrame )
402
392
assert f .flags == set (['END_STREAM' , 'END_HEADERS' ])
@@ -406,11 +396,10 @@ def test_headers_frame_with_priority_parses_properly(self):
406
396
# This test also tests that we can receive a HEADERS frame with no
407
397
# actual headers on it. This is technically possible.
408
398
s = (
409
- b'\x00 \x05 \x01 \x20 \x00 \x00 \x00 \x01 ' +
399
+ b'\x00 \x00 \ x05\x01 \x20 \x00 \x00 \x00 \x01 ' +
410
400
b'\x80 \x00 \x00 \x04 \x40 '
411
401
)
412
- f , length = Frame .parse_frame_header (s [:8 ])
413
- f .parse_body (memoryview (s [8 :8 + length ]))
402
+ f = decode_frame (s )
414
403
415
404
assert isinstance (f , HeadersFrame )
416
405
assert f .flags == set (['PRIORITY' ])
@@ -423,7 +412,7 @@ def test_headers_frame_with_priority_serializes_properly(self):
423
412
# This test also tests that we can receive a HEADERS frame with no
424
413
# actual headers on it. This is technically possible.
425
414
s = (
426
- b'\x00 \x05 \x01 \x20 \x00 \x00 \x00 \x01 ' +
415
+ b'\x00 \x00 \ x05\x01 \x20 \x00 \x00 \x00 \x01 ' +
427
416
b'\x80 \x00 \x00 \x04 \x40 '
428
417
)
429
418
f = HeadersFrame (1 )
@@ -450,14 +439,13 @@ def test_continuation_frame_serializes(self):
450
439
451
440
s = f .serialize ()
452
441
assert s == (
453
- b'\x00 \x0B \x09 \x04 \x00 \x00 \x00 \x01 ' +
442
+ b'\x00 \x00 \ x0B\x09 \x04 \x00 \x00 \x00 \x01 ' +
454
443
b'hello world'
455
444
)
456
445
457
446
def test_continuation_frame_parses_properly (self ):
458
- s = b'\x00 \x0B \x09 \x04 \x00 \x00 \x00 \x01 hello world'
459
- f , length = Frame .parse_frame_header (s [:8 ])
460
- f .parse_body (memoryview (s [8 :8 + length ]))
447
+ s = b'\x00 \x00 \x0B \x09 \x04 \x00 \x00 \x00 \x01 hello world'
448
+ f = decode_frame (s )
461
449
462
450
assert isinstance (f , ContinuationFrame )
463
451
assert f .flags == set (['END_HEADERS' ])
@@ -466,12 +454,12 @@ def test_continuation_frame_parses_properly(self):
466
454
467
455
class TestAltSvcFrame (object ):
468
456
payload_with_origin = (
469
- b'\x00 \x2B \x0A \x00 \x00 \x00 \x00 \x00 '
457
+ b'\x00 \x00 \ x2B\x0A \x00 \x00 \x00 \x00 \x00 '
470
458
b'\x00 \x00 \x00 \x1D \x00 \x50 \x00 \x02 '
471
459
b'h2\x0A google.comhttps://yahoo.com:8080'
472
460
)
473
461
payload_without_origin = (
474
- b'\x00 \x15 \x0A \x00 \x00 \x00 \x00 \x00 '
462
+ b'\x00 \x00 \ x15\x0A \x00 \x00 \x00 \x00 \x00 '
475
463
b'\x00 \x00 \x00 \x1D \x00 \x50 \x00 \x02 '
476
464
b'h2\x0A google.com'
477
465
)
@@ -494,8 +482,7 @@ def test_altsvc_frame_with_origin_serializes_properly(self):
494
482
assert s == self .payload_with_origin
495
483
496
484
def test_altsvc_frame_with_origin_parses_properly (self ):
497
- f , length = Frame .parse_frame_header (self .payload_with_origin [:8 ])
498
- f .parse_body (memoryview (self .payload_with_origin [8 :8 + length ]))
485
+ f = decode_frame (self .payload_with_origin )
499
486
500
487
assert isinstance (f , AltSvcFrame )
501
488
assert f .host == b'google.com'
@@ -515,8 +502,7 @@ def test_altsvc_frame_without_origin_serializes_properly(self):
515
502
assert s == self .payload_without_origin
516
503
517
504
def test_altsvc_frame_without_origin_parses_properly (self ):
518
- f , length = Frame .parse_frame_header (self .payload_without_origin [:8 ])
519
- f .parse_body (memoryview (self .payload_without_origin [8 :8 + length ]))
505
+ f = decode_frame (self .payload_without_origin )
520
506
521
507
assert isinstance (f , AltSvcFrame )
522
508
assert f .host == b'google.com'
@@ -548,12 +534,11 @@ def test_blocked_serializes_properly(self):
548
534
f = BlockedFrame (2 )
549
535
550
536
s = f .serialize ()
551
- assert s == b'\x00 \x00 \x0B \x00 \x00 \x00 \x00 \x02 '
537
+ assert s == b'\x00 \x00 \x00 \ x0B\x00 \x00 \x00 \x00 \x02 '
552
538
553
539
def test_blocked_frame_parses_properly (self ):
554
- s = b'\x00 \x00 \x0B \x00 \x00 \x00 \x00 \x02 '
555
- f , length = Frame .parse_frame_header (s [:8 ])
556
- f .parse_body (memoryview (s [8 :8 + length ]))
540
+ s = b'\x00 \x00 \x00 \x0B \x00 \x00 \x00 \x00 \x02 '
541
+ f = decode_frame (s )
557
542
558
543
assert isinstance (f , BlockedFrame )
559
544
assert f .flags == set ()
@@ -1074,7 +1059,7 @@ def test_that_we_correctly_send_over_the_socket(self):
1074
1059
1075
1060
def test_we_can_read_from_the_socket (self ):
1076
1061
sock = DummySocket ()
1077
- sock .buffer = BytesIO (b'\x00 \x08 \x00 \x01 \x00 \x00 \x00 \x01 testdata' )
1062
+ sock .buffer = BytesIO (b'\x00 \x00 \ x08\x00 \x01 \x00 \x00 \x00 \x01 testdata' )
1078
1063
1079
1064
c = HTTP20Connection ('www.google.com' )
1080
1065
c ._sock = sock
0 commit comments