def test_string_received(self): """ Ensure the string_received method works as expected. """ transport = mock.MagicMock() addr = ('192.168.0.1', 9999) transport.get_extra_info = mock.MagicMock(return_value=addr) connector = mock.MagicMock() connector.receive = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p.string_received('hello world') connector.receive.assert_called_once_with('hello world', '192.168.0.1', node, p)
def test_data_received_containing_weird_unicode(self): """ Ensure the netstring is re-constructed correctly if it contains weird unicode characters and a non-obvious length. """ p = NetstringProtocol('foo', 'bar') p.connection_made(mock.MagicMock()) p.string_received = mock.MagicMock() p.data_received('15:zɐq ɹɐq ooɟ,'.encode('utf-8')) p.string_received.assert_called_once_with('zɐq ɹɐq ooɟ')
def test_data_received_in_chunks(self): """ Ensure the netstring is re-constructed correctly if it is received over several calls to data_received (i.e. ensure the FSM is working). """ p = NetstringProtocol('foo', 'bar') p.connection_made(mock.MagicMock()) p.string_received = mock.MagicMock() p.data_received('11:hello '.encode('utf-8')) p.data_received('world,'.encode('utf-8')) p.string_received.assert_called_once_with('hello world')
def test_data_received_more_data(self): """ Ensure that sequences of netstring from the remote peer are correctly handled. """ p = NetstringProtocol('foo', 'bar') p.connection_made(mock.MagicMock()) p.string_received = mock.MagicMock() p.data_received('11:hello world,'.encode('utf-8')) p.data_received('11:hello world,'.encode('utf-8')) self.assertEqual(2, p.string_received.call_count)
def test_data_received(self): """ Ensure that good data results in the expected results: * No errors. * The correct call to string received. """ p = NetstringProtocol('foo', 'bar') p.connection_made(mock.MagicMock()) p.string_received = mock.MagicMock() p.data_received('11:hello world,'.encode('utf-8')) p.string_received.assert_called_once_with('hello world')