Пример #1
0
class TestWebSocketClient(unittest.TestCase):

    url = "wss://example.com:17070"

    def setUp(self):
        self.client = WebSocketClient(self.url)

    def test_connect(self, mock_create_connection):
        # The WebSocket connection is correctly established.
        self.client.connect()
        mock_create_connection.assert_called_once_with(self.url, timeout=600, sslopt=SSLOPT)

    def test_send(self, mock_create_connection):
        # A request is correctly sent, and a response is returned.
        request = {"request": "foo"}
        expected_request = json.dumps(request)
        expected_response = {"response": "bar"}
        mock_connection = mock_create_connection()
        mock_connection.recv.return_value = json.dumps(expected_response)
        self.client.connect()
        response = self.client.send(request)
        self.assertEqual(expected_response, response)
        mock_connection.send.assert_called_once_with(expected_request)

    def test_close(self, mock_create_connection):
        # A WebSocket connection is correctly terminated.
        self.client.connect()
        self.client.close()
        mock_create_connection().close.assert_called_once_with()
Пример #2
0
class TestWebSocketClient(unittest.TestCase):

    url = 'wss://example.com:17070'

    def setUp(self):
        self.client = WebSocketClient(self.url)

    def test_connect(self, mock_create_connection):
        # The WebSocket connection is correctly established.
        self.client.connect()
        mock_create_connection.assert_called_once_with(self.url,
                                                       timeout=600,
                                                       sslopt=SSLOPT)

    def test_send(self, mock_create_connection):
        # A request is correctly sent, and a response is returned.
        request = {'request': 'foo'}
        expected_request = json.dumps(request)
        expected_response = {'response': 'bar'}
        mock_connection = mock_create_connection()
        mock_connection.recv.return_value = json.dumps(expected_response)
        self.client.connect()
        response = self.client.send(request)
        self.assertEqual(expected_response, response)
        mock_connection.send.assert_called_once_with(expected_request)

    def test_close(self, mock_create_connection):
        # A WebSocket connection is correctly terminated.
        self.client.connect()
        self.client.close()
        mock_create_connection().close.assert_called_once_with()
Пример #3
0
 def setUp(self):
     self.client = WebSocketClient(self.url)
Пример #4
0
 def setUp(self):
     self.client = WebSocketClient(self.url)