def test_when_sending_a_message_then_message_length_and_encoding_is_sent_on_socket( self) -> None: self.socket_connection.send_msg(self.test_message) calls = self.socket.method_calls message_data = self.test_message.encode() expected_calls = [ call.sendall(struct.pack('!I', len(message_data))), call.sendall(message_data) ] self.assertListEqual(expected_calls, calls)
def test_too_many(self): source = unittest.mock.MagicMock() dest = unittest.mock.MagicMock() source.recv.side_effect = b'fubar' OneWayThread(source, dest, limit=2).run() source.sendall.assert_has_calls = [call.recv(4096), \ call.recv(4096), call.close()] dest.sendall.assert_has_calls = [ \ call.sendall('f'), \ call.sendall('u'), \ call.close()]
def test_simple_data(self): source = unittest.mock.Mock() dest = unittest.mock.Mock() source.recv.side_effect = b'fubar' OneWayThread(source, dest).run() source.sendall.assert_has_calls = [call.recv(4096), call.close()] dest.sendall.assert_has_calls = [ \ call.sendall('f'), \ call.sendall('u'), \ call.sendall('b'), \ call.sendall('a'), \ call.sendall('r'), \ call.close()]
def test_send(): """The function 'send' shall transmit data to the socket in two steps: 1. Send the length of the data to transmit in bytes followed by the delimiter '\n', 2. Send the data; and shall return the boolean value True if no error occurred during this operation. """ client = SocketClient("test_send") mock_socket = mock.Mock() client.sock = mock_socket data = 123 ok = client.send(data) expected = [ call.sendall(b'a%d\n' % len(str(data))), call.sendall(str(data).encode()) ] assert mock_socket.mock_calls == expected assert ok is True