| | 675 | |
| | 676 | def test_LISTUnicode(self): |
| | 677 | """ |
| | 678 | LIST will receive Unicode filenames for IFTPShell.list, and will |
| | 679 | encode them using UTF-8. |
| | 680 | """ |
| | 681 | # Login |
| | 682 | d = self._anonymousLogin() |
| | 683 | |
| | 684 | def pachedList(me, segments, attributes): |
| | 685 | return defer.succeed([( |
| | 686 | u'my resum\xe9', (0, 1, 0777, 0, 0, 'user', 'group'))]) |
| | 687 | self.patch(ftp.FTPAnonymousShell, 'list', pachedList) |
| | 688 | |
| | 689 | self._download('LIST something', chainDeferred=d) |
| | 690 | |
| | 691 | def checkDownload(download): |
| | 692 | self.assertEqual( |
| | 693 | 'drwxrwxrwx 0 user group ' |
| | 694 | '0 Jan 01 1970 my resum\xc3\xa9\r\n', |
| | 695 | download) |
| | 696 | return d.addCallback(checkDownload) |
| | 697 | |
| | 698 | |
| | 783 | def test_NLSTUnicode(self): |
| | 784 | """ |
| | 785 | NLST will receive Unicode filenames for IFTPShell.list, and will |
| | 786 | encode them using UTF-8. |
| | 787 | """ |
| | 788 | # Login |
| | 789 | d = self._anonymousLogin() |
| | 790 | |
| | 791 | def pachedList(me, segments): |
| | 792 | return defer.succeed([(u'my resum\xe9', None)]) |
| | 793 | self.patch(ftp.FTPAnonymousShell, 'list', pachedList) |
| | 794 | |
| | 795 | self._download('NLST something', chainDeferred=d) |
| | 796 | |
| | 797 | def checkDownload(download): |
| | 798 | self.assertEqual('my resum\xc3\xa9\r\n', download) |
| | 799 | return d.addCallback(checkDownload) |
| | 800 | |
| | 801 | |
| | 1029 | class DTPTests(unittest.TestCase): |
| | 1030 | """ |
| | 1031 | Tests for L{ftp.DTP}. |
| | 1032 | |
| | 1033 | The DTP instances in these tests are generated using |
| | 1034 | DTPFactory.buildProtocol() |
| | 1035 | """ |
| | 1036 | |
| | 1037 | def setUp(self): |
| | 1038 | """ |
| | 1039 | Create a fake protocol interpreter, a L{ftp.DTPFactory} instance, |
| | 1040 | and dummy transport to help with tests. |
| | 1041 | """ |
| | 1042 | self.reactor = task.Clock() |
| | 1043 | |
| | 1044 | class ProtocolInterpreter(object): |
| | 1045 | dtpInstance = None |
| | 1046 | |
| | 1047 | self.protocolInterpreter = ProtocolInterpreter() |
| | 1048 | self.factory = ftp.DTPFactory( |
| | 1049 | self.protocolInterpreter, None, self.reactor) |
| | 1050 | self.transport = proto_helpers.StringTransportWithDisconnection() |
| | 1051 | |
| | 1052 | |
| | 1053 | def test_sendLine_newline(self): |
| | 1054 | """ |
| | 1055 | Whend sending a line, the newline delimiter will be autoamtically |
| | 1056 | added. |
| | 1057 | """ |
| | 1058 | dtp_instance = self.factory.buildProtocol(None) |
| | 1059 | dtp_instance.makeConnection(self.transport) |
| | 1060 | line_content = 'line content' |
| | 1061 | |
| | 1062 | dtp_instance.sendLine(line_content) |
| | 1063 | |
| | 1064 | data_sent = self.transport.value() |
| | 1065 | self.assertEqual(line_content + '\r\n', data_sent) |
| | 1066 | |
| | 1067 | |
| | 1068 | def test_sendLine_unicode(self): |
| | 1069 | """ |
| | 1070 | When sending an unicode line, it will be converted to str and |
| | 1071 | a warning is raised. |
| | 1072 | """ |
| | 1073 | dtp_instance = self.factory.buildProtocol(None) |
| | 1074 | dtp_instance.makeConnection(self.transport) |
| | 1075 | line_content = u'my resum\xe9' |
| | 1076 | |
| | 1077 | self.assertWarns( |
| | 1078 | UserWarning, |
| | 1079 | "Unicode date received. " |
| | 1080 | "Encoded to UTF-8. Please send only alreay encoded data.", |
| | 1081 | ftp.__file__, |
| | 1082 | dtp_instance.sendLine, line_content) |
| | 1083 | |
| | 1084 | data_sent = self.transport.value() |
| | 1085 | self.assertTrue(isinstance(data_sent, str)) |
| | 1086 | self.assertEqual(line_content.encode('utf-8') + '\r\n', data_sent) |
| | 1087 | |
| | 1088 | |