Skip to content

Commit 7c9f70c

Browse files
authored
[PR #11546/92dee75 backport][3.13] Refactor websocket frame compression tests in test_websocket_writer (#11556)
(cherry picked from commit 92dee75)
1 parent d8f0d03 commit 7c9f70c

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

CHANGES/11546.contrib.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed ``test_send_compress_text`` failing when alternative zlib implementation
2+
is used. (``zlib-ng`` in python 3.14 windows build) -- by :user:`Cycloctane`.

tests/test_websocket_writer.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from aiohttp import WSMsgType
99
from aiohttp._websocket.reader import WebSocketDataQueue
10+
from aiohttp.compression_utils import ZLibBackend
1011
from aiohttp.http import WebSocketReader, WebSocketWriter
1112

1213

@@ -83,20 +84,44 @@ async def test_send_text_masked(protocol, transport) -> None:
8384
writer.transport.write.assert_called_with(b"\x81\x84\rg\xb3fy\x02\xcb\x12") # type: ignore[attr-defined]
8485

8586

87+
@pytest.mark.usefixtures("parametrize_zlib_backend")
8688
async def test_send_compress_text(protocol, transport) -> None:
89+
compress_obj = ZLibBackend.compressobj(level=ZLibBackend.Z_BEST_SPEED, wbits=-15)
8790
writer = WebSocketWriter(protocol, transport, compress=15)
91+
92+
msg = (
93+
compress_obj.compress(b"text") + compress_obj.flush(ZLibBackend.Z_SYNC_FLUSH)
94+
).removesuffix(b"\x00\x00\xff\xff")
8895
await writer.send_frame(b"text", WSMsgType.TEXT)
89-
writer.transport.write.assert_called_with(b"\xc1\x06*I\xad(\x01\x00") # type: ignore[attr-defined]
96+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
97+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
98+
)
99+
100+
msg = (
101+
compress_obj.compress(b"text") + compress_obj.flush(ZLibBackend.Z_SYNC_FLUSH)
102+
).removesuffix(b"\x00\x00\xff\xff")
90103
await writer.send_frame(b"text", WSMsgType.TEXT)
91-
writer.transport.write.assert_called_with(b"\xc1\x05*\x01b\x00\x00") # type: ignore[attr-defined]
104+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
105+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
106+
)
92107

93108

109+
@pytest.mark.usefixtures("parametrize_zlib_backend")
94110
async def test_send_compress_text_notakeover(protocol, transport) -> None:
111+
compress_obj = ZLibBackend.compressobj(level=ZLibBackend.Z_BEST_SPEED, wbits=-15)
95112
writer = WebSocketWriter(protocol, transport, compress=15, notakeover=True)
113+
114+
msg = (
115+
compress_obj.compress(b"text") + compress_obj.flush(ZLibBackend.Z_FULL_FLUSH)
116+
).removesuffix(b"\x00\x00\xff\xff")
96117
await writer.send_frame(b"text", WSMsgType.TEXT)
97-
writer.transport.write.assert_called_with(b"\xc1\x06*I\xad(\x01\x00") # type: ignore[attr-defined]
118+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
119+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
120+
)
98121
await writer.send_frame(b"text", WSMsgType.TEXT)
99-
writer.transport.write.assert_called_with(b"\xc1\x06*I\xad(\x01\x00") # type: ignore[attr-defined]
122+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
123+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
124+
)
100125

101126

102127
async def test_send_compress_text_per_message(protocol, transport) -> None:

0 commit comments

Comments
 (0)