Ticket #5411: 5411-ftp-list-unicode-1.diff

File 5411-ftp-list-unicode-1.diff, 2.9 KB (added by adiroiban, 4 years ago)
  • twisted/test/test_ftp.py

     
    962962        return d
    963963
    964964
     965class DTPTests(unittest.TestCase):
     966    """
     967    Tests for L{ftp.DTP}.
    965968
     969    The DTP instances in these tests are generated using
     970    DTPFactory.buildProtocol()
     971    """
     972
     973    def setUp(self):
     974        """
     975        Create a fake protocol interpreter, a L{ftp.DTPFactory} instance,
     976        and dummy transport to help with tests.
     977        """
     978        self.reactor = task.Clock()
     979
     980        class ProtocolInterpreter(object):
     981            dtpInstance = None
     982
     983        self.protocolInterpreter = ProtocolInterpreter()
     984        self.factory = ftp.DTPFactory(
     985            self.protocolInterpreter, None, self.reactor)
     986        self.transport = proto_helpers.StringTransportWithDisconnection()
     987
     988
     989    def test_sendLine_newline(self):
     990        """
     991        Whend sending a line, the newline delimiter will be autoamtically
     992        added.
     993        """
     994        dtp_instance = self.factory.buildProtocol(None)
     995        dtp_instance.makeConnection(self.transport)
     996        line_content = 'line content'
     997
     998        dtp_instance.sendLine(line_content)
     999
     1000        data_sent = self.transport.value()
     1001        self.assertEqual(line_content + '\r\n', data_sent)
     1002
     1003
     1004    def test_sendLine_unicode(self):
     1005        """
     1006        When sending an unicode line, it will be converted to str.
     1007        """
     1008        dtp_instance = self.factory.buildProtocol(None)
     1009        dtp_instance.makeConnection(self.transport)
     1010        line_content = u'my resum\xe9'
     1011
     1012        dtp_instance.sendLine(line_content)
     1013
     1014        data_sent = self.transport.value()
     1015        self.assertTrue(isinstance(data_sent, str))
     1016        # Please advise what test to use.
     1017        # self.assertEqual('my resum\xc3\xa9' + '\r\n', data_sent)
     1018        self.assertEqual(line_content.encode('utf-8') + '\r\n', data_sent)
     1019
     1020
    9661021# -- Client Tests -----------------------------------------------------------
    9671022
    9681023class PrintLines(protocol.Protocol):
  • twisted/protocols/ftp.py

     
    386386            self._onConnLost.callback(None)
    387387
    388388    def sendLine(self, line):
     389        """
     390        Send a line to data channel.
     391
     392        @type  line: I{str} or I{unicdoe}
     393        @param line: The line to be sent.
     394
     395        If line is I{unicode}, it will be converted to I{str}.
     396        """
     397        if isinstance(line, unicode):
     398            line = line.encode('utf-8')
    389399        self.transport.write(line + '\r\n')
    390400
    391 
    392401    def _formatOneListResponse(self, name, size, directory, permissions, hardlinks, modified, owner, group):
    393402        def formatMode(mode):
    394403            return ''.join([mode & (256 >> n) and 'rwx'[n % 3] or '-' for n in range(9)])