Skip to content

Commit 92dee75

Browse files
authored
Refactor websocket frame compression tests in test_websocket_writer (#11546)
1 parent 2f6774c commit 92dee75

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
@@ -8,6 +8,7 @@
88
from aiohttp import WSMsgType
99
from aiohttp._websocket.reader import WebSocketDataQueue
1010
from aiohttp.base_protocol import BaseProtocol
11+
from aiohttp.compression_utils import ZLibBackend
1112
from aiohttp.http import WebSocketReader, WebSocketWriter
1213

1314

@@ -86,24 +87,48 @@ async def test_send_text_masked(
8687
writer.transport.write.assert_called_with(b"\x81\x84\rg\xb3fy\x02\xcb\x12") # type: ignore[attr-defined]
8788

8889

90+
@pytest.mark.usefixtures("parametrize_zlib_backend")
8991
async def test_send_compress_text(
9092
protocol: BaseProtocol, transport: asyncio.Transport
9193
) -> None:
94+
compress_obj = ZLibBackend.compressobj(level=ZLibBackend.Z_BEST_SPEED, wbits=-15)
9295
writer = WebSocketWriter(protocol, transport, compress=15)
96+
97+
msg = (
98+
compress_obj.compress(b"text") + compress_obj.flush(ZLibBackend.Z_SYNC_FLUSH)
99+
).removesuffix(b"\x00\x00\xff\xff")
93100
await writer.send_frame(b"text", WSMsgType.TEXT)
94-
writer.transport.write.assert_called_with(b"\xc1\x06*I\xad(\x01\x00") # type: ignore[attr-defined]
101+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
102+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
103+
)
104+
105+
msg = (
106+
compress_obj.compress(b"text") + compress_obj.flush(ZLibBackend.Z_SYNC_FLUSH)
107+
).removesuffix(b"\x00\x00\xff\xff")
95108
await writer.send_frame(b"text", WSMsgType.TEXT)
96-
writer.transport.write.assert_called_with(b"\xc1\x05*\x01b\x00\x00") # type: ignore[attr-defined]
109+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
110+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
111+
)
97112

98113

114+
@pytest.mark.usefixtures("parametrize_zlib_backend")
99115
async def test_send_compress_text_notakeover(
100116
protocol: BaseProtocol, transport: asyncio.Transport
101117
) -> None:
118+
compress_obj = ZLibBackend.compressobj(level=ZLibBackend.Z_BEST_SPEED, wbits=-15)
102119
writer = WebSocketWriter(protocol, transport, compress=15, notakeover=True)
120+
121+
msg = (
122+
compress_obj.compress(b"text") + compress_obj.flush(ZLibBackend.Z_FULL_FLUSH)
123+
).removesuffix(b"\x00\x00\xff\xff")
103124
await writer.send_frame(b"text", WSMsgType.TEXT)
104-
writer.transport.write.assert_called_with(b"\xc1\x06*I\xad(\x01\x00") # type: ignore[attr-defined]
125+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
126+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
127+
)
105128
await writer.send_frame(b"text", WSMsgType.TEXT)
106-
writer.transport.write.assert_called_with(b"\xc1\x06*I\xad(\x01\x00") # type: ignore[attr-defined]
129+
writer.transport.write.assert_called_with( # type: ignore[attr-defined]
130+
b"\xc1" + len(msg).to_bytes(1, "big") + msg
131+
)
107132

108133

109134
async def test_send_compress_text_per_message(

0 commit comments

Comments
 (0)