|
44 | 44 |
|
45 | 45 |
|
46 | 46 | def test_log_filterer():
|
47 |
| - filterer = shard._log_filterer("TOKEN") |
| 47 | + filterer = shard._log_filterer(b"TOKEN") |
48 | 48 |
|
49 |
| - returned = filterer("this log contains the TOKEN and it should get removed and the TOKEN here too") |
| 49 | + returned = filterer(b"this log contains the TOKEN and it should get removed and the TOKEN here too") |
50 | 50 | assert returned == (
|
51 |
| - "this log contains the **REDACTED TOKEN** and it should get removed and the **REDACTED TOKEN** here too" |
| 51 | + b"this log contains the **REDACTED TOKEN** and it should get removed and the **REDACTED TOKEN** here too" |
52 | 52 | )
|
53 | 53 |
|
54 | 54 |
|
@@ -275,100 +275,69 @@ def test__handle_other_message_when_message_type_is_unknown(self, transport_impl
|
275 | 275 | assert exc_info.value.__cause__ is exception
|
276 | 276 |
|
277 | 277 | @pytest.mark.asyncio
|
278 |
| - async def test__receive_and_check_text_when_message_type_is_TEXT(self, transport_impl): |
| 278 | + async def test__receive_and_check_text(self, transport_impl): |
279 | 279 | transport_impl._ws.receive = mock.AsyncMock(
|
280 | 280 | return_value=StubResponse(type=aiohttp.WSMsgType.TEXT, data="some text")
|
281 | 281 | )
|
282 | 282 |
|
283 |
| - assert await transport_impl._receive_and_check_text() == "some text" |
| 283 | + assert await transport_impl._receive_and_check_text() == b"some text" |
284 | 284 |
|
285 | 285 | transport_impl._ws.receive.assert_awaited_once_with()
|
286 | 286 |
|
287 | 287 | @pytest.mark.asyncio
|
288 | 288 | async def test__receive_and_check_text_when_message_type_is_unknown(self, transport_impl):
|
289 |
| - mock_exception = errors.GatewayError("aye") |
290 | 289 | transport_impl._ws.receive = mock.AsyncMock(return_value=StubResponse(type=aiohttp.WSMsgType.BINARY))
|
291 | 290 |
|
292 |
| - with mock.patch.object( |
293 |
| - shard._GatewayTransport, "_handle_other_message", side_effect=mock_exception |
294 |
| - ) as handle_other_message: |
295 |
| - with pytest.raises(errors.GatewayError) as exc_info: |
296 |
| - await transport_impl._receive_and_check_text() |
| 291 | + with pytest.raises( |
| 292 | + errors.GatewayTransportError, |
| 293 | + match="Gateway transport error: Unexpected message type received BINARY, expected TEXT", |
| 294 | + ): |
| 295 | + await transport_impl._receive_and_check_text() |
297 | 296 |
|
298 |
| - assert exc_info.value is mock_exception |
299 | 297 | transport_impl._ws.receive.assert_awaited_once_with()
|
300 |
| - handle_other_message.assert_called_once_with(transport_impl._ws.receive.return_value) |
301 | 298 |
|
302 | 299 | @pytest.mark.asyncio
|
303 |
| - async def test__receive_and_check_zlib_when_message_type_is_BINARY(self, transport_impl): |
304 |
| - response = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"some initial data") |
305 |
| - transport_impl._ws.receive = mock.AsyncMock(return_value=response) |
| 300 | + async def test__receive_and_check_zlib_when_payload_split_across_frames(self, transport_impl): |
| 301 | + response1 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"x\xda\xf2H\xcd\xc9") |
| 302 | + response2 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"\xc9W(\xcf/\xcaIQ\x04\x00\x00") |
| 303 | + response3 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"\x00\xff\xff") |
| 304 | + transport_impl._ws.receive = mock.AsyncMock(side_effect=[response1, response2, response3]) |
306 | 305 |
|
307 |
| - with mock.patch.object( |
308 |
| - shard._GatewayTransport, "_receive_and_check_complete_zlib_package" |
309 |
| - ) as receive_and_check_complete_zlib_package: |
310 |
| - assert ( |
311 |
| - await transport_impl._receive_and_check_zlib() is receive_and_check_complete_zlib_package.return_value |
312 |
| - ) |
| 306 | + assert await transport_impl._receive_and_check_zlib() == b"Hello world!" |
313 | 307 |
|
314 |
| - transport_impl._ws.receive.assert_awaited_once_with() |
315 |
| - receive_and_check_complete_zlib_package.assert_awaited_once_with(b"some initial data") |
| 308 | + assert transport_impl._ws.receive.call_count == 3 |
316 | 309 |
|
317 | 310 | @pytest.mark.asyncio
|
318 |
| - async def test__receive_and_check_zlib_when_message_type_is_BINARY_and_the_full_payload(self, transport_impl): |
319 |
| - response = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"some initial data\x00\x00\xff\xff") |
| 311 | + async def test__receive_and_check_zlib_when_full_payload_in_one_frame(self, transport_impl): |
| 312 | + response = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"x\xdaJLD\x07\x00\x00\x00\x00\xff\xff") |
320 | 313 | transport_impl._ws.receive = mock.AsyncMock(return_value=response)
|
321 |
| - transport_impl._zlib = mock.Mock(decompress=mock.Mock(return_value=b"aaaaaaaaaaaaaaaaaa")) |
322 | 314 |
|
323 |
| - assert await transport_impl._receive_and_check_zlib() == "aaaaaaaaaaaaaaaaaa" |
| 315 | + assert await transport_impl._receive_and_check_zlib() == b"aaaaaaaaaaaaaaaaaa" |
324 | 316 |
|
325 | 317 | transport_impl._ws.receive.assert_awaited_once_with()
|
326 |
| - transport_impl._zlib.decompress.assert_called_once_with(response.data) |
327 | 318 |
|
328 | 319 | @pytest.mark.asyncio
|
329 | 320 | async def test__receive_and_check_zlib_when_message_type_is_unknown(self, transport_impl):
|
330 |
| - mock_exception = errors.GatewayError("aye") |
331 | 321 | transport_impl._ws.receive = mock.AsyncMock(return_value=StubResponse(type=aiohttp.WSMsgType.TEXT))
|
332 | 322 |
|
333 |
| - with mock.patch.object( |
334 |
| - shard._GatewayTransport, "_handle_other_message", side_effect=mock_exception |
335 |
| - ) as handle_other_message: |
336 |
| - with pytest.raises(errors.GatewayError) as exc_info: |
337 |
| - await transport_impl._receive_and_check_zlib() |
338 |
| - |
339 |
| - assert exc_info.value is mock_exception |
340 |
| - transport_impl._ws.receive.assert_awaited_once_with() |
341 |
| - handle_other_message.assert_called_once_with(transport_impl._ws.receive.return_value) |
342 |
| - |
343 |
| - @pytest.mark.asyncio |
344 |
| - async def test__receive_and_check_complete_zlib_package_for_unexpected_message_type(self, transport_impl): |
345 |
| - mock_exception = errors.GatewayError("aye") |
346 |
| - response = StubResponse(type=aiohttp.WSMsgType.TEXT) |
347 |
| - transport_impl._ws.receive = mock.AsyncMock(return_value=response) |
348 |
| - |
349 |
| - with mock.patch.object( |
350 |
| - shard._GatewayTransport, "_handle_other_message", side_effect=mock_exception |
351 |
| - ) as handle_other_message: |
352 |
| - with pytest.raises(errors.GatewayError) as exc_info: |
353 |
| - await transport_impl._receive_and_check_complete_zlib_package(b"some") |
354 |
| - |
355 |
| - assert exc_info.value is mock_exception |
356 |
| - transport_impl._ws.receive.assert_awaited_with() |
357 |
| - handle_other_message.assert_called_once_with(response) |
| 323 | + with pytest.raises( |
| 324 | + errors.GatewayTransportError, |
| 325 | + match="Gateway transport error: Unexpected message type received TEXT, expected BINARY", |
| 326 | + ): |
| 327 | + await transport_impl._receive_and_check_zlib() |
358 | 328 |
|
359 | 329 | @pytest.mark.asyncio
|
360 |
| - async def test__receive_and_check_complete_zlib_package(self, transport_impl): |
361 |
| - response1 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"more") |
362 |
| - response2 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"data") |
363 |
| - response3 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"\x00\x00\xff\xff") |
| 330 | + async def test__receive_and_check_zlib_when_issue_during_reception_of_multiple_frames(self, transport_impl): |
| 331 | + response1 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"x\xda\xf2H\xcd\xc9") |
| 332 | + response2 = StubResponse(type=aiohttp.WSMsgType.ERROR, data="Something broke!") |
| 333 | + response3 = StubResponse(type=aiohttp.WSMsgType.BINARY, data=b"\x00\xff\xff") |
364 | 334 | transport_impl._ws.receive = mock.AsyncMock(side_effect=[response1, response2, response3])
|
365 |
| - transport_impl._zlib = mock.Mock(decompress=mock.Mock(return_value=b"decoded utf-8 encoded bytes")) |
366 |
| - |
367 |
| - assert await transport_impl._receive_and_check_complete_zlib_package(b"some") == "decoded utf-8 encoded bytes" |
| 335 | + transport_impl._ws.exception = mock.Mock(return_value=None) |
368 | 336 |
|
369 |
| - assert transport_impl._ws.receive.call_count == 3 |
370 |
| - transport_impl._ws.receive.assert_has_awaits([mock.call(), mock.call(), mock.call()]) |
371 |
| - transport_impl._zlib.decompress.assert_called_once_with(bytearray(b"somemoredata\x00\x00\xff\xff")) |
| 337 | + with pytest.raises( |
| 338 | + errors.GatewayTransportError, match=r"Gateway transport error: 'Something broke!' \[extra=None, type=258\]" |
| 339 | + ): |
| 340 | + await transport_impl._receive_and_check_zlib() |
372 | 341 |
|
373 | 342 | @pytest.mark.parametrize("transport_compression", [True, False])
|
374 | 343 | @pytest.mark.asyncio
|
@@ -1002,7 +971,7 @@ async def test__connect_when_not_reconnecting(self, client, http_settings, proxy
|
1002 | 971 | with stack:
|
1003 | 972 | assert await client._connect() == (heartbeat_task, poll_events_task)
|
1004 | 973 |
|
1005 |
| - log_filterer.assert_called_once_with("sometoken") |
| 974 | + log_filterer.assert_called_once_with(b"sometoken") |
1006 | 975 | gateway_transport_connect.assert_called_once_with(
|
1007 | 976 | http_settings=http_settings,
|
1008 | 977 | log_filterer=log_filterer.return_value,
|
@@ -1087,7 +1056,7 @@ async def test__connect_when_reconnecting(self, client, http_settings, proxy_set
|
1087 | 1056 | with stack:
|
1088 | 1057 | assert await client._connect() == (heartbeat_task, poll_events_task)
|
1089 | 1058 |
|
1090 |
| - log_filterer.assert_called_once_with("sometoken") |
| 1059 | + log_filterer.assert_called_once_with(b"sometoken") |
1091 | 1060 | gateway_transport_connect.assert_called_once_with(
|
1092 | 1061 | http_settings=http_settings,
|
1093 | 1062 | log_filterer=log_filterer.return_value,
|
|
0 commit comments