mitmproxy/test/http/http2/test_frames.py

670 lines
20 KiB
Python
Raw Normal View History

2015-09-15 17:12:15 +00:00
from io import BytesIO
2015-08-01 12:49:15 +00:00
from netlib import tcp, tutils
2015-07-14 21:02:14 +00:00
from netlib.http.http2.frame import *
2015-06-04 12:28:09 +00:00
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
def hex_to_file(data):
data = data.decode('hex')
2015-09-15 17:12:15 +00:00
return tcp.Reader(BytesIO(data))
2015-06-01 10:34:50 +00:00
2015-05-25 10:10:21 +00:00
def test_invalid_flags():
2015-05-30 00:02:58 +00:00
tutils.raises(
ValueError,
DataFrame,
flags=ContinuationFrame.FLAG_END_HEADERS,
stream_id=0x1234567,
payload='foobar')
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_frame_equality():
a = DataFrame(
length=6,
flags=Frame.FLAG_END_STREAM,
stream_id=0x1234567,
payload='foobar')
b = DataFrame(
length=6,
flags=Frame.FLAG_END_STREAM,
stream_id=0x1234567,
payload='foobar')
2015-09-20 23:22:05 +00:00
assert a == b
2015-05-25 10:10:21 +00:00
2015-06-04 12:28:09 +00:00
def test_too_large_frames():
f = DataFrame(
length=9000,
flags=Frame.FLAG_END_STREAM,
stream_id=0x1234567,
payload='foobar' * 3000)
2015-09-15 22:04:23 +00:00
tutils.raises(HttpSyntaxException, f.to_bytes)
2015-06-04 12:28:09 +00:00
2015-05-25 10:10:21 +00:00
def test_data_frame_to_bytes():
f = DataFrame(
length=6,
flags=Frame.FLAG_END_STREAM,
stream_id=0x1234567,
payload='foobar')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000006000101234567666f6f626172'
2015-05-25 10:10:21 +00:00
2015-05-30 00:02:58 +00:00
f = DataFrame(
length=11,
flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED),
stream_id=0x1234567,
payload='foobar',
2015-05-30 00:02:58 +00:00
pad_length=3)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000a00090123456703666f6f626172000000'
2015-05-25 10:10:21 +00:00
f = DataFrame(
length=6,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
payload='foobar')
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_data_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000006000101234567666f6f626172'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, DataFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 6
assert f.TYPE == DataFrame.TYPE
assert f.flags == Frame.FLAG_END_STREAM
assert f.stream_id == 0x1234567
assert f.payload == 'foobar'
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('00000a00090123456703666f6f626172000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, DataFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 10
assert f.TYPE == DataFrame.TYPE
assert f.flags == Frame.FLAG_END_STREAM | Frame.FLAG_PADDED
assert f.stream_id == 0x1234567
assert f.payload == 'foobar'
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_data_frame_human_readable():
2015-05-30 00:02:58 +00:00
f = DataFrame(
length=11,
flags=(Frame.FLAG_END_STREAM | Frame.FLAG_PADDED),
stream_id=0x1234567,
payload='foobar',
2015-05-30 00:02:58 +00:00
pad_length=3)
assert f.human_readable()
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_headers_frame_to_bytes():
f = HeadersFrame(
length=6,
flags=(Frame.FLAG_NO_FLAGS),
stream_id=0x1234567,
header_block_fragment='668594e75e31d9'.decode('hex'))
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000007010001234567668594e75e31d9'
2015-05-27 15:53:06 +00:00
f = HeadersFrame(
length=10,
flags=(HeadersFrame.FLAG_PADDED),
stream_id=0x1234567,
header_block_fragment='668594e75e31d9'.decode('hex'),
2015-05-27 15:53:06 +00:00
pad_length=3)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000b01080123456703668594e75e31d9000000'
2015-05-27 15:53:06 +00:00
f = HeadersFrame(
length=10,
flags=(HeadersFrame.FLAG_PRIORITY),
stream_id=0x1234567,
header_block_fragment='668594e75e31d9'.decode('hex'),
2015-05-27 15:53:06 +00:00
exclusive=True,
stream_dependency=0x7654321,
weight=42)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000c012001234567876543212a668594e75e31d9'
2015-05-27 15:53:06 +00:00
f = HeadersFrame(
length=14,
flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY),
stream_id=0x1234567,
header_block_fragment='668594e75e31d9'.decode('hex'),
2015-05-27 15:53:06 +00:00
pad_length=3,
exclusive=True,
stream_dependency=0x7654321,
weight=42)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00001001280123456703876543212a668594e75e31d9000000'
2015-05-27 15:53:06 +00:00
f = HeadersFrame(
length=14,
flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY),
stream_id=0x1234567,
header_block_fragment='668594e75e31d9'.decode('hex'),
2015-05-27 15:53:06 +00:00
pad_length=3,
exclusive=False,
stream_dependency=0x7654321,
weight=42)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00001001280123456703076543212a668594e75e31d9000000'
2015-05-25 10:10:21 +00:00
f = HeadersFrame(
length=6,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
header_block_fragment='668594e75e31d9'.decode('hex'))
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_headers_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'000007010001234567668594e75e31d9'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, HeadersFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 7
assert f.TYPE == HeadersFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x1234567
assert f.header_block_fragment == '668594e75e31d9'.decode('hex')
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00000b01080123456703668594e75e31d9000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, HeadersFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 11
assert f.TYPE == HeadersFrame.TYPE
assert f.flags == HeadersFrame.FLAG_PADDED
assert f.stream_id == 0x1234567
assert f.header_block_fragment == '668594e75e31d9'.decode('hex')
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00000c012001234567876543212a668594e75e31d9'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, HeadersFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 12
assert f.TYPE == HeadersFrame.TYPE
assert f.flags == HeadersFrame.FLAG_PRIORITY
assert f.stream_id == 0x1234567
assert f.header_block_fragment == '668594e75e31d9'.decode('hex')
assert f.exclusive == True
assert f.stream_dependency == 0x7654321
assert f.weight == 42
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00001001280123456703876543212a668594e75e31d9000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, HeadersFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 16
assert f.TYPE == HeadersFrame.TYPE
assert f.flags == HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY
assert f.stream_id == 0x1234567
assert f.header_block_fragment == '668594e75e31d9'.decode('hex')
assert f.exclusive == True
assert f.stream_dependency == 0x7654321
assert f.weight == 42
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00001001280123456703076543212a668594e75e31d9000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, HeadersFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 16
assert f.TYPE == HeadersFrame.TYPE
assert f.flags == HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY
assert f.stream_id == 0x1234567
assert f.header_block_fragment == '668594e75e31d9'.decode('hex')
assert f.exclusive == False
assert f.stream_dependency == 0x7654321
assert f.weight == 42
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_headers_frame_human_readable():
2015-05-27 15:53:06 +00:00
f = HeadersFrame(
length=7,
flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY),
stream_id=0x1234567,
header_block_fragment=b'',
2015-05-27 15:53:06 +00:00
pad_length=3,
exclusive=False,
stream_dependency=0x7654321,
weight=42)
assert f.human_readable()
f = HeadersFrame(
length=14,
flags=(HeadersFrame.FLAG_PADDED | HeadersFrame.FLAG_PRIORITY),
stream_id=0x1234567,
header_block_fragment='668594e75e31d9'.decode('hex'),
2015-05-27 15:53:06 +00:00
pad_length=3,
exclusive=False,
stream_dependency=0x7654321,
weight=42)
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_priority_frame_to_bytes():
2015-05-30 00:02:58 +00:00
f = PriorityFrame(
length=5,
flags=(Frame.FLAG_NO_FLAGS),
stream_id=0x1234567,
2015-05-30 00:02:58 +00:00
exclusive=True,
2015-08-26 19:04:13 +00:00
stream_dependency=0x0,
2015-05-30 00:02:58 +00:00
weight=42)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000005020001234567800000002a'
2015-05-25 10:10:21 +00:00
2015-05-30 00:02:58 +00:00
f = PriorityFrame(
length=5,
flags=(Frame.FLAG_NO_FLAGS),
stream_id=0x1234567,
2015-05-30 00:02:58 +00:00
exclusive=False,
stream_dependency=0x7654321,
weight=21)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '0000050200012345670765432115'
2015-05-25 10:10:21 +00:00
f = PriorityFrame(
length=5,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
stream_dependency=0x1234567)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_priority_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000005020001234567876543212a'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, PriorityFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 5
assert f.TYPE == PriorityFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x1234567
assert f.exclusive == True
assert f.stream_dependency == 0x7654321
assert f.weight == 42
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('0000050200012345670765432115'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, PriorityFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 5
assert f.TYPE == PriorityFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x1234567
assert f.exclusive == False
assert f.stream_dependency == 0x7654321
assert f.weight == 21
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_priority_frame_human_readable():
2015-05-30 00:02:58 +00:00
f = PriorityFrame(
length=5,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
2015-05-30 00:02:58 +00:00
exclusive=False,
stream_dependency=0x7654321,
weight=21)
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_rst_stream_frame_to_bytes():
f = RstStreamFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
error_code=0x7654321)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000403000123456707654321'
2015-05-25 10:10:21 +00:00
f = RstStreamFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_rst_stream_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('00000403000123456707654321'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, RstStreamFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 4
assert f.TYPE == RstStreamFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x1234567
assert f.error_code == 0x07654321
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_rst_stream_frame_human_readable():
f = RstStreamFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
error_code=0x7654321)
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_settings_frame_to_bytes():
f = SettingsFrame(
length=0,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000000040000000000'
2015-05-25 10:10:21 +00:00
f = SettingsFrame(
length=0,
flags=SettingsFrame.FLAG_ACK,
stream_id=0x0)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000000040100000000'
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
f = SettingsFrame(
length=6,
flags=SettingsFrame.FLAG_ACK,
stream_id=0x0,
2015-05-27 15:53:06 +00:00
settings={
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1})
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000006040100000000000200000001'
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
f = SettingsFrame(
length=12,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-27 15:53:06 +00:00
settings={
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1,
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678})
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000c040000000000000200000001000312345678'
2015-05-25 10:10:21 +00:00
f = SettingsFrame(
length=0,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_settings_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000000040000000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, SettingsFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 0
assert f.TYPE == SettingsFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x0
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000000040100000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, SettingsFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 0
assert f.TYPE == SettingsFrame.TYPE
assert f.flags == SettingsFrame.FLAG_ACK
assert f.stream_id == 0x0
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000006040100000000000200000001'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, SettingsFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 6
assert f.TYPE == SettingsFrame.TYPE
assert f.flags == SettingsFrame.FLAG_ACK, 0x0
assert f.stream_id == 0x0
assert len(f.settings) == 1
assert f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 1
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00000c040000000000000200000001000312345678'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, SettingsFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 12
assert f.TYPE == SettingsFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x0
assert len(f.settings) == 2
assert f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH] == 1
assert f.settings[SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS] == 0x12345678
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_settings_frame_human_readable():
f = SettingsFrame(
length=12,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
settings={})
assert f.human_readable()
2015-05-27 15:53:06 +00:00
f = SettingsFrame(
length=12,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-27 15:53:06 +00:00
settings={
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1,
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678})
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_push_promise_frame_to_bytes():
2015-05-30 00:02:58 +00:00
f = PushPromiseFrame(
length=10,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
promised_stream=0x7654321,
header_block_fragment='foobar')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000a05000123456707654321666f6f626172'
2015-05-25 10:10:21 +00:00
2015-05-30 00:02:58 +00:00
f = PushPromiseFrame(
length=14,
flags=HeadersFrame.FLAG_PADDED,
stream_id=0x1234567,
promised_stream=0x7654321,
header_block_fragment='foobar',
2015-05-30 00:02:58 +00:00
pad_length=3)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000e0508012345670307654321666f6f626172000000'
2015-05-25 10:10:21 +00:00
f = PushPromiseFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
promised_stream=0x1234567)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
f = PushPromiseFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
promised_stream=0x0)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_push_promise_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('00000a05000123456707654321666f6f626172'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, PushPromiseFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 10
assert f.TYPE == PushPromiseFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x1234567
assert f.header_block_fragment == 'foobar'
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00000e0508012345670307654321666f6f626172000000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, PushPromiseFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 14
assert f.TYPE == PushPromiseFrame.TYPE
assert f.flags == PushPromiseFrame.FLAG_PADDED
assert f.stream_id == 0x1234567
assert f.header_block_fragment == 'foobar'
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_push_promise_frame_human_readable():
2015-05-30 00:02:58 +00:00
f = PushPromiseFrame(
length=14,
flags=HeadersFrame.FLAG_PADDED,
stream_id=0x1234567,
promised_stream=0x7654321,
header_block_fragment='foobar',
2015-05-30 00:02:58 +00:00
pad_length=3)
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_ping_frame_to_bytes():
f = PingFrame(
length=8,
flags=PingFrame.FLAG_ACK,
stream_id=0x0,
payload=b'foobar')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000008060100000000666f6f6261720000'
2015-05-25 10:10:21 +00:00
f = PingFrame(
length=8,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
payload=b'foobardeadbeef')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000008060000000000666f6f6261726465'
2015-05-25 10:10:21 +00:00
f = PingFrame(
length=8,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_ping_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000008060100000000666f6f6261720000'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, PingFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 8
assert f.TYPE == PingFrame.TYPE
assert f.flags == PingFrame.FLAG_ACK
assert f.stream_id == 0x0
assert f.payload == b'foobar\0\0'
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000008060000000000666f6f6261726465'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, PingFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 8
assert f.TYPE == PingFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x0
assert f.payload == b'foobarde'
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_ping_frame_human_readable():
f = PingFrame(
length=8,
flags=PingFrame.FLAG_ACK,
stream_id=0x0,
payload=b'foobar')
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_goaway_frame_to_bytes():
2015-05-30 00:02:58 +00:00
f = GoAwayFrame(
length=8,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-30 00:02:58 +00:00
last_stream=0x1234567,
error_code=0x87654321,
data=b'')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '0000080700000000000123456787654321'
2015-05-30 00:02:58 +00:00
f = GoAwayFrame(
length=14,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-30 00:02:58 +00:00
last_stream=0x1234567,
error_code=0x87654321,
data=b'foobar')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000e0700000000000123456787654321666f6f626172'
2015-05-30 00:02:58 +00:00
f = GoAwayFrame(
length=8,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
2015-05-30 00:02:58 +00:00
last_stream=0x1234567,
error_code=0x87654321)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_goaway_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'0000080700000000000123456787654321'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, GoAwayFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 8
assert f.TYPE == GoAwayFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x0
assert f.last_stream == 0x1234567
assert f.error_code == 0x87654321
assert f.data == b''
2015-05-25 10:10:21 +00:00
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file(
'00000e0700000000000123456787654321666f6f626172'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, GoAwayFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 14
assert f.TYPE == GoAwayFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x0
assert f.last_stream == 0x1234567
assert f.error_code == 0x87654321
assert f.data == b'foobar'
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_go_away_frame_human_readable():
2015-05-30 00:02:58 +00:00
f = GoAwayFrame(
length=14,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-30 00:02:58 +00:00
last_stream=0x1234567,
error_code=0x87654321,
data=b'foobar')
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_window_update_frame_to_bytes():
2015-05-30 00:02:58 +00:00
f = WindowUpdateFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-30 00:02:58 +00:00
window_size_increment=0x1234567)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000408000000000001234567'
2015-05-25 10:10:21 +00:00
2015-05-30 00:02:58 +00:00
f = WindowUpdateFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
2015-05-30 00:02:58 +00:00
window_size_increment=0x7654321)
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '00000408000123456707654321'
2015-05-25 10:10:21 +00:00
2015-05-30 00:02:58 +00:00
f = WindowUpdateFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x0,
2015-05-30 00:02:58 +00:00
window_size_increment=0xdeadbeef)
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x0, window_size_increment=0)
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_window_update_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('00000408000000000001234567'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, WindowUpdateFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 4
assert f.TYPE == WindowUpdateFrame.TYPE
assert f.flags == Frame.FLAG_NO_FLAGS
assert f.stream_id == 0x0
assert f.window_size_increment == 0x1234567
2015-05-25 10:10:21 +00:00
2015-05-27 15:53:06 +00:00
def test_window_update_frame_human_readable():
2015-05-30 00:02:58 +00:00
f = WindowUpdateFrame(
length=4,
flags=Frame.FLAG_NO_FLAGS,
stream_id=0x1234567,
2015-05-30 00:02:58 +00:00
window_size_increment=0x7654321)
assert f.human_readable()
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_continuation_frame_to_bytes():
2015-05-30 00:02:58 +00:00
f = ContinuationFrame(
length=6,
flags=ContinuationFrame.FLAG_END_HEADERS,
stream_id=0x1234567,
header_block_fragment='foobar')
2015-09-20 23:22:05 +00:00
assert f.to_bytes().encode('hex') == '000006090401234567666f6f626172'
2015-05-25 10:10:21 +00:00
f = ContinuationFrame(
length=6,
flags=ContinuationFrame.FLAG_END_HEADERS,
stream_id=0x0,
header_block_fragment='foobar')
2015-05-25 10:10:21 +00:00
tutils.raises(ValueError, f.to_bytes)
2015-05-27 15:53:06 +00:00
2015-05-25 10:10:21 +00:00
def test_continuation_frame_from_bytes():
2015-06-05 18:55:32 +00:00
f = Frame.from_file(hex_to_file('000006090401234567666f6f626172'))
2015-05-25 10:10:21 +00:00
assert isinstance(f, ContinuationFrame)
2015-09-20 23:22:05 +00:00
assert f.length == 6
assert f.TYPE == ContinuationFrame.TYPE
assert f.flags == ContinuationFrame.FLAG_END_HEADERS
assert f.stream_id == 0x1234567
assert f.header_block_fragment == 'foobar'
2015-05-27 15:53:06 +00:00
def test_continuation_frame_human_readable():
2015-05-30 00:02:58 +00:00
f = ContinuationFrame(
length=6,
flags=ContinuationFrame.FLAG_END_HEADERS,
stream_id=0x1234567,
header_block_fragment='foobar')
assert f.human_readable()