def test_send_string(self): """ Ensure the raw string form of the message is correctly turned into a valid netstring. """ transport = mock.MagicMock() transport.write = mock.MagicMock() connector = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p.send_string('foo bar baz') transport.write.assert_called_once_with(b'11:foo bar baz,')
def test_send_utf8_string_with_correct_length(self): """ Ensure the raw string containing weird unicode chars is correctly turned into a valid netstring with the correct length. """ transport = mock.MagicMock() transport.write = mock.MagicMock() connector = mock.MagicMock() node = mock.MagicMock() p = NetstringProtocol(connector, node) p.connection_made(transport) p.send_string('zɐq ɹɐq ooɟ') length = len('zɐq ɹɐq ooɟ'.encode('utf-8')) expected = '%d:zɐq ɹɐq ooɟ,' % length transport.write.assert_called_once_with(expected.encode('utf-8'))