Skip to content

Commit

Permalink
Add tests for previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Apr 17, 2022
1 parent 8850eb0 commit 778879e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/websockets/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ def __str__(self) -> str:
data = str(Close.parse(self.data))
elif self.data:
# We don't know if a Continuation frame contains text or binary.
# Ping and Pong frames could contain UTF-8. Attempt to decode as
# UTF-8 and display it as text; fallback to binary.
# Ping and Pong frames could contain UTF-8.
# Attempt to decode as UTF-8 and display it as text; fallback to
# binary. If self.data is a memoryview, it has no decode() method,
# which raises AttributeError.
try:
data = repr(self.data.decode())
coding = "text"
except UnicodeDecodeError:
except (UnicodeDecodeError, AttributeError):
binary = self.data
if len(binary) > 25:
binary = b"".join([binary[:16], b"\x00\x00", binary[-8:]])
Expand Down
38 changes: 38 additions & 0 deletions tests/test_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def test_cont_binary(self):
"CONT fc fd fe ff [binary, 4 bytes, continued]",
)

def test_cont_binary_from_memoryview(self):
self.assertEqual(
str(Frame(OP_CONT, memoryview(b"\xfc\xfd\xfe\xff"), fin=False)),
"CONT fc fd fe ff [binary, 4 bytes, continued]",
)

def test_cont_final_text(self):
self.assertEqual(
str(Frame(OP_CONT, b" cr\xc3\xa8me")),
Expand All @@ -219,6 +225,12 @@ def test_cont_final_binary(self):
"CONT fc fd fe ff [binary, 4 bytes]",
)

def test_cont_final_binary_from_memoryview(self):
self.assertEqual(
str(Frame(OP_CONT, memoryview(b"\xfc\xfd\xfe\xff"))),
"CONT fc fd fe ff [binary, 4 bytes]",
)

def test_cont_text_truncated(self):
self.assertEqual(
str(Frame(OP_CONT, b"caf\xc3\xa9 " * 16, fin=False)),
Expand All @@ -233,6 +245,13 @@ def test_cont_binary_truncated(self):
" f8 f9 fa fb fc fd fe ff [binary, 256 bytes, continued]",
)

def test_cont_binary_truncated_from_memoryview(self):
self.assertEqual(
str(Frame(OP_CONT, memoryview(bytes(range(256))), fin=False)),
"CONT 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ..."
" f8 f9 fa fb fc fd fe ff [binary, 256 bytes, continued]",
)

def test_text(self):
self.assertEqual(
str(Frame(OP_TEXT, b"caf\xc3\xa9")),
Expand Down Expand Up @@ -264,19 +283,38 @@ def test_binary(self):
"BINARY 00 01 02 03 [4 bytes]",
)

def test_binary_from_memoryview(self):
self.assertEqual(
str(Frame(OP_BINARY, memoryview(b"\x00\x01\x02\x03"))),
"BINARY 00 01 02 03 [4 bytes]",
)

def test_binary_non_final(self):
self.assertEqual(
str(Frame(OP_BINARY, b"\x00\x01\x02\x03", fin=False)),
"BINARY 00 01 02 03 [4 bytes, continued]",
)

def test_binary_non_final_from_memoryview(self):
self.assertEqual(
str(Frame(OP_BINARY, memoryview(b"\x00\x01\x02\x03"), fin=False)),
"BINARY 00 01 02 03 [4 bytes, continued]",
)

def test_binary_truncated(self):
self.assertEqual(
str(Frame(OP_BINARY, bytes(range(256)))),
"BINARY 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ..."
" f8 f9 fa fb fc fd fe ff [256 bytes]",
)

def test_binary_truncated_from_memoryview(self):
self.assertEqual(
str(Frame(OP_BINARY, memoryview(bytes(range(256))))),
"BINARY 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ..."
" f8 f9 fa fb fc fd fe ff [256 bytes]",
)

def test_close(self):
self.assertEqual(
str(Frame(OP_CLOSE, b"\x03\xe8")),
Expand Down

0 comments on commit 778879e

Please sign in to comment.