def test_binary_output_length_smaller_or_equal_127(frame_factory, monkeypatch):
# this unit test is very similar to previous one, the difference being of
# course the length of the packet in question
monkeypatch.setattr(
'lomond.frame.make_masking_key', lambda: b'\x00\x00\x00\x00')
frame = frame_factory(Opcode.BINARY, payload=b'\x01' * 127)
frame_binary = frame.to_bytes()
expected_length = 1 + 1 + 2 + 4 + 127
# ^ ^ ^ ^ ^
# | | | | +-- payload length (127 bytes)
# | | | +------- mask length (4 bytes )
# | | +----------- length as uint16_t (2 bytes )
# | +--------------- masking byte
# +------------------- opcode byte
assert len(frame_binary) == expected_length
# the first byte doesn't change at all, so please look inside the previous
# function for in-depth explanation
assert six.indexbytes(frame_binary, 0) == 0b10000010
# in the second byte of the header, length field should be set to 126 to
# indicate a payload of length which should be encoded as uint16_t
# just for the fun of it, we can decode the actual length value:
_length = six.indexbytes(frame_binary, 1) & 0b01111111
assert _length == 126
assert frame_binary[4:8] == b'\x00\x00\x00\x00'
assert frame_binary[8:] == frame.payload
评论列表
文章目录