|
| 1 | +import logging |
| 2 | +import ssl |
| 3 | +import unittest |
| 4 | +from threading import Lock |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +from slack_sdk.socket_mode.builtin.internals import _parse_handshake_response |
| 8 | +from slack_sdk.socket_mode.client import BaseSocketModeClient |
| 9 | + |
| 10 | + |
| 11 | +class TestSocketModeClient(unittest.TestCase): |
| 12 | + logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + def test_connect_to_new_endpoint_does_not_release_lock_on_acquisition_timeout(self): |
| 15 | + client = BaseSocketModeClient.__new__(BaseSocketModeClient) |
| 16 | + client.logger = self.logger |
| 17 | + lock_mock = MagicMock(spec=Lock()) |
| 18 | + lock_mock.acquire.return_value = False |
| 19 | + client.connect_operation_lock = lock_mock |
| 20 | + |
| 21 | + client.connect_to_new_endpoint() |
| 22 | + |
| 23 | + client.connect_operation_lock.release.assert_not_called() |
| 24 | + |
| 25 | + def test_connect_to_new_endpoint_releases_lock_on_successful_acquisition(self): |
| 26 | + client = BaseSocketModeClient.__new__(BaseSocketModeClient) |
| 27 | + client.logger = self.logger |
| 28 | + client.connect_operation_lock = Lock() |
| 29 | + |
| 30 | + with patch.object(client, client.is_connected.__name__, return_value=True): |
| 31 | + client.connect_to_new_endpoint() |
| 32 | + |
| 33 | + acquired = client.connect_operation_lock.acquire(blocking=False) |
| 34 | + self.assertTrue(acquired) |
| 35 | + client.connect_operation_lock.release() |
| 36 | + |
| 37 | + def test_parse_handshake_response_preserves_colons_in_header_values(self): |
| 38 | + lines = [ |
| 39 | + "HTTP/1.1 101 Switching Protocols", |
| 40 | + "Upgrade: websocket", |
| 41 | + "Location: https://example.com:8080/path", |
| 42 | + "", |
| 43 | + ] |
| 44 | + with patch( |
| 45 | + "slack_sdk.socket_mode.builtin.internals._read_http_response_line", |
| 46 | + side_effect=lines, |
| 47 | + ): |
| 48 | + status, headers, _ = _parse_handshake_response(MagicMock(spec=ssl.SSLSocket)) |
| 49 | + |
| 50 | + self.assertEqual(status, 101) |
| 51 | + self.assertEqual(headers["upgrade"], "websocket") |
| 52 | + self.assertEqual(headers["location"], "https://example.com:8080/path") |
| 53 | + |
| 54 | + def test_parse_handshake_response_parses_standard_headers(self): |
| 55 | + lines = [ |
| 56 | + "HTTP/1.1 200 OK", |
| 57 | + "Content-Type: text/html", |
| 58 | + "", |
| 59 | + ] |
| 60 | + with patch( |
| 61 | + "slack_sdk.socket_mode.builtin.internals._read_http_response_line", |
| 62 | + side_effect=lines, |
| 63 | + ): |
| 64 | + status, headers, _ = _parse_handshake_response(MagicMock(spec=ssl.SSLSocket)) |
| 65 | + |
| 66 | + self.assertEqual(status, 200) |
| 67 | + self.assertEqual(headers["content-type"], "text/html") |
0 commit comments