26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
from connection.message_codec import BYTE_ORDER, MESSAGE_SIZE_BYTES, MESSAGE_TYPE_BYTES, encode_message
|
|
|
|
|
|
class TestEncodeMessage:
|
|
def test_encodes_empty_payload(self):
|
|
result = encode_message(message_type=1, message=b"")
|
|
|
|
expected_size = MESSAGE_TYPE_BYTES.to_bytes(MESSAGE_SIZE_BYTES, byteorder=BYTE_ORDER)
|
|
expected_type = (1).to_bytes(MESSAGE_TYPE_BYTES, byteorder=BYTE_ORDER)
|
|
assert result == expected_size + expected_type
|
|
|
|
def test_encodes_payload(self):
|
|
payload = b"\x0a\x0b\x0c"
|
|
result = encode_message(message_type=42, message=payload)
|
|
|
|
expected_size = (MESSAGE_TYPE_BYTES + len(payload)).to_bytes(MESSAGE_SIZE_BYTES, byteorder=BYTE_ORDER)
|
|
expected_type = (42).to_bytes(MESSAGE_TYPE_BYTES, byteorder=BYTE_ORDER)
|
|
assert result == expected_size + expected_type + payload
|
|
|
|
def test_size_field_excludes_itself(self):
|
|
payload = b"\xff"
|
|
result = encode_message(message_type=0, message=payload)
|
|
|
|
size_field = int.from_bytes(result[:MESSAGE_SIZE_BYTES], byteorder=BYTE_ORDER)
|
|
assert size_field == MESSAGE_TYPE_BYTES + len(payload)
|