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_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_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(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')
def test_data_received_length_too_long(self): """ If the netstring is expected to be too long then ensure the problem is handled correctly. """ transport = mock.MagicMock() transport.close = mock.MagicMock() connector = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p.data_received('999999999999999999:hello world,'.encode('utf-8')) self.assertEqual(1, transport.close.call_count)
def test_data_received_bad_length(self): """ If the netstring does not start with an integer defining the length of the message then ensure the problem is handled correctly. """ transport = mock.MagicMock() transport.close = mock.MagicMock() connector = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p.data_received('foo:hello world,'.encode('utf-8')) self.assertEqual(1, transport.close.call_count)
def test_data_received_bad_end_character(self): """ If the netstring does not terminate with a comma ensure that it is handled correctly. """ transport = mock.MagicMock() transport.close = mock.MagicMock() connector = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p.data_received('11:hello world@'.encode('utf-8')) self.assertEqual(1, transport.close.call_count)
def test_data_received_in_bad_state(self): """ Ensure the correct RuntimeError is called if the state of the FSM is invalid. """ transport = mock.MagicMock() transport.close = mock.MagicMock() connector = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p._reader_state = 4 with self.assertRaises(RuntimeError): p.data_received('11:hello world@'.encode('utf-8'))