|
| 1 | +from typing import List |
| 2 | +from unittest import TestCase |
| 3 | +from uuid import uuid4, UUID |
| 4 | + |
| 5 | +import pytest |
| 6 | +from botframework.streaming.payloads import HeaderSerializer |
| 7 | +from botframework.streaming.payloads.models import Header, PayloadTypes |
| 8 | +from botframework.streaming.transport import TransportConstants |
| 9 | + |
| 10 | + |
| 11 | +class TestHeaderSerializer(TestCase): |
| 12 | + def test_can_round_trip(self): |
| 13 | + header = Header() |
| 14 | + header.type = PayloadTypes.REQUEST |
| 15 | + header.payload_length = 168 |
| 16 | + header.id = uuid4() |
| 17 | + header.end = True |
| 18 | + |
| 19 | + buffer: List[int] = [None] * TransportConstants.MAX_PAYLOAD_LENGTH |
| 20 | + offset: int = 0 |
| 21 | + |
| 22 | + length = HeaderSerializer.serialize(header, buffer, offset) |
| 23 | + result = HeaderSerializer.deserialize(buffer, 0, length) |
| 24 | + |
| 25 | + self.assertEqual(header.type, result.type) |
| 26 | + self.assertEqual(header.payload_length, result.payload_length) |
| 27 | + self.assertEqual(header.id, result.id) |
| 28 | + self.assertEqual(header.end, result.end) |
| 29 | + |
| 30 | + def test_serializes_to_ascii(self): |
| 31 | + header = Header() |
| 32 | + header.type = PayloadTypes.REQUEST |
| 33 | + header.payload_length = 168 |
| 34 | + header.id = uuid4() |
| 35 | + header.end = True |
| 36 | + |
| 37 | + buffer: List[int] = [None] * TransportConstants.MAX_PAYLOAD_LENGTH |
| 38 | + offset: int = 0 |
| 39 | + |
| 40 | + length = HeaderSerializer.serialize(header, buffer, offset) |
| 41 | + decoded = bytes(buffer[offset:length]).decode("ascii") |
| 42 | + |
| 43 | + self.assertEqual(f"A.000168.{str(header.id)}.1\n", decoded) |
| 44 | + |
| 45 | + def test_deserializes_from_ascii(self): |
| 46 | + header_id: UUID = uuid4() |
| 47 | + header: str = f"A.000168.{str(header_id)}.1\n" |
| 48 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 49 | + |
| 50 | + result = HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 51 | + |
| 52 | + self.assertEqual("A", result.type) |
| 53 | + self.assertEqual(168, result.payload_length) |
| 54 | + self.assertEqual(header_id, result.id) |
| 55 | + self.assertTrue(result.end) |
| 56 | + |
| 57 | + def test_deserialize_unknown_type(self): |
| 58 | + header_id: UUID = uuid4() |
| 59 | + header: str = f"Z.000168.{str(header_id)}.1\n" |
| 60 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 61 | + |
| 62 | + result = HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 63 | + |
| 64 | + self.assertEqual("Z", result.type) |
| 65 | + self.assertEqual(168, result.payload_length) |
| 66 | + |
| 67 | + def test_deserialize_length_too_short_throws(self): |
| 68 | + header_id: UUID = uuid4() |
| 69 | + header: str = f"A.000168.{str(header_id)}.1\n" |
| 70 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 71 | + |
| 72 | + with pytest.raises(ValueError): |
| 73 | + HeaderSerializer.deserialize(buffer, 0, 5) |
| 74 | + |
| 75 | + def test_deserialize_length_too_long_throws(self): |
| 76 | + header_id: UUID = uuid4() |
| 77 | + header: str = f"A.000168.{str(header_id)}.1\n" |
| 78 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 79 | + |
| 80 | + with pytest.raises(ValueError): |
| 81 | + HeaderSerializer.deserialize(buffer, 0, 55) |
| 82 | + |
| 83 | + def test_deserialize_bad_type_delimiter_throws(self): |
| 84 | + header_id: UUID = uuid4() |
| 85 | + header: str = f"Ax000168.{str(header_id)}.1\n" |
| 86 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 87 | + |
| 88 | + with pytest.raises(ValueError): |
| 89 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 90 | + |
| 91 | + def test_deserialize_bad_length_delimiter_throws(self): |
| 92 | + header_id: UUID = uuid4() |
| 93 | + header: str = f"A.000168x{str(header_id)}.1\n" |
| 94 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 95 | + |
| 96 | + with pytest.raises(ValueError): |
| 97 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 98 | + |
| 99 | + def test_deserialize_bad_id_delimiter_throws(self): |
| 100 | + header_id: UUID = uuid4() |
| 101 | + header: str = f"A.000168.{str(header_id)}x1\n" |
| 102 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 103 | + |
| 104 | + with pytest.raises(ValueError): |
| 105 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 106 | + |
| 107 | + def test_deserialize_bad_terminator_throws(self): |
| 108 | + header_id: UUID = uuid4() |
| 109 | + header: str = f"A.000168.{str(header_id)}.1c" |
| 110 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 111 | + |
| 112 | + with pytest.raises(ValueError): |
| 113 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 114 | + |
| 115 | + def test_deserialize_bad_length_throws(self): |
| 116 | + header_id: UUID = uuid4() |
| 117 | + header: str = f"A.00p168.{str(header_id)}.1\n" |
| 118 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 119 | + |
| 120 | + with pytest.raises(ValueError): |
| 121 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 122 | + |
| 123 | + def test_deserialize_bad_id_throws(self): |
| 124 | + header: str = "A.000168.68e9p9ca-a651-40f4-ad8f-3aaf781862b4.1\n" |
| 125 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 126 | + |
| 127 | + with pytest.raises(ValueError): |
| 128 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
| 129 | + |
| 130 | + def test_deserialize_bad_end_throws(self): |
| 131 | + header_id: UUID = uuid4() |
| 132 | + header: str = f"A.000168.{str(header_id)}.z\n" |
| 133 | + buffer: List[int] = list(bytes(header, "ascii")) |
| 134 | + |
| 135 | + with pytest.raises(ValueError): |
| 136 | + HeaderSerializer.deserialize(buffer, 0, len(buffer)) |
0 commit comments